13
13
14
14
class SlimBenchmarks
15
15
def initialize ( slow )
16
- @benches = [ ]
16
+ @benches = Hash . new { | h , k | h [ k ] = [ ] }
17
17
18
18
@erb_code = File . read ( File . dirname ( __FILE__ ) + '/view.erb' )
19
19
@haml_code = File . read ( File . dirname ( __FILE__ ) + '/view.haml' )
@@ -41,14 +41,14 @@ def run_slim_pretty; #{Slim::Engine.new(pretty: true).call @slim_code}; end
41
41
def run_slim_ugly; #{ Slim ::Engine . new . call @slim_code } ; end
42
42
}
43
43
44
- bench ( '(1) erb') { context . run_erb }
45
- bench ( '(1) erubis') { context . run_erubis }
46
- bench ( '(1) fast erubis') { context . run_fast_erubis }
47
- bench ( '(1) temple erb') { context . run_temple_erb }
48
- bench ( '(1) slim pretty') { context . run_slim_pretty }
49
- bench ( '(1) slim ugly') { context . run_slim_ugly }
50
- bench ( '(1) haml pretty') { context . run_haml_pretty }
51
- bench ( '(1) haml ugly') { context . run_haml_ugly }
44
+ bench ( :compiled , ' erb') { context . run_erb }
45
+ bench ( :compiled , ' erubis') { context . run_erubis }
46
+ bench ( :compiled , ' fast erubis') { context . run_fast_erubis }
47
+ bench ( :compiled , ' temple erb') { context . run_temple_erb }
48
+ bench ( :compiled , ' slim pretty') { context . run_slim_pretty }
49
+ bench ( :compiled , ' slim ugly') { context . run_slim_ugly }
50
+ bench ( :compiled , ' haml pretty') { context . run_haml_pretty }
51
+ bench ( :compiled , ' haml ugly') { context . run_haml_ugly }
52
52
end
53
53
54
54
def init_tilt_benches
@@ -62,47 +62,54 @@ def init_tilt_benches
62
62
63
63
context = Context . new
64
64
65
- bench ( '(2) erb') { tilt_erb . render ( context ) }
66
- bench ( '(2) erubis') { tilt_erubis . render ( context ) }
67
- bench ( '(2) temple erb') { tilt_temple_erb . render ( context ) }
68
- bench ( '(2) slim pretty') { tilt_slim_pretty . render ( context ) }
69
- bench ( '(2) slim ugly') { tilt_slim_ugly . render ( context ) }
70
- bench ( '(2) haml pretty') { tilt_haml_pretty . render ( context ) }
71
- bench ( '(2) haml ugly') { tilt_haml_ugly . render ( context ) }
65
+ bench ( :tilt , ' erb') { tilt_erb . render ( context ) }
66
+ bench ( :tilt , ' erubis') { tilt_erubis . render ( context ) }
67
+ bench ( :tilt , ' temple erb') { tilt_temple_erb . render ( context ) }
68
+ bench ( :tilt , ' slim pretty') { tilt_slim_pretty . render ( context ) }
69
+ bench ( :tilt , ' slim ugly') { tilt_slim_ugly . render ( context ) }
70
+ bench ( :tilt , ' haml pretty') { tilt_haml_pretty . render ( context ) }
71
+ bench ( :tilt , ' haml ugly') { tilt_haml_ugly . render ( context ) }
72
72
end
73
73
74
74
def init_parsing_benches
75
75
context = Context . new
76
76
context_binding = context . instance_eval { binding }
77
77
78
- bench ( '(3) erb') { ERB . new ( @erb_code ) . result ( context_binding ) }
79
- bench ( '(3) erubis') { Erubis ::Eruby . new ( @erb_code ) . result ( context_binding ) }
80
- bench ( '(3) fast erubis') { Erubis ::FastEruby . new ( @erb_code ) . result ( context_binding ) }
81
- bench ( '(3) temple erb') { Temple ::ERB ::Template . new { @erb_code } . render ( context ) }
82
- bench ( '(3) slim pretty') { Slim ::Template . new ( pretty : true ) { @slim_code } . render ( context ) }
83
- bench ( '(3) slim ugly') { Slim ::Template . new { @slim_code } . render ( context ) }
84
- bench ( '(3) haml pretty') { Haml ::Engine . new ( @haml_code , format : :html5 ) . render ( context ) }
85
- bench ( '(3) haml ugly') { Haml ::Engine . new ( @haml_code , format : :html5 , ugly : true ) . render ( context ) }
78
+ bench ( :parsing , ' erb') { ERB . new ( @erb_code ) . result ( context_binding ) }
79
+ bench ( :parsing , ' erubis') { Erubis ::Eruby . new ( @erb_code ) . result ( context_binding ) }
80
+ bench ( :parsing , ' fast erubis') { Erubis ::FastEruby . new ( @erb_code ) . result ( context_binding ) }
81
+ bench ( :parsing , ' temple erb') { Temple ::ERB ::Template . new { @erb_code } . render ( context ) }
82
+ bench ( :parsing , ' slim pretty') { Slim ::Template . new ( pretty : true ) { @slim_code } . render ( context ) }
83
+ bench ( :parsing , ' slim ugly') { Slim ::Template . new { @slim_code } . render ( context ) }
84
+ bench ( :parsing , ' haml pretty') { Haml ::Engine . new ( @haml_code , format : :html5 ) . render ( context ) }
85
+ bench ( :parsing , ' haml ugly') { Haml ::Engine . new ( @haml_code , format : :html5 , ugly : true ) . render ( context ) }
86
86
end
87
87
88
88
def run
89
- Benchmark . ips do |x |
90
- @benches . each do |name , block |
91
- x . report ( name . to_s , &block )
89
+ @benches . each do |group_name , group_benches |
90
+ puts "Running #{ group_name } benchmarks:"
91
+
92
+ Benchmark . ips do |x |
93
+ group_benches . each do |name , block |
94
+ x . report ( "#{ group_name } #{ name } " , &block )
95
+ end
96
+
97
+ x . compare!
92
98
end
93
99
end
100
+
94
101
puts "
95
- (1) Compiled benchmark. Template is parsed before the benchmark and
102
+ Compiled benchmark: Template is parsed before the benchmark and
96
103
generated ruby code is compiled into a method.
97
104
This is the fastest evaluation strategy because it benchmarks
98
105
pure execution speed of the generated ruby code.
99
106
100
- (2) Compiled Tilt benchmark. Template is compiled with Tilt, which gives a more
107
+ Compiled Tilt benchmark: Template is compiled with Tilt, which gives a more
101
108
accurate result of the performance in production mode in frameworks like
102
109
Sinatra, Ramaze and Camping. (Rails still uses its own template
103
110
compilation.)
104
111
105
- (3) Parsing benchmark. Template is parsed every time.
112
+ Parsing benchmark: Template is parsed every time.
106
113
This is not the recommended way to use the template engine
107
114
and Slim is not optimized for it. Activate this benchmark with 'rake bench slow=1'.
108
115
@@ -111,8 +118,8 @@ def run
111
118
"
112
119
end
113
120
114
- def bench ( name , &block )
115
- @benches . push ( [ name , block ] )
121
+ def bench ( group , name , &block )
122
+ @benches [ group ] . push ( [ name , block ] )
116
123
end
117
124
end
118
125
0 commit comments