2015-10-29 12:16:18 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'bundler/setup'
|
|
|
|
require 'hamlit'
|
|
|
|
require 'faml'
|
2015-11-21 12:44:10 -05:00
|
|
|
require 'slim'
|
2015-10-29 12:16:18 -04:00
|
|
|
require 'thor'
|
|
|
|
require 'benchmark/ips'
|
2015-11-02 23:35:24 -05:00
|
|
|
require 'hamlit1' if File.exist?('hamlit1')
|
2015-10-29 12:16:18 -04:00
|
|
|
|
|
|
|
class Bench < Thor
|
2015-11-21 03:40:30 -05:00
|
|
|
class_option :with_hamlit1, type: :boolean, aliases: ['-w']
|
|
|
|
|
2015-10-29 12:16:18 -04:00
|
|
|
desc 'bench HAML', 'Benchmark haml template'
|
2015-11-21 03:40:30 -05:00
|
|
|
option :compile, type: :boolean, aliases: ['-c']
|
|
|
|
option :show_code, type: :boolean, aliases: ['-s']
|
2015-11-15 01:20:16 -05:00
|
|
|
def bench(*files)
|
|
|
|
files.each { |file| render(file) }
|
2015-11-21 03:40:30 -05:00
|
|
|
files.each { |file| compile(file) if options[:compile] }
|
2015-11-15 01:20:16 -05:00
|
|
|
files.each { |file| code(file) if options[:show_code] }
|
2015-10-29 12:16:18 -04:00
|
|
|
end
|
|
|
|
|
2015-11-13 09:32:09 -05:00
|
|
|
desc 'compile HAML', 'Benchmark compilation'
|
|
|
|
def compile(file)
|
|
|
|
puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}"
|
2015-10-30 23:51:44 -04:00
|
|
|
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) }
|
2015-11-21 03:40:30 -05:00
|
|
|
x.report("hamlit#{options[:with_hamlit1] ? '2' : ''} v#{Hamlit::VERSION}") { Hamlit::Engine.new.call(haml) }
|
|
|
|
x.report("hamlit1 v#{Hamlit1::VERSION}") { Hamlit1::Engine.new.call(haml) } if File.exist?('hamlit1') && options[:with_hamlit1]
|
2015-10-29 12:16:18 -04:00
|
|
|
x.compare!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-13 09:32:09 -05:00
|
|
|
desc 'render HAML', 'Benchmark rendering'
|
|
|
|
def render(file)
|
|
|
|
puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}"
|
2015-10-30 23:51:44 -04:00
|
|
|
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')
|
2015-11-21 12:44:10 -05:00
|
|
|
object.instance_eval(File.read(ruby_file)) if File.exist?(ruby_file)
|
|
|
|
slim_file = file.gsub(/\.haml\z/, '.slim')
|
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"
|
2015-11-02 23:35:24 -05:00
|
|
|
object.instance_eval "def hamlit1; #{Hamlit1::Engine.new.call(haml)}; end" if File.exist?('hamlit1')
|
2015-11-21 12:44:10 -05:00
|
|
|
object.instance_eval "def slim; #{Slim::Engine.new.call(File.read(slim_file))}; end" if File.exist?(slim_file)
|
2015-10-29 12:16:18 -04:00
|
|
|
|
|
|
|
Benchmark.ips do |x|
|
|
|
|
x.report("haml v#{Haml::VERSION}") { object.haml }
|
|
|
|
x.report("faml v#{Faml::VERSION}") { object.faml }
|
2015-11-21 03:40:30 -05:00
|
|
|
x.report("hamlit#{options[:with_hamlit1] ? '2' : ''} v#{Hamlit::VERSION}") { object.hamlit }
|
|
|
|
x.report("hamlit1 v#{Hamlit1::VERSION}") { object.hamlit1 } if File.exist?('hamlit1') && options[:with_hamlit1]
|
2015-11-21 12:44:10 -05:00
|
|
|
x.report("slim v#{Hamlit1::VERSION}") { object.slim } if File.exist?(slim_file)
|
2015-10-29 12:16:18 -04:00
|
|
|
x.compare!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-13 09:32:09 -05:00
|
|
|
desc 'code HAML', 'Show compiled code'
|
|
|
|
def code(file)
|
|
|
|
haml = File.read(file)
|
|
|
|
puts "#{?= * 49}\n Haml Source: #{file}\n#{?= * 49}"
|
|
|
|
puts Haml::Engine.new(haml, escape_html: true, escape_attrs: true, ugly: true).precompiled
|
|
|
|
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)
|
|
|
|
if File.exist?('hamlit1')
|
|
|
|
puts "\n#{?= * 49}\n Hamlit1 Source: #{file}\n#{?= * 49}"
|
|
|
|
puts Hamlit1::Engine.new.call(haml)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-10-29 12:16:18 -04:00
|
|
|
def method_missing(*args)
|
|
|
|
return super if args.length > 1
|
2015-11-13 09:32:09 -05:00
|
|
|
render(args.first.to_s)
|
2015-10-29 12:16:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-02 23:35:24 -05:00
|
|
|
# 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
|
|
|
|
|
2015-10-29 12:16:18 -04:00
|
|
|
Bench.start
|