mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Create benchmark
This commit is contained in:
parent
9cbf796547
commit
e6487befcb
4 changed files with 79 additions and 1 deletions
2
Rakefile
2
Rakefile
|
@ -3,6 +3,8 @@ require 'rake/testtask'
|
|||
|
||||
task default: :test
|
||||
|
||||
Dir['benchmark/*.rake'].each { |b| import(b) }
|
||||
|
||||
Rake::TestTask.new do |t|
|
||||
t.libs << 'lib' << 'test'
|
||||
files = Dir['test/*_test.rb']
|
||||
|
|
71
benchmark/compile.rake
Normal file
71
benchmark/compile.rake
Normal file
|
@ -0,0 +1,71 @@
|
|||
$:.unshift File.expand_path('../lib', __FILE__)
|
||||
|
||||
require 'hamlit'
|
||||
require 'faml'
|
||||
require 'json'
|
||||
|
||||
class Benchmark
|
||||
def self.bench
|
||||
start_time = Time.now
|
||||
yield
|
||||
Time.now - start_time
|
||||
end
|
||||
|
||||
def initialize
|
||||
@total = 0.0
|
||||
@times = 0
|
||||
@max = 0.0
|
||||
end
|
||||
|
||||
def capture(result)
|
||||
@max = result if @max < result
|
||||
@total += result
|
||||
@times += 1
|
||||
end
|
||||
|
||||
def report(message)
|
||||
puts "[#{message}] Count: #{@times}, Total: #{format(@total)}, Avg: #{format(@total / @times)}, Max: #{format(@max)}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def format(seconds)
|
||||
"%.2fms" % (1000 * seconds)
|
||||
end
|
||||
end
|
||||
|
||||
namespace :benchmark do
|
||||
desc 'Benchmark compilation'
|
||||
task :compile do
|
||||
haml_benchmark = Benchmark.new
|
||||
faml_benchmark = Benchmark.new
|
||||
hamlit_benchmark = Benchmark.new
|
||||
json_path = File.expand_path('../test/haml-spec/tests.json', __dir__)
|
||||
contexts = JSON.parse(File.read(json_path))
|
||||
|
||||
contexts.each do |context|
|
||||
context[1].each do |name, test|
|
||||
haml = test['haml']
|
||||
locals = Hash[(test['locals'] || {}).map {|x, y| [x.to_sym, y]}]
|
||||
options = Hash[(test['config'] || {}).map {|x, y| [x.to_sym, y]}]
|
||||
options[:format] = options[:format].to_sym if options.key?(:format)
|
||||
options = { ugly: true }.merge(options)
|
||||
|
||||
begin
|
||||
haml_time = Benchmark.bench { Haml::Engine.new(haml, options).precompiled }
|
||||
faml_time = Benchmark.bench { Faml::Engine.new(filename: '').call(haml) }
|
||||
hamlit_time = Benchmark.bench { Hamlit::HamlEngine.new(haml, options).precompiled }
|
||||
|
||||
haml_benchmark.capture(haml_time)
|
||||
faml_benchmark.capture(faml_time)
|
||||
hamlit_benchmark.capture(hamlit_time)
|
||||
rescue Temple::FilterError, TypeError
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
haml_benchmark.report('Haml ')
|
||||
faml_benchmark.report('Faml ')
|
||||
hamlit_benchmark.report('Hamlit')
|
||||
end
|
||||
end
|
|
@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
|
|||
spec.add_dependency 'temple', '~> 0.7.6'
|
||||
|
||||
spec.add_development_dependency 'bundler', '~> 1.10'
|
||||
spec.add_development_dependency 'faml', '>= 0.3.3'
|
||||
spec.add_development_dependency 'minitest-reporters', '~> 1.1'
|
||||
spec.add_development_dependency 'rake', '~> 10.0'
|
||||
spec.add_development_dependency 'thor', '~> 0.19'
|
||||
|
|
|
@ -28,10 +28,14 @@ module Hamlit
|
|||
@options = options
|
||||
end
|
||||
|
||||
def precompiled
|
||||
Engine.new.call(@template)
|
||||
end
|
||||
|
||||
def render(scope = Object.new, locals = {}, &block)
|
||||
scope = wrap_binding(scope)
|
||||
set_locals(locals, scope)
|
||||
eval(Engine.new.call(@template), scope)
|
||||
eval(precompiled, scope)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
Loading…
Add table
Reference in a new issue