1
0
Fork 0

Add big templates

This commit is contained in:
Gregory Eremin 2013-12-14 01:43:03 +07:00
parent 1e8c510d47
commit b41d194a63
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
12 changed files with 154 additions and 27 deletions

1
.gitignore vendored
View File

@ -0,0 +1 @@
/data/*

View File

@ -1,5 +1,6 @@
source 'https://rubygems.org'
gem 'faker'
gem 'tilt'
gem 'erubis'

View File

@ -2,8 +2,11 @@ GEM
remote: https://rubygems.org/
specs:
erubis (2.7.0)
faker (1.2.0)
i18n (~> 0.5)
haml (4.0.4)
tilt
i18n (0.6.9)
tilt (2.0.0)
PLATFORMS
@ -11,5 +14,6 @@ PLATFORMS
DEPENDENCIES
erubis
faker
haml
tilt

View File

@ -1,10 +1,16 @@
# Template Engine Comparison
## Running
```
```bash
# Prepare workspace
git clone git@github.com:localhots/template_engine_comparison.git
cd template_engine_comparison
bundle install
# Generate data for templates
./fakedata.rb 10000 > data/big.yml
# Run benchmarks
./benchmark.rb
```

View File

@ -3,6 +3,7 @@
require 'rubygems'
require 'bundler/setup'
require 'benchmark'
require 'yaml'
require 'erubis'
require 'haml'
@ -16,55 +17,74 @@ BM_WIDTH = 46
# Benchmark settings
COMPILE_LOOPS = 1000
RENDER_LOOPS = 10000
RENDER_LOOPS = 1000
$templates = {}
$template_params = {title: 'Greetings!', username: '%username%'}
templates = {}
big_data_file = File.expand_path('../data/big.yml', __FILE__)
def path_for template
File.expand_path('../templates/' + template, __FILE__)
unless File.exist?(big_data_file)
raise 'Big data file not found. Please generate it with "./fakedata.rb 10000 > data/big.yml"'
end
small_data = {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
ENGINES = {
erb: {
class: Tilt::ERBTemplate,
layout: path_for('erb/layout.erb')
extension: 'erb'
},
erubis: {
class: Tilt::ErubisTemplate,
layout: path_for('erubis/layout.erubis')
extension: 'erubis'
},
haml: {
class: Tilt::HamlTemplate,
layout: path_for('haml/layout.haml')
extension: 'haml'
}
}
banner 'Compilation (%d runs)' % COMPILE_LOOPS
Benchmark.bm(TERMINAL_WIDTH - BM_WIDTH) do |x|
ENGINES.each do |name, attrs|
x.report(name) do
COMPILE_LOOPS.times do
$templates[name] = attrs[:class].new(attrs[:layout])
end
end
end
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)
end
puts
banner 'Render (%d runs)' % RENDER_LOOPS
Benchmark.bm(TERMINAL_WIDTH - BM_WIDTH) do |x|
ENGINES.each do |name, attrs|
x.report(name) do
RENDER_LOOPS.times do
$templates[name].render(Object, $template_params)
end
end
end
banner 'Render (small) (%d runs)' % RENDER_LOOPS
benchmark(loops: RENDER_LOOPS) do |name, attrs|
templates[name].render(Object, small_data)
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)
end
puts
banner 'Render (big) (%d runs)' % RENDER_LOOPS
benchmark(loops: 100) do |name, attrs|
templates[name].render(Object, customers: big_data)
end
puts

26
fakedata.rb Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'yaml'
require 'faker'
if ARGV.first.to_i > 0
num_records = ARGV.first.to_i
else
num_records = 10
end
data = 1.upto(num_records).map do
{
name: Faker::Name.name,
age: rand(20..50),
country: Faker::Address.country,
city: Faker::Address.city,
zip: Faker::Address.zip.to_i,
address: Faker::Address.street_address(true),
employer: Faker::Company.name
}
end
puts YAML.dump(data)

25
templates/erb/big.erb Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Customers List</title>
</head>
<body>
<% customers.each do |customer| %>
<dl>
<dt>Name:</dt>
<dd><%= customer[:name] %></dd>
<dt>Age:</dt>
<dd><%= customer[:age] %></dd>
<dt>Address:</dt>
<dd>
<%= customer[:zip] %>
<%= customer[:country] %>
<%= customer[:city] %>
<%= customer[:address] %>
</dd>
<dt>Employer:</dt>
<dd><%= customer[:employer] %></dd>
</dl>
<% end %>
</body>
</html>

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Customers List</title>
</head>
<body>
<% customers.each do |customer| %>
<dl>
<dt>Name:</dt>
<dd><%= customer[:name] %></dd>
<dt>Age:</dt>
<dd><%= customer[:age] %></dd>
<dt>Address:</dt>
<dd>
<%= customer[:zip] %>
<%= customer[:country] %>
<%= customer[:city] %>
<%= customer[:address] %>
</dd>
<dt>Employer:</dt>
<dd><%= customer[:employer] %></dd>
</dl>
<% end %>
</body>
</html>

19
templates/haml/big.haml Normal file
View File

@ -0,0 +1,19 @@
!!!
%html
%head
%title Customers List
%body
- customers.each do |customer|
%dl
%dt Name:
%dd= customer[:name]
%dt Age:
%dd= customer[:age]
%dt Address:
%dd
= customer[:zip]
= customer[:country]
= customer[:city]
= customer[:address]
%dt Employer:
%dd= customer[:employer]