public
Description: A port of Ruby to HTML::Template::Pro.
Homepage:
Clone URL: git://github.com/tom-lpsd/ruby-html-template-pro.git
tom-lpsd (author)
Fri Oct 02 08:37:52 -0700 2009
commit  29f4834168c1d1debb32d6f064bebf33ddeea7e1
tree    9b8c3b3dcb81f7c95214d0866a2bdf767c17972a
parent  e140136dffe75225f0f002dd383108bd138dae11
ruby-html-template-pro / benchmark.rb
100644 137 lines (119 sloc) 3.78 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'benchmark'
require 'erb'
require 'erubis'
require 'html/template/pro'
 
varset1 = { :VAR1 => 'VAR1', :VAR2 => 'VAR2', :VAR3 => 'VAR3', :VAR10 => 'VAR10' }
varset2 = { :STUFF1 => '<>"; %FA' }
 
refset1 = {
  :HASHREF0=>[],
  :HASHREF2=>[{},{}],
  :HASHREF1=>[
              {:LOOPVAR1=>'LOOP1-VAR1',:LOOPVAR2=>'LOOP1-VAR2',:LOOPVAR3=>'LOOP1-VAR3',:LOOPVAR10=>'LOOP1-VAR10'},
              {:LOOPVAR1=>'LOOP2-VAR1',:LOOPVAR2=>'LOOP2-VAR2',:LOOPVAR3=>'LOOP2-VAR3',:LOOPVAR10=>'LOOP2-VAR10'},
              {:LOOPVAR1=>'LOOP3-VAR1',:LOOPVAR2=>'LOOP3-VAR2',:LOOPVAR3=>'LOOP3-VAR3',:LOOPVAR10=>'LOOP3-VAR10'},
              {:LOOPVAR1=>'LOOP4-VAR1',:LOOPVAR2=>'LOOP4-VAR2',:LOOPVAR3=>'LOOP4-VAR3',:LOOPVAR10=>'LOOP4-VAR10'},
             ]
}
 
# -------------------------
 
def test_tmpl(file, *args)
  params = args.inject({}) do |result, item|
    result.update(item)
  end
  testname = file
  test_tmpl_complete(testname, params)
  test_tmpl_output(testname, params)
end
 
def test_tmpl_output(testname, params)
  Dir.chdir 'templates-Pro'
  file = testname
  count = 1000
 
  tmplo = HTML::Template::Pro.new(:filename => file + '.tmpl',
                                  :die_on_bad_params => false,
                                  :strict => false,
                                  :case_sensitive => false,
                                  :loop_context_vars => true
                                  )
  tmplo.param(params);
 
  File.open('/dev/null', 'w') do |file|
    puts "Template::Pro " + '-' * 40
    puts Benchmark::CAPTION
    puts Benchmark.measure{
      count.times do
        tmplo.output(:print_to => file)
      end
    }
  end
 
  erb = ERB.new(File.read(file + '.erb'))
  File.open('/dev/null', 'w') do |file|
    puts "ERB " + '-' * 40
    puts Benchmark::CAPTION
    puts Benchmark.measure{
      count.times do
        file.puts erb.result(binding)
      end
    }
  end
 
  erubis = Erubis::FastEruby.new(File.read(file + '.erb'))
  File.open('/dev/null', 'w') do |file|
    puts "Erubis " + '-' * 40
    puts Benchmark::CAPTION
    puts Benchmark.measure{
      count.times do
        file.puts erubis.result(binding)
      end
    }
  end
 
  Dir.chdir '..'
end
 
def test_tmpl_complete(testname, params)
  Dir.chdir 'templates-Pro';
  file = testname
  count = 1000
 
  File.open('/dev/null', 'w') do |file|
    puts "Template::Pro " + '-' * 40
    puts Benchmark::CAPTION
    puts Benchmark.measure{
      count.times do
tmpl = HTML::Template::Pro.new(:filename => testname + '.tmpl',
                                       :loop_context_vars => true,
                                       :case_sensitive => false,
                                       :die_on_bad_params => false)
tmpl.param(params)
tmpl.output(:print_to => file)
      end
    }
  end
 
  File.open('/dev/null', 'w') do |file|
    puts "ERB " + '-' * 40
    puts Benchmark::CAPTION
    puts Benchmark.measure{
      count.times do
        erb = ERB.new(File.read(testname + '.erb'))
        file.puts erb.result(binding)
      end
    }
  end
 
  File.open('/dev/null', 'w') do |file|
    puts "Erubis " + '-' * 40
    puts Benchmark::CAPTION
    puts Benchmark.measure{
      count.times do
        erubis = Erubis::FastEruby.new(File.read(testname + '.erb'))
        file.puts erubis.result(binding)
      end
    }
  end
 
  Dir.chdir '..'
end
 
test_tmpl('test_var1', varset1);
test_tmpl('test_var2', varset1);
test_tmpl('test_loop1', varset1, refset1);
test_tmpl('test_loop2', varset1, refset1);
 
__END__
test_tmpl('test_var3', varset1, varset2);
test_tmpl('test_if1', varset1);
test_tmpl('test_if2', varset1);
test_tmpl('test_if3', refset1);
test_tmpl('test_loop3', varset1, refset1);
test_tmpl('test_loop4', varset1, refset1);
test_tmpl('test_loop5', varset1, refset1);