/
benchmark.rb
executable file
·125 lines (106 loc) · 2.39 KB
/
benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env ruby
# encoding: utf-8
require 'rubygems'
begin
require 'bundler/setup'
rescue LoadError
puts "Bundler not available. Install it with: gem install bundler"
end
YAML::ENGINE.yamler = 'psych' if 'psych' == ARGV.first
require 'pathname'
dir = Pathname.new(__FILE__).dirname.expand_path
require 'i18n'
require 'rbench'
require 'r18n-core'
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
I18n::Backend::Simple.send(:include, I18n::Backend::InterpolationCompiler)
I18n::Backend::Simple.send(:include, I18n::Backend::Flatten)
class Array
def nitems
count {|i| !i.nil?}
end
end
RBench.run(1000) do
column :r18n, :title => 'R18n'
column :i18n, :title => 'Rails I18n'
column :diff, :title => 'I18n/R18n', :compare => [:i18n, :r18n]
report 'cold load' do
r18n {
R18n.clear_cache!
R18n.set(%w{ru fr en}, dir + 'r18n')
R18n.get.available_locales
}
i18n {
I18n.reload!
I18n.available_locales = nil
I18n.load_path = [Dir.glob(dir.join('i18n/*.{yml,rb}').to_s)]
I18n.locale = :ru
I18n.fallbacks[:ru] = [:ru, :en]
I18n.available_locales
}
end
report 'load' do
r18n {
R18n.set(%w{ru fr en}, dir + 'r18n')
R18n.get.available_locales
}
i18n {
I18n.locale = :ru
I18n.fallbacks[:ru] = [:ru, :en]
I18n.available_locales
}
end
report 'get' do
r18n {
R18n.get.user.name
R18n.get.users.new.title
}
i18n {
I18n.t :'user.name'
I18n.t :'users.new.title'
}
end
report 'variables' do
r18n {
R18n.get.user.your_name('John', 'user123')
}
i18n {
I18n.t :'user.your_name', :name => 'John', :login => 'user123'
}
end
report 'pluralize' do
r18n {
R18n.get.user.count(51)
}
i18n {
I18n.t :'user.count', :count => 51
}
end
report 'localize' do
r18n {
R18n.get.l Time.now
}
i18n {
I18n.l Time.now
}
end
report 'default' do
r18n {
R18n.get.not.exists | 'default'
R18n.get.not.exists | R18n.get.user.name
}
i18n {
I18n.t :'not.exists', :default => 'default'
I18n.t :'not.exists', :default => :'user.name'
}
end
report 'fallback' do
r18n {
R18n.get.users.edit.title
}
i18n {
I18n.t :'users.edit.title'
}
end
end