Skip to content

Commit 5e7cfee

Browse files
committed
Implement benchmarks for deserialize, and deserialize & render
1 parent 5b1f9d8 commit 5e7cfee

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ end
66

77
gemspec
88

9-
gem 'liquid', github: 'Shopify/liquid', ref: 'master'
9+
gem 'liquid', github: 'Shopify/liquid', ref: 'pz-serialize-benchmark-refactor'
1010

1111
group :test do
1212
gem 'rubocop', '~> 0.93.1', require: false

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ namespace :benchmark do
7373
desc "Run the liquid benchmark with lax parsing"
7474
task :run do
7575
ruby "./performance.rb c benchmark lax"
76+
ruby "./performance/serialization.rb lax"
7677
end
7778

7879
desc "Run the liquid benchmark with strict parsing"
7980
task :strict do
8081
ruby "./performance.rb c benchmark strict"
82+
ruby "./performance/serialization.rb strict"
8183
end
8284
end
8385

performance/serialization.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require 'liquid'
4+
require 'liquid/c'
5+
require 'benchmark/ips'
6+
7+
liquid_lib_dir = $LOAD_PATH.detect { |p| File.exist?(File.join(p, 'liquid.rb')) }
8+
require File.join(File.dirname(liquid_lib_dir), "performance/theme_runner.rb")
9+
10+
module SerializationThemeRunner
11+
def initialize
12+
super
13+
serialize_all_tests
14+
end
15+
16+
def deserialize
17+
@serialized_tests.each do |test|
18+
Liquid::Template.load(test[:tmpl])
19+
Liquid::Template.load(test[:layout])
20+
end
21+
end
22+
23+
def deserialize_and_render
24+
@serialized_tests.each do |test|
25+
tmpl = test[:tmpl]
26+
assigns = test[:assigns]
27+
layout = test[:layout]
28+
29+
render_layout(Liquid::Template.load(tmpl), Liquid::Template.load(layout), assigns)
30+
end
31+
end
32+
33+
private
34+
35+
def serialize_all_tests
36+
@serialized_tests = []
37+
@compiled_tests.each do |test_hash|
38+
@serialized_tests <<
39+
if test_hash[:layout]
40+
{ tmpl: test_hash[:tmpl].dump, assigns: test_hash[:assigns], layout: test_hash[:layout].dump }
41+
else
42+
{ tmpl: test_hash[:tmpl].dump, assigns: test_hash[:assigns] }
43+
end
44+
end
45+
@serialized_tests
46+
end
47+
end
48+
49+
ThemeRunner.prepend(SerializationThemeRunner)
50+
51+
Liquid::Template.error_mode = ARGV.first.to_sym
52+
profiler = ThemeRunner.new
53+
54+
Benchmark.ips do |x|
55+
x.time = 10
56+
x.warmup = 5
57+
58+
puts
59+
puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)."
60+
puts
61+
62+
x.report("deserialize:") { profiler.deserialize }
63+
x.report("deserialize & render:") { profiler.deserialize_and_render }
64+
end

0 commit comments

Comments
 (0)