From 1af11cdea2937f3edb9195759e9f706a6d880e66 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Fri, 13 Dec 2013 13:39:20 +0700 Subject: [PATCH] Initial commit --- .gitignore | 0 Gemfile | 6 +++ Gemfile.lock | 15 ++++++++ compare.rb | 70 ++++++++++++++++++++++++++++++++++ templates/erb/layout.erb | 18 +++++++++ templates/erubis/layout.erubis | 18 +++++++++ templates/haml/layout.haml | 17 +++++++++ 7 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100755 compare.rb create mode 100644 templates/erb/layout.erb create mode 100644 templates/erubis/layout.erubis create mode 100644 templates/haml/layout.haml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..e9c0320 --- /dev/null +++ b/Gemfile @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +gem 'tilt' + +gem 'erubis' +gem 'haml' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..b579fe6 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,15 @@ +GEM + remote: https://rubygems.org/ + specs: + erubis (2.7.0) + haml (4.0.4) + tilt + tilt (2.0.0) + +PLATFORMS + ruby + +DEPENDENCIES + erubis + haml + tilt diff --git a/compare.rb b/compare.rb new file mode 100755 index 0000000..8111a6b --- /dev/null +++ b/compare.rb @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby + +require 'rubygems' +require 'bundler/setup' +require 'benchmark' + +require 'erubis' +require 'haml' +require 'tilt/erb' +require 'tilt/erubis' +require 'tilt/haml' + +# Terminal settings +TERMINAL_WIDTH = 60 +BM_WIDTH = 46 + +# Benchmark settings +COMPILE_LOOPS = 1000 +RENDER_LOOPS = 10000 + +$templates = {} +$template_params = {title: 'Greetings!', username: '%username%'} + +def path_for template + File.expand_path('../templates/' + template, __FILE__) +end + +def banner title + puts '#' * TERMINAL_WIDTH + puts '##' + title.center(TERMINAL_WIDTH - 4) + '##' + puts '#' * TERMINAL_WIDTH +end + +ENGINES = { + erb: { + class: Tilt::ERBTemplate, + layout: path_for('erb/layout.erb') + }, + erubis: { + class: Tilt::ErubisTemplate, + layout: path_for('erubis/layout.erubis') + }, + haml: { + class: Tilt::HamlTemplate, + layout: path_for('haml/layout.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 +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 +end diff --git a/templates/erb/layout.erb b/templates/erb/layout.erb new file mode 100644 index 0000000..31abbbd --- /dev/null +++ b/templates/erb/layout.erb @@ -0,0 +1,18 @@ + + + + <%= title %> + + +

Hello, <%= username %>!

+

+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim + veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea + commodo consequat. Duis aute irure dolor in reprehenderit in voluptate + velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat + cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id + est laborum. +

+ + diff --git a/templates/erubis/layout.erubis b/templates/erubis/layout.erubis new file mode 100644 index 0000000..31abbbd --- /dev/null +++ b/templates/erubis/layout.erubis @@ -0,0 +1,18 @@ + + + + <%= title %> + + +

Hello, <%= username %>!

+

+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim + veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea + commodo consequat. Duis aute irure dolor in reprehenderit in voluptate + velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat + cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id + est laborum. +

+ + diff --git a/templates/haml/layout.haml b/templates/haml/layout.haml new file mode 100644 index 0000000..b4f160b --- /dev/null +++ b/templates/haml/layout.haml @@ -0,0 +1,17 @@ +!!! +%html + %head + %title= title + %body + %h1 + Hello, + = username + ! + %p + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim + veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea + commodo consequat. Duis aute irure dolor in reprehenderit in voluptate + velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint + occaecat cupidatat non proident, sunt in culpa qui officia deserunt + mollit anim id est laborum.