Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

113 lines (92 sloc) 4.255 kb
#!/usr/bin/env ruby
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
require 'slim'
require 'context'
require 'benchmark'
require 'erubis'
require 'erb'
require 'haml'
class SlimBenchmarks
def initialize(slow, iterations)
@iterations = (iterations || 1000).to_i
@benches = []
tpl_erb = File.read(File.dirname(__FILE__) + '/view.erb')
tpl_haml = File.read(File.dirname(__FILE__) + '/view.haml')
tpl_slim = File.read(File.dirname(__FILE__) + '/view.slim')
context = Context.new
context_binding = context.instance_eval { binding }
erb = ERB.new(tpl_erb)
erubis = Erubis::Eruby.new(tpl_erb)
fast_erubis = Erubis::FastEruby.new(tpl_erb)
haml = Haml::Engine.new(tpl_haml, :format => :html5)
haml_ugly = Haml::Engine.new(tpl_haml, :format => :html5, :ugly => true)
slim = Slim::Template.new { tpl_slim }
tilt_erb = Tilt::ERBTemplate.new { tpl_erb }
tilt_erubis = Tilt::ErubisTemplate.new { tpl_erb }
tilt_haml = Tilt::HamlTemplate.new(:format => :html5){ tpl_haml }
tilt_haml_ugly = Tilt::HamlTemplate.new(:format => :html5, :ugly => true){ tpl_haml }
tilt_slim = Slim::Template.new { tpl_slim }
haml.def_method(context, :run_haml)
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_fast_erubis; #{fast_erubis.src}; end
def run_slim; #{slim.precompiled_template}; end
}
if slow
bench('(1) erb') { ERB.new(tpl_erb).result(context_binding) }
bench('(1) erubis') { Erubis::Eruby.new(tpl_erb).result(context_binding) }
bench('(1) fast erubis') { Erubis::Eruby.new(tpl_erb).result(context_binding) }
bench('(1) slim') { Slim::Template.new { tpl_slim }.render(context) }
bench('(1) haml') { Haml::Engine.new(tpl_haml, :format => :html5).render(context) }
bench('(1) haml ugly') { Haml::Engine.new(tpl_haml, :format => :html5, :ugly => true).render(context) }
end
bench('(2) erb') { erb.result(context_binding) }
bench('(2) erubis') { erubis.result(context_binding) }
bench('(2) fast erubis') { fast_erubis.result(context_binding) }
bench('(2) slim') { slim.render(context) }
bench('(2) haml') { haml.render(context) }
bench('(2) haml ugly') { haml_ugly.render(context) }
bench('(3) erb') { context.run_erb }
bench('(3) erubis') { context.run_erubis }
bench('(3) fast erubis') { context.run_fast_erubis }
bench('(3) slim') { context.run_slim }
bench('(3) haml') { context.run_haml }
bench('(3) haml ugly') { context.run_haml_ugly }
bench('(4) erb') { tilt_erb.render(context) }
bench('(4) erubis') { tilt_erubis.render(context) }
bench('(4) slim') { tilt_slim.render(context) }
bench('(4) haml') { tilt_haml.render(context) }
bench('(4) haml ugly') { tilt_haml_ugly.render(context) }
end
def run
puts "#{@iterations} Iterations"
Benchmark.bmbm do |x|
@benches.each do |name, block|
x.report name.to_s do
@iterations.to_i.times { block.call }
end
end
end
puts "
1. Uncached benchmark. Template is parsed every time.
Activate this benchmark with 'rake bench slow=1'.
2. Cached benchmark. Template is parsed before the benchmark.
The ruby code generated by the template engine might be evaluated every time.
This benchmark uses the standard API of the template engine.
3. Compiled benchmark. Template is parsed before the benchmark and
generated ruby code is compiled into a method.
This is the fastest evaluation strategy because it benchmarks
pure execution speed of the generated ruby code.
4. Compiled Tilt benchmark. Template is compiled with Tilt, which gives a more
accurate result of the performance in production mode in frameworks like
Sinatra, Ramaze and Camping. (Rails still uses its own template
compilation.)
"
end
def bench(name, &block)
@benches.push([name, block])
end
end
SlimBenchmarks.new(ENV['slow'], ENV['iterations']).run
Jump to Line
Something went wrong with that request. Please try again.