1
0
Fork 0
template_engine_comparison/benchmark.rb

137 lines
2.8 KiB
Ruby
Raw Normal View History

2013-12-13 06:39:20 +00:00
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'benchmark'
2013-12-13 18:43:03 +00:00
require 'yaml'
2013-12-13 19:40:54 +00:00
require 'ostruct'
2013-12-13 06:39:20 +00:00
require 'erubis'
require 'haml'
2013-12-13 19:40:54 +00:00
require 'slim'
require 'liquid'
2013-12-13 20:29:37 +00:00
require 'mustache'
2013-12-13 06:39:20 +00:00
require 'tilt/erb'
require 'tilt/erubis'
require 'tilt/haml'
2013-12-13 19:40:54 +00:00
require 'tilt/liquid'
2013-12-13 06:39:20 +00:00
# Terminal settings
TERMINAL_WIDTH = 60
BM_WIDTH = 46
# Benchmark settings
COMPILE_LOOPS = 1000
2013-12-13 18:43:03 +00:00
RENDER_LOOPS = 1000
2013-12-13 06:39:20 +00:00
2013-12-13 18:43:03 +00:00
templates = {}
big_data_file = File.expand_path('../data/big.yml', __FILE__)
2013-12-13 06:39:20 +00:00
2013-12-13 18:43:03 +00:00
unless File.exist?(big_data_file)
raise 'Big data file not found. Please generate it with "./fakedata.rb 10000 > data/big.yml"'
2013-12-13 06:39:20 +00:00
end
2013-12-13 18:43:03 +00:00
small_data = {title: 'Greetings!', username: '%username%'}
big_data = YAML.load_file(big_data_file)
2013-12-13 06:39:20 +00:00
def banner title
puts '#' * TERMINAL_WIDTH
puts '##' + title.center(TERMINAL_WIDTH - 4) + '##'
puts '#' * TERMINAL_WIDTH
end
2013-12-13 18:43:03 +00:00
def benchmark params
raise 'No block given' unless block_given?
Benchmark.bm(TERMINAL_WIDTH - BM_WIDTH) do |x|
ENGINES.each do |name, attrs|
x.report(name) do
params[:loops].times{ yield(name, attrs) }
end
end
end
end
2013-12-13 20:29:37 +00:00
module Wrappers
class Base
attr_reader :tpl
def initialize path
@tpl = File.read(path)
end
end
class Mustache < Base
def render context, args = {}
::Mustache.render(tpl, args)
end
end
end
2013-12-13 06:39:20 +00:00
ENGINES = {
2013-12-13 20:04:31 +00:00
string: {
class: Tilt::StringTemplate,
extension: 'str'
},
2013-12-13 06:39:20 +00:00
erubis: {
class: Tilt::ErubisTemplate,
2013-12-13 18:43:03 +00:00
extension: 'erubis'
2013-12-13 06:39:20 +00:00
},
2013-12-13 20:29:37 +00:00
erb: {
class: Tilt::ERBTemplate,
extension: 'erb'
},
2013-12-13 06:39:20 +00:00
haml: {
class: Tilt::HamlTemplate,
2013-12-13 18:43:03 +00:00
extension: 'haml'
2013-12-13 19:40:54 +00:00
},
slim: {
class: Slim::Template,
extension: 'slim'
},
2013-12-13 20:29:37 +00:00
mustache: {
class: Wrappers::Mustache,
extension: 'mustache'
},
2013-12-13 19:40:54 +00:00
liquid: {
class: Tilt::LiquidTemplate,
extension: 'liquid'
2013-12-13 20:29:37 +00:00
},
2013-12-13 06:39:20 +00:00
}
2013-12-13 18:43:03 +00:00
banner 'Compilation (small) (%d runs)' % COMPILE_LOOPS
benchmark(loops: COMPILE_LOOPS) do |name, attrs|
template_path = File.expand_path('../templates/%s/small.%s', __FILE__) % [name, attrs[:extension]]
templates[name] = attrs[:class].new(template_path)
2013-12-13 06:39:20 +00:00
end
puts
2013-12-13 18:43:03 +00:00
banner 'Render (small) (%d runs)' % RENDER_LOOPS
benchmark(loops: RENDER_LOOPS) do |name, attrs|
2013-12-13 19:40:54 +00:00
case name
when :slim
templates[name].render(OpenStruct.new(small_data))
else
templates[name].render(Object, small_data)
end
2013-12-13 18:43:03 +00:00
end
puts
banner 'Compilation (big) (%d runs)' % COMPILE_LOOPS
benchmark(loops: COMPILE_LOOPS) do |name, attrs|
template_path = File.expand_path('../templates/%s/big.%s', __FILE__) % [name, attrs[:extension]]
templates[name] = attrs[:class].new(template_path)
2013-12-13 06:39:20 +00:00
end
2013-12-13 18:43:03 +00:00
puts
2013-12-13 19:40:54 +00:00
banner 'Render (big) (%d runs)' % 100
2013-12-13 18:43:03 +00:00
benchmark(loops: 100) do |name, attrs|
2013-12-13 19:40:54 +00:00
case name
when :slim
templates[name].render(OpenStruct.new(customers: big_data))
else
templates[name].render(Object, {customers: big_data})
end
2013-12-13 18:43:03 +00:00
end
puts