forked from ruby-i18n/i18n
/
benchmark_lazyload_test.rb
64 lines (53 loc) · 1.63 KB
/
benchmark_lazyload_test.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
require 'test_helper'
require 'benchmark'
require 'securerandom'
require "byebug"
class BenchmarkLazyLoadTest < I18n::TestCase
test "lazy load performance" do
benchmark(backend: I18n::Backend::LazyLoad.new)
end
test "simple performance" do
benchmark(backend: I18n::Backend::Simple.new)
end
def benchmark(backend:)
@backend = I18n.backend = backend
@backend.reload!
en_files = create_temp_translation_files(locale: "en", num_files: 100, num_keys: 1000)
fr_files = create_temp_translation_files(locale: "fr", num_files: 100, num_keys: 1000)
de_files = create_temp_translation_files(locale: "de", num_files: 100, num_keys: 1000)
Benchmark.bm do |x|
puts "\n"
x.report(@backend.class) do
I18n.with_locale(:en) do
@backend.eager_load!
end
end
end
remove_tempfiles(en_files)
remove_tempfiles(fr_files)
remove_tempfiles(de_files)
end
def create_temp_translation_files(locale:, num_files:, num_keys:)
paths = []
num_files.times do |file_num|
path = File.join(Dir.tmpdir, "#{locale}-#{SecureRandom.uuid}.yml")
File.write(path, generate_random_file_content(locale, num_keys, file_num))
paths << path
I18n.load_path << path
end
paths
end
def generate_random_file_content(locale, num_keys, file_num)
content = {}
num_keys.times do |key_num|
content["#{file_num}-#{key_num}"] = SecureRandom.alphanumeric
end
{ locale => content }.to_yaml
end
def remove_tempfiles(paths)
paths.each do |path|
I18n.load_path.delete(path)
File.delete(path) if File.exist?(path)
end
end
end