From e6487befcb77e61f773b634126076c339854a5a0 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 11 Oct 2015 17:58:11 +0900 Subject: [PATCH] Create benchmark --- Rakefile | 2 ++ benchmark/compile.rake | 71 ++++++++++++++++++++++++++++++++++++++++++ hamlit.gemspec | 1 + lib/hamlit/engine.rb | 6 +++- 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 benchmark/compile.rake diff --git a/Rakefile b/Rakefile index 9f4ea0cd..42fd44a4 100644 --- a/Rakefile +++ b/Rakefile @@ -3,6 +3,8 @@ require 'rake/testtask' task default: :test +Dir['benchmark/*.rake'].each { |b| import(b) } + Rake::TestTask.new do |t| t.libs << 'lib' << 'test' files = Dir['test/*_test.rb'] diff --git a/benchmark/compile.rake b/benchmark/compile.rake new file mode 100644 index 00000000..d5151a93 --- /dev/null +++ b/benchmark/compile.rake @@ -0,0 +1,71 @@ +$:.unshift File.expand_path('../lib', __FILE__) + +require 'hamlit' +require 'faml' +require 'json' + +class Benchmark + def self.bench + start_time = Time.now + yield + Time.now - start_time + end + + def initialize + @total = 0.0 + @times = 0 + @max = 0.0 + end + + def capture(result) + @max = result if @max < result + @total += result + @times += 1 + end + + def report(message) + puts "[#{message}] Count: #{@times}, Total: #{format(@total)}, Avg: #{format(@total / @times)}, Max: #{format(@max)}" + end + + private + + def format(seconds) + "%.2fms" % (1000 * seconds) + end +end + +namespace :benchmark do + desc 'Benchmark compilation' + task :compile do + haml_benchmark = Benchmark.new + faml_benchmark = Benchmark.new + hamlit_benchmark = Benchmark.new + json_path = File.expand_path('../test/haml-spec/tests.json', __dir__) + contexts = JSON.parse(File.read(json_path)) + + contexts.each do |context| + context[1].each do |name, test| + haml = test['haml'] + locals = Hash[(test['locals'] || {}).map {|x, y| [x.to_sym, y]}] + options = Hash[(test['config'] || {}).map {|x, y| [x.to_sym, y]}] + options[:format] = options[:format].to_sym if options.key?(:format) + options = { ugly: true }.merge(options) + + begin + haml_time = Benchmark.bench { Haml::Engine.new(haml, options).precompiled } + faml_time = Benchmark.bench { Faml::Engine.new(filename: '').call(haml) } + hamlit_time = Benchmark.bench { Hamlit::HamlEngine.new(haml, options).precompiled } + + haml_benchmark.capture(haml_time) + faml_benchmark.capture(faml_time) + hamlit_benchmark.capture(hamlit_time) + rescue Temple::FilterError, TypeError + end + end + end + + haml_benchmark.report('Haml ') + faml_benchmark.report('Faml ') + hamlit_benchmark.report('Hamlit') + end +end diff --git a/hamlit.gemspec b/hamlit.gemspec index 33c3c8cc..d4ebee7a 100644 --- a/hamlit.gemspec +++ b/hamlit.gemspec @@ -23,6 +23,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'temple', '~> 0.7.6' spec.add_development_dependency 'bundler', '~> 1.10' + spec.add_development_dependency 'faml', '>= 0.3.3' spec.add_development_dependency 'minitest-reporters', '~> 1.1' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'thor', '~> 0.19' diff --git a/lib/hamlit/engine.rb b/lib/hamlit/engine.rb index 7ef4c3fd..0c75c1ce 100644 --- a/lib/hamlit/engine.rb +++ b/lib/hamlit/engine.rb @@ -28,10 +28,14 @@ module Hamlit @options = options end + def precompiled + Engine.new.call(@template) + end + def render(scope = Object.new, locals = {}, &block) scope = wrap_binding(scope) set_locals(locals, scope) - eval(Engine.new.call(@template), scope) + eval(precompiled, scope) end private