Create bin/bench to run view benchmark directly

This commit is contained in:
Takashi Kokubun 2015-10-30 01:16:18 +09:00
parent ee64e06ef6
commit c2ecc0bad3
6 changed files with 101 additions and 189 deletions

View File

@ -3,6 +3,7 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in hamlit.gemspec
gemspec
gem 'benchmark-ips', '2.3.0'
gem 'lineprof'
gem 'minitest-line'
gem 'pry-byebug'

View File

@ -0,0 +1,5 @@
- h = { 'user' => { id: 1234, name: 'eagletmt' }, book_id: 5432 }
- c = %w[content active]
%span.book{data: h, class: c}
Book

View File

@ -1,86 +0,0 @@
$:.unshift File.expand_path('../lib', __FILE__)
require 'hamlit'
require 'faml'
require 'yaml'
class Benchmark
def self.bench
start_time = Time.now
yield
Time.now - start_time
end
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
@total = 0.0
@times = 0
@max = 0.0
end
def capture(result)
@max = result if @max < result
@total += result
@times += 1
end
def report(times)
puts "[#{@name}] Count: #{@times}, Total: #{format(@total)}, Avg: #{format(@total / @times)}, Max: #{format(@max)} (#{times}x)"
end
private
def format(seconds)
"%.2fms" % (1000 * seconds)
end
end
namespace :benchmark do
desc 'Benchmark compilation'
task :compile do
haml_benchmark = Benchmark.new('haml ')
faml_benchmark = Benchmark.new('faml ')
hamlit_benchmark = Benchmark.new('hamlit')
yaml_path = File.expand_path('../test/haml/haml-spec/tests.yml', __dir__)
contexts = YAML.load(File.read(yaml_path))
faml_engine = Faml::Engine.new(filename: '')
hamlit_engine = Hamlit::Engine.new
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.call(haml) }
hamlit_time = Benchmark.bench { hamlit_engine.call(haml) }
haml_benchmark.capture(haml_time)
faml_benchmark.capture(faml_time)
hamlit_benchmark.capture(hamlit_time)
rescue Temple::FilterError, TypeError, Hamlit::Filters::NotFound
end
end
end
puts '# tests.yml compilation'
Benchmark.ranking(haml_benchmark, faml_benchmark, hamlit_benchmark)
puts
end
end
task bench: %w[benchmark:compile benchmark:standard]

View File

@ -1,29 +0,0 @@
$:.unshift File.expand_path('../lib', __FILE__)
require 'hamlit'
require 'yaml'
desc 'Profile compilation'
task :profile do
yaml_path = File.expand_path('../test/haml/haml-spec/tests.yml', __dir__)
contexts = YAML.load(File.read(yaml_path))
hamlit_engine = Hamlit::Engine.new
Lineprof.profile(/hamlit|temple/) do
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
hamlit_engine.call(haml)
rescue Temple::FilterError, TypeError
end
end
end
end
end

View File

@ -1,74 +0,0 @@
$:.unshift File.expand_path('../lib', __FILE__)
require 'hamlit'
require 'faml'
require 'yaml'
class Benchmark
def self.bench
start_time = Time.now
yield
Time.now - start_time
end
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
@total = 0.0
@times = 0
@max = 0.0
end
def capture(result)
@max = result if @max < result
@total += result
@times += 1
end
def report(times)
puts "[#{@name}] Count: #{@times}, Total: #{format(@total)}, Avg: #{format(@total / @times)}, Max: #{format(@max)} (#{times}x)"
end
private
def format(seconds)
"%.2fms" % (1000 * seconds)
end
end
namespace :benchmark do
desc 'Benchmark standard compilation'
task :standard do
haml_benchmark = Benchmark.new('haml ')
faml_benchmark = Benchmark.new('faml ')
hamlit_benchmark = Benchmark.new('hamlit')
faml_engine = Faml::Engine.new(filename: '')
hamlit_engine = Hamlit::Engine.new
haml = File.read(File.expand_path('../test/haml/templates/standard.haml', __dir__))
locals = {}
options = { ugly: true, escape_html: true }
haml_time = Benchmark.bench { Haml::Engine.new(haml, options).precompiled }
faml_time = Benchmark.bench { faml_engine.call(haml) }
hamlit_time = Benchmark.bench { hamlit_engine.call(haml) }
haml_benchmark.capture(haml_time)
faml_benchmark.capture(faml_time)
hamlit_benchmark.capture(hamlit_time)
puts '# standard.haml compilation'
Benchmark.ranking(haml_benchmark, faml_benchmark, hamlit_benchmark)
end
end
task standard: 'benchmark:standard'

95
bin/bench Executable file
View File

@ -0,0 +1,95 @@
#!/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'
def bench(file)
haml = File.read(file)
puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}"
bench_compile(haml)
puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}"
bench_render(haml)
end
private
def bench_compile(haml)
Benchmark.ips do |x|
x.report("haml v#{Haml::VERSION}") { Haml::Engine.new(haml).precompiled }
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
def bench_render(haml)
object = Object.new
Haml::Engine.new(haml).def_method(object, :haml)
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