diff --git a/Rakefile b/Rakefile index d249e718..b86947b6 100644 --- a/Rakefile +++ b/Rakefile @@ -2,7 +2,7 @@ require 'bundler/gem_tasks' desc 'Run benchmarks' task :bench do - system('bundle exec ruby benchmarks/benchmark.rb') + exit system('bundle exec ruby benchmarks/benchmark.rb') end desc 'Run RSpec code examples' diff --git a/benchmarks/benchmark.rb b/benchmarks/benchmark.rb index 8e3b0922..345b3f44 100644 --- a/benchmarks/benchmark.rb +++ b/benchmarks/benchmark.rb @@ -48,10 +48,10 @@ class Benchmarks def run_hamlit; #{Hamlit::Engine.new.call @haml_code}; end } - bench('erubis') { context.run_erubis } - bench('slim') { context.run_slim_ugly } - bench('fast_haml') { context.run_fast_haml } bench('hamlit') { context.run_hamlit } + bench('erubis') { context.run_erubis } + bench('fast_haml') { context.run_fast_haml } + bench('slim') { context.run_slim_ugly } if ENV['ALL'] bench('tenjin') { context.run_tenjin } @@ -63,18 +63,33 @@ class Benchmarks end def run - Benchmark.ips do |x| + result = Benchmark.ips do |x| x.config(time: @time, warmup: 2) @benches.each do |name, block| x.report(name, &block) end x.compare! end + assert_fastest(result) end def bench(name, &block) @benches.push([name, block]) end + + def assert_fastest(result) + hamlit = result.entries.first + rival = result.entries[1..-1].max_by(&:ips) + + if hamlit.ips < rival.ips + fatal "Hamlit is slower than #{rival.label}" + end + end + + def fatal(message) + puts "\e[31m#{message}\e[0m" + exit 1 + end end time = (ENV['TIME'] || 5).to_i