diff --git a/Gemfile b/Gemfile index f69b825c..27b82141 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ end gemspec -gem 'liquid', github: 'Shopify/liquid', ref: 'master' +gem 'liquid', github: 'Shopify/liquid', ref: 'pz-serialize-benchmark-refactor' group :test do gem 'rubocop', '~> 0.93.1', require: false diff --git a/Rakefile b/Rakefile index ec4be2e4..0fe62a25 100644 --- a/Rakefile +++ b/Rakefile @@ -73,11 +73,13 @@ namespace :benchmark do desc "Run the liquid benchmark with lax parsing" task :run do ruby "./performance.rb c benchmark lax" + ruby "./performance/serialization.rb lax" end desc "Run the liquid benchmark with strict parsing" task :strict do ruby "./performance.rb c benchmark strict" + ruby "./performance/serialization.rb strict" end end diff --git a/performance/serialization.rb b/performance/serialization.rb new file mode 100644 index 00000000..e12d33d9 --- /dev/null +++ b/performance/serialization.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'liquid' +require 'liquid/c' +require 'benchmark/ips' + +liquid_lib_dir = $LOAD_PATH.detect { |p| File.exist?(File.join(p, 'liquid.rb')) } +require File.join(File.dirname(liquid_lib_dir), "performance/theme_runner.rb") + +module SerializationThemeRunner + def initialize + super + serialize_all_tests + end + + def deserialize + @serialized_tests.each do |test| + Liquid::Template.load(test[:tmpl]) + Liquid::Template.load(test[:layout]) + end + end + + def deserialize_and_render + @serialized_tests.each do |test| + tmpl = test[:tmpl] + assigns = test[:assigns] + layout = test[:layout] + + render_layout(Liquid::Template.load(tmpl), Liquid::Template.load(layout), assigns) + end + end + + private + + def serialize_all_tests + @serialized_tests = [] + @compiled_tests.each do |test_hash| + @serialized_tests << + if test_hash[:layout] + { tmpl: test_hash[:tmpl].dump, assigns: test_hash[:assigns], layout: test_hash[:layout].dump } + else + { tmpl: test_hash[:tmpl].dump, assigns: test_hash[:assigns] } + end + end + @serialized_tests + end +end + +ThemeRunner.prepend(SerializationThemeRunner) + +Liquid::Template.error_mode = ARGV.first.to_sym +profiler = ThemeRunner.new + +Benchmark.ips do |x| + x.time = 10 + x.warmup = 5 + + puts + puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)." + puts + + x.report("deserialize:") { profiler.deserialize } + x.report("deserialize & render:") { profiler.deserialize_and_render } +end