2012-04-27 21:08:02 -04:00
|
|
|
require "rubygems"
|
|
|
|
require "bundler/setup"
|
|
|
|
require "haml"
|
|
|
|
require "rbench"
|
|
|
|
|
2008-05-06 03:43:43 -04:00
|
|
|
times = (ARGV.first || 1000).to_i
|
|
|
|
|
|
|
|
if times == 0 # Invalid parameter
|
|
|
|
puts <<END
|
|
|
|
ruby #$0 [times=1000]
|
2010-08-22 19:04:02 -04:00
|
|
|
Benchmark Haml against various other templating languages.
|
2008-05-06 03:43:43 -04:00
|
|
|
END
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2010-10-27 03:53:56 -04:00
|
|
|
%w[rubygems erb erubis active_support action_controller
|
2008-12-28 19:54:37 -05:00
|
|
|
action_view action_pack haml/template rbench].each {|dep| require(dep)}
|
2006-08-20 23:33:30 -04:00
|
|
|
|
2008-10-27 13:17:21 -04:00
|
|
|
def view
|
|
|
|
base = ActionView::Base.new
|
|
|
|
base.finder.append_view_path(File.dirname(__FILE__))
|
|
|
|
base
|
|
|
|
end
|
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
def render(view, file)
|
|
|
|
view.render :file => file
|
|
|
|
end
|
|
|
|
|
|
|
|
RBench.run(times) do
|
|
|
|
column :haml, :title => "Haml"
|
|
|
|
column :haml_ugly, :title => "Haml :ugly"
|
|
|
|
column :erb, :title => "ERB"
|
|
|
|
column :erubis, :title => "Erubis"
|
2008-05-06 03:43:43 -04:00
|
|
|
|
|
|
|
template_name = 'standard'
|
2012-04-26 15:43:39 -04:00
|
|
|
haml_template = File.read("#{File.dirname(__FILE__)}/templates/#{template_name}.haml")
|
|
|
|
erb_template = File.read("#{File.dirname(__FILE__)}/erb/#{template_name}.erb")
|
2008-05-06 03:43:43 -04:00
|
|
|
|
|
|
|
report "Cached" do
|
|
|
|
obj = Object.new
|
|
|
|
|
|
|
|
Haml::Engine.new(haml_template).def_method(obj, :haml)
|
2008-12-28 19:54:37 -05:00
|
|
|
Haml::Engine.new(haml_template, :ugly => true).def_method(obj, :haml_ugly)
|
2008-05-06 03:43:43 -04:00
|
|
|
Erubis::Eruby.new(erb_template).def_method(obj, :erubis)
|
|
|
|
obj.instance_eval("def erb; #{ERB.new(erb_template, nil, '-').src}; end")
|
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
haml { obj.haml }
|
|
|
|
haml_ugly { obj.haml_ugly }
|
|
|
|
erb { obj.erb }
|
|
|
|
erubis { obj.erubis }
|
2008-05-06 03:43:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
report "ActionView" do
|
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
Haml::Template.options[:ugly] = false
|
2008-05-06 03:43:43 -04:00
|
|
|
# To cache the template
|
2012-04-27 21:08:02 -04:00
|
|
|
render view, 'templates/standard'
|
|
|
|
render view, 'erb/standard'
|
2008-12-28 19:54:37 -05:00
|
|
|
|
2012-04-27 21:08:02 -04:00
|
|
|
haml { render view, 'templates/standard' }
|
|
|
|
erb { render view, 'erb/standard' }
|
2008-05-06 03:43:43 -04:00
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
Haml::Template.options[:ugly] = true
|
2012-04-27 21:08:02 -04:00
|
|
|
render view, 'templates/standard_ugly'
|
|
|
|
haml_ugly { render view, 'templates/standard_ugly' }
|
2008-05-06 03:43:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
report "ActionView with deep partials" do
|
2008-12-28 19:54:37 -05:00
|
|
|
Haml::Template.options[:ugly] = false
|
2008-05-06 03:43:43 -04:00
|
|
|
# To cache the template
|
2012-04-27 21:08:02 -04:00
|
|
|
render view, 'templates/action_view'
|
|
|
|
render view, 'erb/action_view'
|
2008-12-28 19:54:37 -05:00
|
|
|
|
2012-04-27 21:08:02 -04:00
|
|
|
haml { render view, 'templates/action_view' }
|
|
|
|
erb { render view, 'erb/action_view' }
|
2008-05-06 03:43:43 -04:00
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
Haml::Template.options[:ugly] = true
|
2012-04-27 21:08:02 -04:00
|
|
|
render view, 'templates/action_view_ugly'
|
|
|
|
haml_ugly { render view, 'templates/action_view_ugly' }
|
2008-05-06 03:43:43 -04:00
|
|
|
end
|
|
|
|
end
|