<commit_before>#! /usr/bin/ruby
$:.unshift File.expand_path("../lib", File.dirname(__FILE__))

require 'i18n'
require 'i18n/core_ext/object/meta_class'
require 'benchmark'
require 'yaml'

# Load YAML example file
YAML_HASH = YAML.load_file(File.expand_path("example.yml", File.dirname(__FILE__)))

# Create benchmark backends
def create_backend(*modules)
  Class.new do
    modules.unshift(:Base)
    modules.each { |m| include I18n::Backend.const_get(m) }
  end
end

BACKENDS = []
BACKENDS << (SimpleBackend        = create_backend)
BACKENDS << (FastBackend          = create_backend(:Fast))
BACKENDS << (InterpolationBackend = create_backend(:InterpolationCompiler))
BACKENDS << (FastInterpolBackend  = create_backend(:Fast, :InterpolationCompiler))

# Hack Report to print ms
module Benchmark
  def self.ms(label = "", width=20, &blk) # :yield:
    print label.ljust(width)
    res = Benchmark::measure(&blk)
    print format("%10.6f ms\n", res.real * 1000)
    res
  end
end

# Run!
BACKENDS.each do |backend|
  I18n.backend = backend.new
  puts "===> #{backend.name}\n\n"

  Benchmark.ms "store" do
    I18n.backend.store_translations *(YAML_HASH.to_a.first)
    I18n.backend.translate :en, :first
  end

  Benchmark.ms "t (depth=3)" do
    I18n.backend.translate :en, :"activerecord.models.user"
  end

  Benchmark.ms "t (depth=5)" do
    I18n.backend.translate :en, :"activerecord.attributes.admins.user.login"
  end

  Benchmark.ms "t (depth=7)" do
    I18n.backend.translate :en, :"activerecord.errors.models.user.attributes.login.blank"
  end

  Benchmark.ms "t w/ default" do
    I18n.backend.translate :en, :"activerecord.models.another", :default => "Another"<commit_msg>Measure objects allocated if available. Repeat 1000 times to reduce benchmark jitter.<commit_after>#! /usr/bin/ruby
$:.unshift File.expand_path("../lib", File.dirname(__FILE__))

require 'i18n'
require 'i18n/core_ext/object/meta_class'
require 'benchmark'
require 'yaml'

N = (ARGV.shift || 1000).to_i

# Load YAML example file
YAML_HASH = YAML.load_file(File.expand_path("example.yml", File.dirname(__FILE__)))

# Create benchmark backends
def create_backend(*modules)
  Class.new do
    modules.unshift(:Base)
    modules.each { |m| include I18n::Backend.const_get(m) }
  end
end

BACKENDS = []
BACKENDS << (SimpleBackend        = create_backend)
BACKENDS << (FastBackend          = create_backend(:Fast))
BACKENDS << (InterpolationBackend = create_backend(:InterpolationCompiler))
BACKENDS << (FastInterpolBackend  = create_backend(:Fast, :InterpolationCompiler))

# Hack Report to print ms
module Benchmark
  def self.ms(label = "", width=20, &blk) # :yield:
    print label.ljust(width)
    t, obj = measure_objects(&blk)
    print format("%8.2f ms  %8d objects\n", t * 1000, obj)
    t
  end

  if ObjectSpace.respond_to?(:allocated_objects)
    def self.measure_objects(&blk)
      obj = ObjectSpace.allocated_objects
      t = Benchmark.realtime { N.times(&blk) }
      [t, ObjectSpace.allocated_objects - obj]
    end
  else
    def self.measure_objects(&blk)
      [Benchmark.measure { N.times(&blk) }, 0]
    end
  end
end

# Run!
BACKENDS.each do |backend|
  I18n.backend = backend.new
  puts "===> #{backend.name} (#{N} translations)\n\n"

  Benchmark.ms "store" do
    I18n.backend.store_translations *(YAML_HASH.to_a.first)
    I18n.backend.translate :en, :first
  end

  Benchmark.ms "t (depth=3)" do
    I18n.backend.translate :en, :"activerecord.models.user"
  end

  Benchmark.ms "t (depth=5)" do
    I18n.backend.translate :en, :"activerecord.attributes.admins.user.login"
  end

  Benchmark.ms "t (depth=7)" do
    I18n.backend.translate :en, :"activerecord.errors.models.user.attributes.login.blank"
  end

  Benchmark.ms "t w/ default" do
    I18n.backend.translate :en, :"activerecord.models.another", :default => "Another"
{'fraction_non_alphanumeric': 0.13886713651788044, 'fraction_numerical': 0.01905507700339337, 'mean_word_length': 4.116154873164219, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1824753', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1357, 'n_words': 286}