public
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/nex3/haml.git
haml / test / benchmark.rb
100755 83 lines (64 sloc) 2.524 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
 
times = (ARGV.first || 1000).to_i
 
if times == 0 # Invalid parameter
  puts <<END
ruby #$0 [times=1000]
Benchmark Haml against various other templating languages and Sass
on its own.
END
  exit 1
end
 
require File.dirname(__FILE__) + '/../lib/haml'
%w[sass rubygems erb erubis markaby active_support action_controller
action_view haml/template].each(&method(:require))
 
begin
  require 'benchwarmer'
rescue LoadError
  # Since it's not as simple as gem install at the time of writing,
  # we need to direct folks to the benchwarmer gem.
  raise "The Haml benchmarks require the benchwarmer gem, available from http://github.com/wycats/benchwarmer"
end
 
Benchmark.warmer(times) do
  columns :haml, :erb, :erubis, :mab
  titles :haml => "Haml", :erb => "ERB", :erubis => "Erubis", :mab => "Markaby"
 
  template_name = 'standard'
  directory = File.dirname(__FILE__) + '/haml'
  haml_template = File.read("#{directory}/templates/#{template_name}.haml")
  erb_template = File.read("#{directory}/rhtml/#{template_name}.rhtml")
  markaby_template = File.read("#{directory}/markaby/#{template_name}.mab")
 
  report "Uncached" do
    haml { Haml::Engine.new(haml_template).render }
    erb { ERB.new(erb_template, nil, '-').result }
    erubis { Erubis::Eruby.new(erb_template).result }
    mab { Markaby::Template.new(markaby_template).render }
  end
 
  report "Cached" do
    obj = Object.new
 
    Haml::Engine.new(haml_template).def_method(obj, :haml)
    Erubis::Eruby.new(erb_template).def_method(obj, :erubis)
    obj.instance_eval("def erb; #{ERB.new(erb_template, nil, '-').src}; end")
 
    haml { obj.haml }
    erb { obj.erb }
    erubis { obj.erubis }
  end
 
  report "ActionView" do
    @base = ActionView::Base.new(File.dirname(__FILE__))
 
    # To cache the template
    @base.render 'haml/templates/standard'
    @base.render 'haml/rhtml/standard'
 
    haml { @base.render 'haml/templates/standard' }
    erb { @base.render 'haml/rhtml/standard' }
  end
 
  report "ActionView with deep partials" do
    @base = ActionView::Base.new(File.dirname(__FILE__))
 
    # To cache the template
    @base.render 'haml/templates/action_view'
    @base.render 'haml/rhtml/action_view'
 
    haml { @base.render 'haml/templates/action_view' }
    erb { @base.render 'haml/rhtml/action_view' }
  end
end
 
Benchmark.warmer(times) do
  sass_template = File.read("#{File.dirname(__FILE__)}/sass/templates/complex.sass")
 
  report("Sass") { Sass::Engine.new(sass_template).render }
end