$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
require 'slim'
require 'context'
require 'benchmark/ips'
require 'tilt'
require 'erubi'
require 'erb'
require 'haml'
require 'faml'
require 'hamlit'
class SlimBenchmarks
def initialize(only_haml)
@only_haml = only_haml
@benches = []
@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, escape_html: true)
haml_ugly.def_method(context, :run_haml_ugly)
context.instance_eval %{
def run_erubi; #{Erubi::Engine.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("erubi v#{Erubi::VERSION}") { context.run_erubi } unless @only_haml
bench("slim v#{Slim::VERSION}") { context.run_slim_ugly } unless @only_haml
bench("haml v#{Haml::VERSION}") { context.run_haml_ugly }
bench("faml v#{Faml::VERSION}") { context.run_faml }
bench("hamlit v#{Hamlit::VERSION}") { context.run_hamlit }
end
def run
Benchmark.ips do |x|
@benches.each do |name, block|
x.report(name.to_s, &block)
end
x.compare!
end
end
def bench(name, &block)
@benches.push([name, block])
end
end
SlimBenchmarks.new(ENV['ONLY_HAML'] == '1').run