-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
run-benchmarks.rb
97 lines (79 loc) · 3.04 KB
/
run-benchmarks.rb
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
#!/usr/bin/env ruby
=begin
The MIT License
Copyright (c) 2010 - 2015 Slim Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=end
#
# Original: https://github.com/slim-template/slim/blob/v3.0.6/benchmarks/run-benchmarks.rb
#
# SlimBenchmarks with following modifications:
# 1. Skipping slow engines, tilt and parsing benches.
# 2. All Ruby script and attributes are escaped for fairness.
# 3. Faml and Hamlit are added.
#
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
require 'slim'
require 'context'
require 'benchmark/ips'
require 'tilt'
require 'erubi'
require 'erb'
require 'haml'
require 'hamlit'
class SlimBenchmarks
def initialize(only_haml)
@only_haml = only_haml
@benches = []
@erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
@haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
@slim_code = File.read(File.dirname(__FILE__) + '/view.slim')
init_compiled_benches
end
def init_compiled_benches
context = Context.new
if Gem::Version.new(Haml::VERSION) >= Gem::Version.new('6.0.0')
context.instance_eval %{
def run_haml; #{Haml::Engine.new.call @haml_code}; end
}
else
haml = Haml::Engine.new(@haml_code, format: :html5, escape_html: true)
haml.def_method(context, :run_haml)
end
context.instance_eval %{
def run_erubi; #{Erubi::Engine.new(@erb_code).src}; end
def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
def run_hamlit; #{Hamlit::Engine.new.call @haml_code}; end
}
bench("erubi v#{Erubi::VERSION}") { context.run_erubi } unless @only_haml
bench("slim v#{Slim::VERSION}") { context.run_slim_ugly } unless @only_haml
bench("haml v#{Haml::VERSION}") { context.run_haml }
bench("hamlit v#{Hamlit::VERSION}") { context.run_hamlit }
end
def run
Benchmark.ips do |x|
@benches.each do |name, block|
x.report(name.to_s, &block)
end
x.compare!
end
end
def bench(name, &block)
@benches.push([name, block])
end
end
SlimBenchmarks.new(ENV['ONLY_HAML'] == '1').run