1
0
Fork 0

Cleanup code

This commit is contained in:
Gregory Eremin 2013-12-15 00:47:11 +07:00
parent 5940fce015
commit 72089c5d4b
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
6 changed files with 117 additions and 116 deletions

View File

@ -37,55 +37,55 @@ bundle install
## Compilation (small template) - 1.000 runs ##
############################################################
user system total real
string 0.020000 0.020000 0.040000 ( 0.043484)
erubis 0.060000 0.020000 0.080000 ( 0.079481)
erb 0.250000 0.020000 0.270000 ( 0.266557)
haml 0.850000 0.030000 0.880000 ( 0.879533)
slim 1.970000 0.030000 2.000000 ( 2.004234)
mustache 0.030000 0.020000 0.050000 ( 0.047097)
liquid 0.060000 0.010000 0.070000 ( 0.072771)
markaby 0.020000 0.020000 0.040000 ( 0.037510)
builder 0.020000 0.020000 0.040000 ( 0.039034)
string 0.030000 0.020000 0.050000 ( 0.044899)
erubis 0.060000 0.030000 0.090000 ( 0.086133)
erb 0.260000 0.020000 0.280000 ( 0.278659)
haml 0.840000 0.030000 0.870000 ( 0.869582)
slim 1.950000 0.030000 1.980000 ( 1.993040)
mustache 0.020000 0.020000 0.040000 ( 0.035736)
liquid 0.060000 0.020000 0.080000 ( 0.076226)
markaby 0.030000 0.010000 0.040000 ( 0.041241)
builder 0.020000 0.020000 0.040000 ( 0.041957)
############################################################
## Render (small template) - 10.000 runs ##
############################################################
user system total real
string 0.080000 0.000000 0.080000 ( 0.085994)
erubis 0.090000 0.000000 0.090000 ( 0.094257)
erb 0.140000 0.010000 0.150000 ( 0.139963)
haml 0.220000 0.000000 0.220000 ( 0.221741)
slim 0.220000 0.000000 0.220000 ( 0.218883)
mustache 2.480000 0.030000 2.510000 ( 2.515265)
liquid 0.410000 0.010000 0.420000 ( 0.414001)
markaby 2.390000 0.010000 2.400000 ( 2.401374)
builder 1.900000 0.010000 1.910000 ( 1.904287)
string 0.080000 0.010000 0.090000 ( 0.086388)
erubis 0.130000 0.010000 0.140000 ( 0.148000)
erb 0.120000 0.010000 0.130000 ( 0.117680)
haml 0.220000 0.000000 0.220000 ( 0.223524)
slim 0.270000 0.000000 0.270000 ( 0.268160)
mustache 2.380000 0.060000 2.440000 ( 2.448563)
liquid 0.380000 0.000000 0.380000 ( 0.392713)
markaby 2.410000 0.040000 2.450000 ( 2.441700)
builder 1.840000 0.010000 1.850000 ( 1.848402)
############################################################
## Compilation (big template) - 1.000 runs ##
############################################################
user system total real
string 0.030000 0.010000 0.040000 ( 0.043037)
erubis 0.130000 0.020000 0.150000 ( 0.156926)
erb 0.320000 0.020000 0.340000 ( 0.339588)
haml 1.210000 0.030000 1.240000 ( 1.231755)
slim 2.460000 0.030000 2.490000 ( 2.491272)
mustache 0.030000 0.010000 0.040000 ( 0.047284)
liquid 0.110000 0.020000 0.130000 ( 0.124385)
markaby 0.020000 0.020000 0.040000 ( 0.037225)
builder 0.020000 0.010000 0.030000 ( 0.038738)
string 0.030000 0.010000 0.040000 ( 0.044474)
erubis 0.160000 0.030000 0.190000 ( 0.183202)
erb 0.300000 0.020000 0.320000 ( 0.321878)
haml 1.160000 0.030000 1.190000 ( 1.193374)
slim 2.380000 0.040000 2.420000 ( 2.423795)
mustache 0.020000 0.020000 0.040000 ( 0.029639)
liquid 0.110000 0.020000 0.130000 ( 0.132466)
markaby 0.020000 0.010000 0.030000 ( 0.039952)
builder 0.030000 0.020000 0.050000 ( 0.043474)
############################################################
## Render (big template, 150KB data) - 30 runs ##
## Render (big template, 150KB data) - 50 runs ##
############################################################
user system total real
string 0.150000 0.010000 0.160000 ( 0.155014)
erubis 0.130000 0.010000 0.140000 ( 0.135254)
erb 0.200000 0.000000 0.200000 ( 0.205322)
haml 0.300000 0.010000 0.310000 ( 0.310301)
slim 0.420000 0.000000 0.420000 ( 0.423397)
mustache 1.140000 0.000000 1.140000 ( 1.137800)
liquid 3.210000 0.010000 3.220000 ( 3.221065)
markaby 3.550000 0.020000 3.570000 ( 3.560586)
builder 5.620000 0.010000 5.630000 ( 5.637401)
string 0.200000 0.030000 0.230000 ( 0.220962)
erubis 0.210000 0.010000 0.220000 ( 0.225647)
erb 0.310000 0.010000 0.320000 ( 0.321146)
haml 0.460000 0.010000 0.470000 ( 0.474801)
slim 0.710000 0.010000 0.720000 ( 0.717359)
mustache 1.900000 0.010000 1.910000 ( 1.909129)
liquid 5.230000 0.030000 5.260000 ( 5.265334)
markaby 5.760000 0.010000 5.770000 ( 5.766475)
builder 9.150000 0.020000 9.170000 ( 9.168262)
```

View File

@ -1,74 +1,14 @@
#!/usr/bin/env ruby
require File.expand_path('../lib/workbench', __FILE__)
require 'rubygems'
require 'bundler/setup'
require 'benchmark'
require 'yaml'
require 'ostruct'
require 'erubis'
require 'haml'
require 'slim'
require 'liquid'
require 'mustache'
require 'tilt/erb'
require 'tilt/erubis'
require 'tilt/haml'
require 'tilt/liquid'
# Terminal settings
TERMINAL_WIDTH = 60
BM_WIDTH = 46
# Benchmark settings
COMPILE_LOOPS = 1000
RENDER_LOOPS = 1000
templates = {}
big_data_file = File.expand_path('../data/big.yml', __FILE__)
unless File.exist?(big_data_file)
raise 'Big data file not found. Please generate it with "./fakedata.rb 10000 > data/big.yml"'
end
raise <<-AGRR unless File.exist?(big_data_file)
Big data file not found. Please generate it by running this:
$ ./fakedata.rb 10000 > data/big.yml
AGRR
small_data = {window_title: 'Greetings!', username: '%username%'}
big_data = YAML.load_file(big_data_file)
def banner title
puts '#' * TERMINAL_WIDTH
puts '##' + title.center(TERMINAL_WIDTH - 4) + '##'
puts '#' * TERMINAL_WIDTH
end
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
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
ENGINES = {
Workbench::Benchmark.describe_engines(
string: {
class: Tilt::StringTemplate,
extension: 'str'
@ -105,17 +45,18 @@ ENGINES = {
class: Tilt::BuilderTemplate,
extension: 'builder'
},
}
)
banner 'Compilation (small template) - 1.000 runs'
benchmark(loops: 1000) do |name, attrs|
templates = {}
small_data = {window_title: 'Greetings!', username: '%username%'}
big_data = YAML.load_file(big_data_file)
Workbench::Benchmark.new(loops: 1000, title: 'Compilation (small template) - 1.000 runs') do |name, attrs|
template_path = File.expand_path('../templates/%s/small.%s', __FILE__) % [name, attrs[:extension]]
templates[name] = attrs[:class].new(template_path)
end
puts
banner 'Render (small template) - 10.000 runs'
benchmark(loops: 10_000) do |name, attrs|
Workbench::Benchmark.new(loops: 10000, title: 'Render (small template) - 10.000 runs') do |name, attrs|
case name
when :slim
templates[name].render(OpenStruct.new(small_data))
@ -123,17 +64,13 @@ benchmark(loops: 10_000) do |name, attrs|
templates[name].render(Object, small_data)
end
end
puts
banner 'Compilation (big template) - 1.000 runs'
benchmark(loops: 1000) do |name, attrs|
Workbench::Benchmark.new(loops: 1000, title: 'Compilation (big template) - 1.000 runs') do |name, attrs|
template_path = File.expand_path('../templates/%s/big.%s', __FILE__) % [name, attrs[:extension]]
templates[name] = attrs[:class].new(template_path)
end
puts
banner 'Render (big template, 1.5MB data) - 30 runs'
benchmark(loops: 30) do |name, attrs|
Workbench::Benchmark.new(loops: 50, title: 'Render (big template, 150KB data) - 50 runs') do |name, attrs|
case name
when :slim
templates[name].render(OpenStruct.new(customers: big_data))
@ -141,4 +78,3 @@ benchmark(loops: 30) do |name, attrs|
templates[name].render(Object, {customers: big_data})
end
end
puts

21
lib/workbench.rb Normal file
View File

@ -0,0 +1,21 @@
$:.unshift File.dirname(__FILE__)
require 'rubygems'
require 'bundler/setup'
require 'benchmark'
require 'yaml'
require 'ostruct'
require 'erubis'
require 'haml'
require 'slim'
require 'liquid'
require 'mustache'
require 'tilt/erb'
require 'tilt/erubis'
require 'tilt/haml'
require 'tilt/liquid'
require 'workbench/benchmark'
require 'workbench/wrappers/base'
require 'workbench/wrappers/mustache'

View File

@ -0,0 +1,28 @@
module Workbench
class Benchmark
class << self
def describe_engines engines
@@engines = engines
end
end
TERMINAL_WIDTH = 60
BM_WIDTH = 46
def initialize args, &block
puts '#' * TERMINAL_WIDTH
puts '##' + args[:title].center(TERMINAL_WIDTH - 4) + '##'
puts '#' * TERMINAL_WIDTH
::Benchmark.bm(TERMINAL_WIDTH - BM_WIDTH) do |benchmark|
@@engines.each do |name, attrs|
benchmark.report(name) do
args[:loops].times{ yield(name, attrs) }
end
end
end
puts
end
end
end

View File

@ -0,0 +1,9 @@
module Wrappers
class Base
attr_reader :tpl
def initialize path
@tpl = File.read(path)
end
end
end

View File

@ -0,0 +1,7 @@
module Wrappers
class Mustache < Base
def render context, args = {}
::Mustache.render(tpl, args)
end
end
end