From b41d194a636a944cd45098a226c82c16653162c0 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Sat, 14 Dec 2013 01:43:03 +0700 Subject: [PATCH] Add big templates --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 4 ++ README.md | 8 ++- benchmark.rb | 72 ++++++++++++------- fakedata.rb | 26 +++++++ templates/erb/big.erb | 25 +++++++ templates/erb/{layout.erb => small.erb} | 0 templates/erubis/big.erubis | 25 +++++++ .../erubis/{layout.erubis => small.erubis} | 0 templates/haml/big.haml | 19 +++++ templates/haml/{layout.haml => small.haml} | 0 12 files changed, 154 insertions(+), 27 deletions(-) create mode 100755 fakedata.rb create mode 100644 templates/erb/big.erb rename templates/erb/{layout.erb => small.erb} (100%) create mode 100644 templates/erubis/big.erubis rename templates/erubis/{layout.erubis => small.erubis} (100%) create mode 100644 templates/haml/big.haml rename templates/haml/{layout.haml => small.haml} (100%) diff --git a/.gitignore b/.gitignore index e69de29..efce6ad 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +/data/* diff --git a/Gemfile b/Gemfile index e9c0320..e12d85b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,6 @@ source 'https://rubygems.org' +gem 'faker' gem 'tilt' gem 'erubis' diff --git a/Gemfile.lock b/Gemfile.lock index b579fe6..d440696 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/README.md b/README.md index a762ad1..38262f8 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/benchmark.rb b/benchmark.rb index 8111a6b..62d8161 100755 --- a/benchmark.rb +++ b/benchmark.rb @@ -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 diff --git a/fakedata.rb b/fakedata.rb new file mode 100755 index 0000000..4e70b4c --- /dev/null +++ b/fakedata.rb @@ -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) diff --git a/templates/erb/big.erb b/templates/erb/big.erb new file mode 100644 index 0000000..10f3cc5 --- /dev/null +++ b/templates/erb/big.erb @@ -0,0 +1,25 @@ + + + + Customers List + + + <% customers.each do |customer| %> +
+
Name:
+
<%= customer[:name] %>
+
Age:
+
<%= customer[:age] %>
+
Address:
+
+ <%= customer[:zip] %> + <%= customer[:country] %> + <%= customer[:city] %> + <%= customer[:address] %> +
+
Employer:
+
<%= customer[:employer] %>
+
+ <% end %> + + diff --git a/templates/erb/layout.erb b/templates/erb/small.erb similarity index 100% rename from templates/erb/layout.erb rename to templates/erb/small.erb diff --git a/templates/erubis/big.erubis b/templates/erubis/big.erubis new file mode 100644 index 0000000..10f3cc5 --- /dev/null +++ b/templates/erubis/big.erubis @@ -0,0 +1,25 @@ + + + + Customers List + + + <% customers.each do |customer| %> +
+
Name:
+
<%= customer[:name] %>
+
Age:
+
<%= customer[:age] %>
+
Address:
+
+ <%= customer[:zip] %> + <%= customer[:country] %> + <%= customer[:city] %> + <%= customer[:address] %> +
+
Employer:
+
<%= customer[:employer] %>
+
+ <% end %> + + diff --git a/templates/erubis/layout.erubis b/templates/erubis/small.erubis similarity index 100% rename from templates/erubis/layout.erubis rename to templates/erubis/small.erubis diff --git a/templates/haml/big.haml b/templates/haml/big.haml new file mode 100644 index 0000000..0a661e3 --- /dev/null +++ b/templates/haml/big.haml @@ -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] diff --git a/templates/haml/layout.haml b/templates/haml/small.haml similarity index 100% rename from templates/haml/layout.haml rename to templates/haml/small.haml