2008-05-06 03:43:43 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-05-06 03:51:49 -04:00
|
|
|
require File.dirname(__FILE__) + '/../lib/haml'
|
2008-10-27 13:19:52 -04:00
|
|
|
require File.dirname(__FILE__) + '/linked_rails'
|
2010-08-22 19:04:02 -04:00
|
|
|
%w[rubygems erb erubis markaby 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
|
2009-01-22 19:29:02 -05:00
|
|
|
unless Haml::Util.has?(:instance_method, ActionView::Base, :finder)
|
2008-12-28 19:54:37 -05:00
|
|
|
return ActionView::Base.new(File.dirname(__FILE__), {})
|
2008-10-27 13:17:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Rails >=2.1.0
|
|
|
|
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'
|
|
|
|
directory = File.dirname(__FILE__) + '/haml'
|
|
|
|
haml_template = File.read("#{directory}/templates/#{template_name}.haml")
|
2009-10-05 00:17:19 -04:00
|
|
|
erb_template = File.read("#{directory}/erb/#{template_name}.erb")
|
2008-05-06 03:43:43 -04:00
|
|
|
markaby_template = File.read("#{directory}/markaby/#{template_name}.mab")
|
|
|
|
|
|
|
|
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-10-27 13:17:21 -04:00
|
|
|
@base = view
|
2008-05-06 03:43:43 -04:00
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
@base.unmemoize_all
|
|
|
|
Haml::Template.options[:ugly] = false
|
2008-05-06 03:43:43 -04:00
|
|
|
# To cache the template
|
2008-12-28 19:54:37 -05:00
|
|
|
render @base, 'haml/templates/standard'
|
2009-10-05 00:17:19 -04:00
|
|
|
render @base, 'haml/erb/standard'
|
2008-12-28 19:54:37 -05:00
|
|
|
|
|
|
|
haml { render @base, 'haml/templates/standard' }
|
2009-10-05 00:17:19 -04:00
|
|
|
erb { render @base, 'haml/erb/standard' }
|
2008-05-06 03:43:43 -04:00
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
Haml::Template.options[:ugly] = true
|
|
|
|
render @base, 'haml/templates/standard_ugly'
|
|
|
|
haml_ugly { render @base, 'haml/templates/standard_ugly' }
|
2008-05-06 03:43:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
report "ActionView with deep partials" do
|
2008-10-27 13:17:21 -04:00
|
|
|
@base = view
|
2008-05-06 03:43:43 -04:00
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
@base.unmemoize_all
|
|
|
|
Haml::Template.options[:ugly] = false
|
2008-05-06 03:43:43 -04:00
|
|
|
# To cache the template
|
2008-12-28 19:54:37 -05:00
|
|
|
render @base, 'haml/templates/action_view'
|
2009-10-05 00:17:19 -04:00
|
|
|
render @base, 'haml/erb/action_view'
|
2008-12-28 19:54:37 -05:00
|
|
|
|
|
|
|
haml { render @base, 'haml/templates/action_view' }
|
2009-10-05 00:17:19 -04:00
|
|
|
erb { render @base, 'haml/erb/action_view' }
|
2008-05-06 03:43:43 -04:00
|
|
|
|
2008-12-28 19:54:37 -05:00
|
|
|
Haml::Template.options[:ugly] = true
|
|
|
|
render @base, 'haml/templates/action_view_ugly'
|
|
|
|
haml_ugly { render @base, 'haml/templates/action_view_ugly' }
|
2008-05-06 03:43:43 -04:00
|
|
|
end
|
|
|
|
end
|