-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmark Test: Compare LazyLoadable vs Simple backend
- Loading branch information
1 parent
27c800d
commit 20ad16a
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'test_helper' | ||
require 'benchmark' | ||
require 'securerandom' | ||
|
||
class BenchmarkLazyLoadableTest < I18n::TestCase | ||
test "lazy load performance" do | ||
benchmark(backend: I18n::Backend::LazyLoadable.new(lazy_load: true)) | ||
end | ||
|
||
test "simple performance" do | ||
benchmark(backend: I18n::Backend::Simple.new) | ||
end | ||
|
||
def benchmark(backend:) | ||
@backend = I18n.backend = backend | ||
@backend.reload! | ||
|
||
num_files = 100 | ||
num_keys = 100 | ||
|
||
en_files = create_temp_translation_files(locale: "en", num_files: num_files, num_keys: num_keys) | ||
fr_files = create_temp_translation_files(locale: "fr", num_files: num_files, num_keys: num_keys) | ||
de_files = create_temp_translation_files(locale: "de", num_files: num_files, num_keys: num_keys) | ||
|
||
Benchmark.bm do |x| | ||
puts "\n" | ||
x.report(@backend.class) do | ||
I18n.with_locale(:en) { I18n.t("1.1") } | ||
assert_equal num_keys * num_files, translations[:en].size | ||
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 |