1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/benchmarks/benchmark.rb

98 lines
2.6 KiB
Ruby
Raw Normal View History

2015-03-09 02:22:59 -04:00
#!/usr/bin/env ruby
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
require_relative './context'
require 'benchmark/ips'
2015-03-09 02:37:26 -04:00
2015-03-09 02:22:59 -04:00
require 'erb'
2015-03-09 02:37:26 -04:00
require 'erubis'
2015-03-28 01:32:20 -04:00
require 'faml'
2015-03-09 02:22:59 -04:00
require 'haml'
require 'slim'
2015-03-09 03:08:19 -04:00
require 'tenjin'
2015-03-09 02:37:26 -04:00
require 'tilt'
require 'hamlit'
2015-03-09 02:22:59 -04:00
class Benchmarks
2015-03-09 06:14:31 -04:00
def initialize(time)
@time = time
@benches = []
2015-03-09 02:22:59 -04:00
2015-03-09 03:08:19 -04:00
@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')
@rbhtml_path = File.dirname(__FILE__) + '/view.rbhtml'
2015-03-09 02:22:59 -04:00
init_compiled_benches
end
def init_compiled_benches
erb = ERB.new(@erb_code)
erubis = Erubis::Eruby.new(@erb_code)
fast_erubis = Erubis::FastEruby.new(@erb_code)
haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true)
2015-03-28 01:36:23 -04:00
hamlit_ugly = Hamlit::Engine.new(ugly: true)
2015-03-09 03:08:19 -04:00
tenjin = Tenjin::Engine.new.get_template(@rbhtml_path)
2015-03-09 02:22:59 -04:00
2015-03-09 03:08:19 -04:00
context = Context.new
2015-03-09 02:22:59 -04:00
haml_ugly.def_method(context, :run_haml_ugly)
context.instance_eval %{
def run_erb; #{erb.src}; end
def run_erubis; #{erubis.src}; end
def run_temple_erb; #{Temple::ERB::Engine.new.call @erb_code}; end
def run_fast_erubis; #{fast_erubis.src}; end
def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
2015-03-28 01:32:20 -04:00
def run_faml; #{Faml::Engine.new.call @haml_code}; end
2015-03-09 03:08:19 -04:00
def run_tenjin; _buf = ''; #{tenjin.script}; end
2015-03-28 01:36:23 -04:00
def run_hamlit; #{hamlit_ugly.call @haml_code}; end
2015-03-09 02:22:59 -04:00
}
2015-03-28 01:21:48 -04:00
bench('hamlit') { context.run_hamlit }
2015-03-15 11:55:25 -04:00
bench('erubis') { context.run_erubis }
2015-03-28 01:32:20 -04:00
bench('faml') { context.run_faml }
2015-03-28 01:21:48 -04:00
bench('slim') { context.run_slim_ugly }
2015-03-15 11:55:25 -04:00
if ENV['ALL']
bench('tenjin') { context.run_tenjin }
bench('fast erubis') { context.run_fast_erubis }
bench('temple erb') { context.run_temple_erb }
bench('erb') { context.run_erb }
bench('haml') { context.run_haml_ugly }
end
2015-03-09 02:22:59 -04:00
end
def run
2015-03-28 01:21:48 -04:00
result = Benchmark.ips do |x|
2015-03-09 06:14:31 -04:00
x.config(time: @time, warmup: 2)
2015-03-09 02:22:59 -04:00
@benches.each do |name, block|
x.report(name, &block)
end
x.compare!
end
2015-03-28 01:21:48 -04:00
assert_fastest(result)
2015-03-09 02:22:59 -04:00
end
def bench(name, &block)
@benches.push([name, block])
end
2015-03-28 01:21:48 -04:00
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
2015-03-09 02:22:59 -04:00
end
2015-03-09 06:14:31 -04:00
time = (ENV['TIME'] || 5).to_i
Benchmarks.new(time).run