From f6086794b05cd6d6977681fb9255e10e70c45f9f Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Fri, 12 Jun 2015 01:00:23 +0900 Subject: [PATCH] Split benchmarks for html escaping --- .travis.yml | 8 ++++ benchmarks/benchmark.rb | 80 +++++++++++++++++++++++++----------- benchmarks/view.escaped.slim | 17 ++++++++ 3 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 benchmarks/view.escaped.slim diff --git a/.travis.yml b/.travis.yml index 43db400a..a3014e8a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,9 +14,11 @@ env: - TASK=spec - TASK=rails:spec - TASK=bench TIME=20 + - TASK=bench TIME=20 HTML_ESCAPE=1 matrix: allow_failures: - env: TASK=bench TIME=20 + - env: TASK=bench TIME=20 HTML_ESCAPE=1 exclude: - env: TASK=spec gemfile: spec/rails/Gemfile @@ -24,11 +26,17 @@ matrix: gemfile: Gemfile - env: TASK=bench TIME=20 gemfile: spec/rails/Gemfile + - env: TASK=bench TIME=20 HTML_ESCAPE=1 + gemfile: spec/rails/Gemfile - rvm: 2.1 env: TASK=rails:spec - rvm: 2.1 env: TASK=bench TIME=20 + - rvm: 2.1 + env: TASK=bench TIME=20 HTML_ESCAPE=1 - rvm: 2.0.0 env: TASK=rails:spec - rvm: 2.0.0 env: TASK=bench TIME=20 + - rvm: 2.0.0 + env: TASK=bench TIME=20 HTML_ESCAPE=1 diff --git a/benchmarks/benchmark.rb b/benchmarks/benchmark.rb index 94dead13..91b33d14 100644 --- a/benchmarks/benchmark.rb +++ b/benchmarks/benchmark.rb @@ -16,31 +16,20 @@ class Benchmarks @time = time @benches = [] @versions = {} - - @erb_code = File.read(File.dirname(__FILE__) + '/view.erb') - @haml_code = File.read(File.dirname(__FILE__) + '/view.haml') - @slim_code = File.read(File.dirname(__FILE__) + '/view.slim') - - init_compiled_benches end - def init_compiled_benches - context = Context.new - - haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true) - haml_ugly.def_method(context, :run_haml_ugly) - context.instance_eval %{ - def run_erubis; #{Erubis::Eruby.new(@erb_code).src}; end - def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end - def run_faml; #{Faml::Engine.new.call @haml_code}; end - def run_hamlit; #{Hamlit::Engine.new.call @haml_code}; end - } - - bench('hamlit', Hamlit::VERSION) { context.run_hamlit } - bench('erubis', Erubis::VERSION) { context.run_erubis } - bench('slim', Slim::VERSION) { context.run_slim_ugly } - bench('faml', Faml::VERSION) { context.run_faml } - bench('haml', Haml::VERSION) { context.run_haml_ugly } + def init_compiled_benches(escape_enabled) + if escape_enabled + @erb_code = File.read(File.dirname(__FILE__) + '/view.erb') + @haml_code = File.read(File.dirname(__FILE__) + '/view.haml') + @slim_code = File.read(File.dirname(__FILE__) + '/view.escaped.slim') + init_escaped_benches + else + @erb_code = File.read(File.dirname(__FILE__) + '/view.erb') + @haml_code = File.read(File.dirname(__FILE__) + '/view.haml') + @slim_code = File.read(File.dirname(__FILE__) + '/view.slim') + init_plain_benches + end end def run @@ -56,6 +45,47 @@ class Benchmarks private + # Totally the same as slim-template/slim's compiled bench. + def init_plain_benches + puts 'Compiled rendering benchmarks without HTML escape' + context = Context.new + + haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true) + haml_ugly.def_method(context, :run_haml_ugly) + context.instance_eval %{ + def run_erubis; #{Erubis::Eruby.new(@erb_code).src}; end + def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end + def run_hamlit; #{Hamlit::Engine.new(escape_html: false).call @haml_code}; end + } + + bench('hamlit', Hamlit::VERSION) { context.run_hamlit } + bench('erubis', Erubis::VERSION) { context.run_erubis } + bench('slim', Slim::VERSION) { context.run_slim_ugly } + bench('haml', Haml::VERSION) { context.run_haml_ugly } + end + + # slim-template/slim's compiled bench with HTML escaping. + def init_escaped_benches + puts 'Compiled rendering benchmarks with HTML escape' + context = Context.new + context.instance_eval("def header; '