2006-12-03 23:56:47 +00:00
|
|
|
require File.dirname(__FILE__) + '/../lib/haml'
|
|
|
|
require 'haml/template'
|
2006-12-04 16:50:28 +00:00
|
|
|
require 'sass/engine'
|
2006-08-21 03:33:30 +00:00
|
|
|
require 'rubygems'
|
2006-10-14 23:50:07 +00:00
|
|
|
require 'active_support'
|
2006-08-21 03:33:30 +00:00
|
|
|
require 'action_view'
|
2006-10-14 23:50:07 +00:00
|
|
|
require 'benchmark'
|
|
|
|
require 'stringio'
|
2006-08-21 03:33:30 +00:00
|
|
|
|
2006-10-14 23:50:07 +00:00
|
|
|
module Haml
|
|
|
|
class Benchmarker
|
|
|
|
|
|
|
|
# Creates a new benchmarker that looks for templates in the base
|
|
|
|
# directory.
|
|
|
|
def initialize(base = File.dirname(__FILE__))
|
|
|
|
ActionView::Base.register_template_handler("haml", Haml::Template)
|
|
|
|
unless base.class == ActionView::Base
|
|
|
|
@base = ActionView::Base.new(base)
|
|
|
|
else
|
|
|
|
@base = base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-04 16:50:28 +00:00
|
|
|
# Benchmarks HAML against ERb, and Sass on its own.
|
2006-10-14 23:50:07 +00:00
|
|
|
#
|
|
|
|
# Returns the results of the benchmarking as a string.
|
|
|
|
#
|
2006-12-04 16:50:28 +00:00
|
|
|
def benchmark(runs = 100)
|
|
|
|
template_name = 'standard'
|
|
|
|
haml_template = "haml/templates/#{template_name}"
|
|
|
|
rhtml_template = "haml/rhtml/#{template_name}"
|
|
|
|
sass_template = File.dirname(__FILE__) + "/sass/templates/complex.sass"
|
2006-10-14 23:50:07 +00:00
|
|
|
|
|
|
|
old_stdout = $stdout
|
|
|
|
$stdout = StringIO.new
|
|
|
|
|
2006-11-16 04:34:00 +00:00
|
|
|
times = Benchmark.bmbm do |b|
|
2006-10-14 23:50:07 +00:00
|
|
|
b.report("haml:") { runs.times { @base.render haml_template } }
|
|
|
|
b.report("erb:") { runs.times { @base.render rhtml_template } }
|
2006-12-04 16:50:28 +00:00
|
|
|
b.report("sass:") { runs.times { Sass::Engine.new(File.read(sass_template)).render } }
|
2006-10-14 23:50:07 +00:00
|
|
|
end
|
|
|
|
|
2006-12-04 16:50:28 +00:00
|
|
|
#puts times[0].inspect, times[1].inspect
|
2006-11-16 04:34:00 +00:00
|
|
|
ratio = sprintf("%g", times[0].to_a[5] / times[1].to_a[5])
|
|
|
|
puts "Haml/ERB: " + ratio
|
|
|
|
|
2006-10-14 23:50:07 +00:00
|
|
|
$stdout.pos = 0
|
|
|
|
to_return = $stdout.read
|
|
|
|
$stdout = old_stdout
|
|
|
|
|
|
|
|
to_return
|
|
|
|
end
|
|
|
|
end
|
2006-09-12 14:50:13 +00:00
|
|
|
end
|