mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Rewrite benchmark with rbench
This commit is contained in:
parent
3db13fb80a
commit
294ab939ca
5 changed files with 71 additions and 4 deletions
2
Gemfile
2
Gemfile
|
@ -2,3 +2,5 @@ source 'https://rubygems.org'
|
|||
|
||||
# Specify your gem's dependencies in hamilton.gemspec
|
||||
gemspec
|
||||
|
||||
gem 'rbench', github: 'miloops/rbench'
|
||||
|
|
6
Rakefile
6
Rakefile
|
@ -1,5 +1,9 @@
|
|||
require "bundler/gem_tasks"
|
||||
|
||||
task :bench do
|
||||
system('bundle exec ruby benchmarks/benchmark.rb')
|
||||
system('TIME=20 bundle exec ruby benchmarks/benchmark.rb')
|
||||
end
|
||||
|
||||
task :rbench do
|
||||
system('NUM=200000 bundle exec ruby benchmarks/rbench.rb')
|
||||
end
|
||||
|
|
|
@ -14,8 +14,9 @@ require 'tenjin'
|
|||
require 'tilt'
|
||||
|
||||
class Benchmarks
|
||||
def initialize
|
||||
@benches = []
|
||||
def initialize(time)
|
||||
@time = time
|
||||
@benches = []
|
||||
|
||||
@erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
|
||||
@haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
|
||||
|
@ -57,6 +58,7 @@ class Benchmarks
|
|||
|
||||
def run
|
||||
Benchmark.ips do |x|
|
||||
x.config(time: @time, warmup: 2)
|
||||
@benches.each do |name, block|
|
||||
x.report(name, &block)
|
||||
end
|
||||
|
@ -69,4 +71,5 @@ class Benchmarks
|
|||
end
|
||||
end
|
||||
|
||||
Benchmarks.new.run
|
||||
time = (ENV['TIME'] || 5).to_i
|
||||
Benchmarks.new(time).run
|
||||
|
|
57
benchmarks/rbench.rb
Normal file
57
benchmarks/rbench.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
require_relative './context'
|
||||
require 'rbench'
|
||||
|
||||
require 'erb'
|
||||
require 'erubis'
|
||||
require 'fast_haml'
|
||||
require 'haml'
|
||||
require 'slim'
|
||||
require 'tenjin'
|
||||
require 'tilt'
|
||||
|
||||
iteration = (ENV['NUM'] || 100000).to_i
|
||||
RBench.run(iteration) do
|
||||
column :erubis, title: 'erubis'
|
||||
column :tenjin, title: 'tenjin'
|
||||
column :fast_haml, title: 'fast_haml'
|
||||
column :fast_erubis, title: 'fast erubis'
|
||||
column :slim, title: 'slim'
|
||||
column :temple_erb, title: 'temple erb'
|
||||
column :erb, title: 'erb'
|
||||
column :haml, title: 'haml'
|
||||
|
||||
@erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
|
||||
@haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
|
||||
@slim_code = File.read(File.dirname(__FILE__) + '/view.slim')
|
||||
@rbhtml_path = File.dirname(__FILE__) + '/view.rbhtml'
|
||||
|
||||
erb = ERB.new(@erb_code)
|
||||
erubis = Erubis::Eruby.new(@erb_code)
|
||||
fast_erubis = Erubis::FastEruby.new(@erb_code)
|
||||
haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true)
|
||||
tenjin = Tenjin::Engine.new.get_template(@rbhtml_path)
|
||||
|
||||
context = Context.new
|
||||
|
||||
haml_ugly.def_method(context, :run_haml_ugly)
|
||||
context.instance_eval %{
|
||||
def run_erb; #{erb.src}; end
|
||||
def run_erubis; #{erubis.src}; end
|
||||
def run_temple_erb; #{Temple::ERB::Engine.new.call @erb_code}; end
|
||||
def run_fast_erubis; #{fast_erubis.src}; end
|
||||
def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
|
||||
def run_fast_haml; #{FastHaml::Engine.new.call @haml_code}; end
|
||||
def run_tenjin; _buf = ''; #{tenjin.script}; end
|
||||
}
|
||||
|
||||
report 'precompiled' do
|
||||
erubis { context.run_erubis }
|
||||
tenjin { context.run_tenjin }
|
||||
fast_haml { context.run_fast_haml }
|
||||
fast_erubis { context.run_fast_erubis }
|
||||
slim { context.run_slim_ugly }
|
||||
temple_erb { context.run_temple_erb }
|
||||
erb { context.run_erb }
|
||||
haml { context.run_haml_ugly }
|
||||
end
|
||||
end
|
|
@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
|
|||
spec.add_development_dependency "erubis"
|
||||
spec.add_development_dependency "fast_haml"
|
||||
spec.add_development_dependency "haml"
|
||||
spec.add_development_dependency "pry"
|
||||
spec.add_development_dependency "rake"
|
||||
spec.add_development_dependency "slim"
|
||||
spec.add_development_dependency "tenjin"
|
||||
|
|
Loading…
Reference in a new issue