2015-10-29 12:16:18 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'bundler/setup'
|
|
|
|
require 'hamlit'
|
|
|
|
require 'faml'
|
|
|
|
require 'thor'
|
|
|
|
require 'benchmark/ips'
|
|
|
|
|
|
|
|
# Monkey patch to show milliseconds
|
|
|
|
module Benchmark
|
|
|
|
module IPS
|
|
|
|
class Report
|
|
|
|
module EntryExtension
|
|
|
|
def body
|
|
|
|
return super if Benchmark::IPS.options[:format] != :human
|
|
|
|
|
|
|
|
left = "%s i/s (%1.3fms)" % [Helpers.scale(ips), (1000.0 / ips)]
|
|
|
|
iters = Helpers.scale(@iterations)
|
|
|
|
|
|
|
|
if @show_total_time
|
|
|
|
left.ljust(20) + (" - %s in %10.6fs" % [iters, runtime])
|
|
|
|
else
|
|
|
|
left.ljust(20) + (" - %s" % iters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Entry.send(:prepend, EntryExtension)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module CompareExtension
|
|
|
|
def compare(*reports)
|
|
|
|
return if reports.size < 2
|
|
|
|
|
|
|
|
sorted = reports.sort_by(&:ips).reverse
|
|
|
|
best = sorted.shift
|
|
|
|
$stdout.puts "\nComparison:"
|
|
|
|
$stdout.printf "%20s: %10.1f i/s (%1.3fms)\n", best.label, best.ips, (1000.0 / best.ips)
|
|
|
|
|
|
|
|
sorted.each do |report|
|
|
|
|
name = report.label.to_s
|
|
|
|
|
|
|
|
x = (best.ips.to_f / report.ips.to_f)
|
|
|
|
$stdout.printf "%20s: %10.1f i/s (%1.3fms) - %.2fx slower\n", name, report.ips, (1000.0 / report.ips), x
|
|
|
|
end
|
|
|
|
|
|
|
|
$stdout.puts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
extend CompareExtension
|
|
|
|
end
|
|
|
|
|
|
|
|
class Bench < Thor
|
|
|
|
desc 'bench HAML', 'Benchmark haml template'
|
2015-10-31 08:46:09 -04:00
|
|
|
option :show_code, type: :boolean, aliases: ['-c']
|
2015-10-29 12:16:18 -04:00
|
|
|
def bench(file)
|
|
|
|
puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}"
|
2015-10-30 23:51:44 -04:00
|
|
|
bench_compile(file)
|
2015-10-29 12:16:18 -04:00
|
|
|
puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}"
|
2015-10-30 23:51:44 -04:00
|
|
|
bench_render(file)
|
2015-10-31 08:46:09 -04:00
|
|
|
code(file) if options[:show_code]
|
2015-10-29 12:16:18 -04:00
|
|
|
end
|
|
|
|
|
2015-10-31 00:21:43 -04:00
|
|
|
desc 'code HAML', 'Show compiled code'
|
|
|
|
def code(file)
|
|
|
|
haml = File.read(file)
|
|
|
|
puts "#{?= * 49}\n Haml Source: #{file}\n#{?= * 49}"
|
2015-11-02 11:04:53 -05:00
|
|
|
puts Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled
|
2015-10-31 00:21:43 -04:00
|
|
|
puts "\n#{?= * 49}\n Faml Source: #{file}\n#{?= * 49}"
|
|
|
|
puts Faml::Engine.new.call(haml)
|
|
|
|
puts "\n#{?= * 49}\n Hamlit Source: #{file}\n#{?= * 49}"
|
|
|
|
puts Hamlit::Engine.new.call(haml)
|
|
|
|
end
|
|
|
|
|
2015-10-29 12:16:18 -04:00
|
|
|
private
|
|
|
|
|
2015-10-30 23:51:44 -04:00
|
|
|
def bench_compile(file)
|
|
|
|
haml = File.read(file)
|
|
|
|
|
2015-10-29 12:16:18 -04:00
|
|
|
Benchmark.ips do |x|
|
2015-11-02 11:04:53 -05:00
|
|
|
x.report("haml v#{Haml::VERSION}") { Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled }
|
2015-10-29 12:16:18 -04:00
|
|
|
x.report("faml v#{Faml::VERSION}") { Faml::Engine.new.call(haml) }
|
|
|
|
x.report("hamlit v#{Hamlit::VERSION}") { Hamlit::Engine.new.call(haml) }
|
|
|
|
x.compare!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-30 23:51:44 -04:00
|
|
|
def bench_render(file)
|
|
|
|
haml = File.read(file)
|
2015-10-29 12:16:18 -04:00
|
|
|
object = Object.new
|
2015-10-30 23:51:44 -04:00
|
|
|
ruby_file = file.gsub(/\.haml\z/, '.rb')
|
|
|
|
if File.exist?(ruby_file)
|
|
|
|
object.instance_eval(File.read(ruby_file))
|
|
|
|
end
|
2015-10-29 12:16:18 -04:00
|
|
|
|
2015-11-02 11:04:53 -05:00
|
|
|
Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).def_method(object, :haml)
|
2015-10-29 12:16:18 -04:00
|
|
|
object.instance_eval "def faml; #{Faml::Engine.new.call(haml)}; end"
|
|
|
|
object.instance_eval "def hamlit; #{Hamlit::Engine.new.call(haml)}; end"
|
|
|
|
|
|
|
|
Benchmark.ips do |x|
|
|
|
|
x.report("haml v#{Haml::VERSION}") { object.haml }
|
|
|
|
x.report("faml v#{Faml::VERSION}") { object.faml }
|
|
|
|
x.report("hamlit v#{Hamlit::VERSION}") { object.hamlit }
|
|
|
|
x.compare!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(*args)
|
|
|
|
return super if args.length > 1
|
|
|
|
bench(args.first.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Bench.start
|