haml--haml/benchmark/compile.rake

87 lines
2.3 KiB
Ruby
Raw Normal View History

2015-10-11 08:58:11 +00:00
$:.unshift File.expand_path('../lib', __FILE__)
require 'hamlit'
require 'faml'
2015-10-11 11:45:15 +00:00
require 'yaml'
2015-10-11 08:58:11 +00:00
class Benchmark
def self.bench
start_time = Time.now
yield
Time.now - start_time
end
2015-10-11 09:15:43 +00:00
def self.ranking(*benchmarks)
benchmarks = benchmarks.sort_by(&:total)
base = benchmarks.first.total
benchmarks.each do |bench|
bench.report("%.2f" % (bench.total / base))
end
end
attr_reader :total
def initialize(name)
@name = name
2015-10-11 08:58:11 +00:00
@total = 0.0
@times = 0
@max = 0.0
end
def capture(result)
@max = result if @max < result
@total += result
@times += 1
end
2015-10-11 09:15:43 +00:00
def report(times)
puts "[#{@name}] Count: #{@times}, Total: #{format(@total)}, Avg: #{format(@total / @times)}, Max: #{format(@max)} (#{times}x)"
2015-10-11 08:58:11 +00:00
end
private
def format(seconds)
"%.2fms" % (1000 * seconds)
end
end
namespace :benchmark do
desc 'Benchmark compilation'
task :compile do
2015-10-11 09:15:43 +00:00
haml_benchmark = Benchmark.new('haml ')
faml_benchmark = Benchmark.new('faml ')
hamlit_benchmark = Benchmark.new('hamlit')
2015-10-11 11:45:15 +00:00
yaml_path = File.expand_path('../test/haml-spec/tests.yml', __dir__)
contexts = YAML.load(File.read(yaml_path))
2015-10-11 08:58:11 +00:00
2015-10-11 09:31:58 +00:00
faml_engine = Faml::Engine.new(filename: '')
hamlit_engine = Hamlit::Engine.new
2015-10-11 08:58:11 +00:00
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 }
2015-10-11 09:31:58 +00:00
faml_time = Benchmark.bench { faml_engine.call(haml) }
hamlit_time = Benchmark.bench { hamlit_engine.call(haml) }
2015-10-11 08:58:11 +00:00
haml_benchmark.capture(haml_time)
faml_benchmark.capture(faml_time)
hamlit_benchmark.capture(hamlit_time)
2015-10-12 04:55:49 +00:00
rescue Temple::FilterError, TypeError, Hamlit::Filters::NotFound
2015-10-11 08:58:11 +00:00
end
end
end
2015-10-15 16:13:46 +00:00
puts '# tests.yml compilation'
2015-10-11 09:15:43 +00:00
Benchmark.ranking(haml_benchmark, faml_benchmark, hamlit_benchmark)
2015-10-15 16:13:46 +00:00
puts
2015-10-11 08:58:11 +00:00
end
end
2015-10-15 16:13:46 +00:00
task bench: %w[benchmark:compile benchmark:standard]