From bf8e64b98cf0e6ae917fceda5b405357b3619521 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Fri, 16 Oct 2015 01:13:46 +0900 Subject: [PATCH] Benchmark standard.haml compilation --- benchmark/compile.rake | 4 ++- benchmark/standard.rake | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 benchmark/standard.rake diff --git a/benchmark/compile.rake b/benchmark/compile.rake index ede759b4..cba868fa 100644 --- a/benchmark/compile.rake +++ b/benchmark/compile.rake @@ -78,7 +78,9 @@ namespace :benchmark do end end end + puts '# tests.yml compilation' Benchmark.ranking(haml_benchmark, faml_benchmark, hamlit_benchmark) + puts end end -task bench: 'benchmark:compile' +task bench: %w[benchmark:compile benchmark:standard] diff --git a/benchmark/standard.rake b/benchmark/standard.rake new file mode 100644 index 00000000..9ed5bb0c --- /dev/null +++ b/benchmark/standard.rake @@ -0,0 +1,74 @@ +$:.unshift File.expand_path('../lib', __FILE__) + +require 'hamlit' +require 'faml' +require 'yaml' + +class Benchmark + def self.bench + start_time = Time.now + yield + Time.now - start_time + end + + def self.ranking(*benchmarks) + benchmarks = benchmarks.sort_by(&:total) + + base = benchmarks.first.total + benchmarks.each do |bench| + bench.report("%.2f" % (bench.total / base)) + end + end + + attr_reader :total + + def initialize(name) + @name = name + @total = 0.0 + @times = 0 + @max = 0.0 + end + + def capture(result) + @max = result if @max < result + @total += result + @times += 1 + end + + def report(times) + puts "[#{@name}] Count: #{@times}, Total: #{format(@total)}, Avg: #{format(@total / @times)}, Max: #{format(@max)} (#{times}x)" + end + + private + + def format(seconds) + "%.2fms" % (1000 * seconds) + end +end + +namespace :benchmark do + desc 'Benchmark standard compilation' + task :standard do + haml_benchmark = Benchmark.new('haml ') + faml_benchmark = Benchmark.new('faml ') + hamlit_benchmark = Benchmark.new('hamlit') + + faml_engine = Faml::Engine.new(filename: '') + hamlit_engine = Hamlit::Engine.new + + haml = File.read(File.expand_path('../test/templates/standard.haml', __dir__)) + locals = {} + options = { ugly: true, escape_html: true } + + haml_time = Benchmark.bench { Haml::Engine.new(haml, options).precompiled } + faml_time = Benchmark.bench { faml_engine.call(haml) } + hamlit_time = Benchmark.bench { hamlit_engine.call(haml) } + + haml_benchmark.capture(haml_time) + faml_benchmark.capture(faml_time) + hamlit_benchmark.capture(hamlit_time) + puts '# standard.haml compilation' + Benchmark.ranking(haml_benchmark, faml_benchmark, hamlit_benchmark) + end +end +task standard: 'benchmark:standard'