From 3cf278d6265d84923f95afb5353927a4f5d8d1b1 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 31 Oct 2015 12:51:44 +0900 Subject: [PATCH] Add benchmark in slim --- benchmark/basic.haml | 18 ++++++++++++++++++ benchmark/basic.rb | 11 +++++++++++ bin/bench | 16 +++++++++++----- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 benchmark/basic.haml create mode 100644 benchmark/basic.rb diff --git a/benchmark/basic.haml b/benchmark/basic.haml new file mode 100644 index 00000000..2e37c852 --- /dev/null +++ b/benchmark/basic.haml @@ -0,0 +1,18 @@ +!!! html + +%html + %head + %title Simple Benchmark + %body + %h1= header + - unless item.empty? + %ul + - for i in item + - if i[:current] + %li + %strong= i[:name] + - else + %li + %a{:href => i[:url]}= i[:name] + - else + %p The list is empty. diff --git a/benchmark/basic.rb b/benchmark/basic.rb new file mode 100644 index 00000000..1de9f9fc --- /dev/null +++ b/benchmark/basic.rb @@ -0,0 +1,11 @@ +def header + 'Colors' +end + +def item + [ + { name: 'red', current: true, url: '#red' }, + { name: 'green', current: false, url: '#green' }, + { name: 'blue', current: false, url: '#blue' }, + ] +end diff --git a/bin/bench b/bin/bench index 794de528..ddad0512 100755 --- a/bin/bench +++ b/bin/bench @@ -53,16 +53,17 @@ end class Bench < Thor desc 'bench HAML', 'Benchmark haml template' def bench(file) - haml = File.read(file) puts "#{?= * 49}\n Compilation: #{file}\n#{?= * 49}" - bench_compile(haml) + bench_compile(file) puts "#{?= * 49}\n Rendering: #{file}\n#{?= * 49}" - bench_render(haml) + bench_render(file) end private - def bench_compile(haml) + def bench_compile(file) + haml = File.read(file) + Benchmark.ips do |x| x.report("haml v#{Haml::VERSION}") { Haml::Engine.new(haml).precompiled } x.report("faml v#{Faml::VERSION}") { Faml::Engine.new.call(haml) } @@ -71,8 +72,13 @@ class Bench < Thor end end - def bench_render(haml) + def bench_render(file) + haml = File.read(file) object = Object.new + ruby_file = file.gsub(/\.haml\z/, '.rb') + if File.exist?(ruby_file) + object.instance_eval(File.read(ruby_file)) + end Haml::Engine.new(haml).def_method(object, :haml) object.instance_eval "def faml; #{Faml::Engine.new.call(haml)}; end"