mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 03:10:39 -07:00
improved benchmark suite
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
require 'rubygems'
|
||||||
|
require 'benchmark'
|
||||||
|
require File.dirname(__FILE__) + '/theme_runner'
|
||||||
|
|
||||||
|
profiler = ThemeRunner.new
|
||||||
|
|
||||||
|
Benchmark.bmbm do |x|
|
||||||
|
x.report("parse & run:") { 10.times { profiler.run(false) } }
|
||||||
|
end
|
||||||
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
require 'rubygems'
|
||||||
|
require 'ruby-prof' rescue fail("install ruby-prof extension/gem")
|
||||||
|
require File.dirname(__FILE__) + '/theme_runner'
|
||||||
|
|
||||||
|
profiler = ThemeRunner.new
|
||||||
|
|
||||||
|
puts 'Running profiler...'
|
||||||
|
|
||||||
|
results = profiler.run(true)
|
||||||
|
|
||||||
|
puts 'Success'
|
||||||
|
puts
|
||||||
|
|
||||||
|
[RubyProf::FlatPrinter, RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter, RubyProf::CallTreePrinter].each do |klass|
|
||||||
|
filename = (ENV['TMP'] || '/tmp') + (klass.name.include?('Html') ? "/liquid.#{klass.name.downcase}.html" : "/liquid.#{klass.name.downcase}.txt")
|
||||||
|
filename.gsub!(/:+/, '_')
|
||||||
|
File.open(filename, "w+") { |fp| klass.new(results).print(fp) }
|
||||||
|
$stderr.puts "wrote #{klass.name} output to #{filename}"
|
||||||
|
end
|
||||||
@@ -13,9 +13,7 @@ require 'digest/md5'
|
|||||||
require File.dirname(__FILE__) + '/shopify/liquid'
|
require File.dirname(__FILE__) + '/shopify/liquid'
|
||||||
require File.dirname(__FILE__) + '/shopify/database.rb'
|
require File.dirname(__FILE__) + '/shopify/database.rb'
|
||||||
|
|
||||||
require "ruby-prof" rescue fail("install ruby-prof extension/gem")
|
class ThemeRunner
|
||||||
|
|
||||||
class ThemeProfiler
|
|
||||||
|
|
||||||
# Load all templates into memory, do this now so that
|
# Load all templates into memory, do this now so that
|
||||||
# we don't profile IO.
|
# we don't profile IO.
|
||||||
@@ -30,8 +28,8 @@ class ThemeProfiler
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def profile
|
def run(profile = false)
|
||||||
RubyProf.measure_mode = RubyProf::WALL_TIME
|
RubyProf.measure_mode = RubyProf::WALL_TIME if profile
|
||||||
|
|
||||||
# Dup assigns because will make some changes to them
|
# Dup assigns because will make some changes to them
|
||||||
assigns = Database.tables.dup
|
assigns = Database.tables.dup
|
||||||
@@ -43,16 +41,25 @@ class ThemeProfiler
|
|||||||
page_template = File.basename(template_name, File.extname(template_name))
|
page_template = File.basename(template_name, File.extname(template_name))
|
||||||
|
|
||||||
# Profile compiling and rendering both
|
# Profile compiling and rendering both
|
||||||
RubyProf.resume { html = compile_and_render(liquid, layout, assigns, page_template) }
|
|
||||||
|
if profile
|
||||||
|
|
||||||
|
RubyProf.resume do
|
||||||
|
html = compile_and_render(liquid, layout, assigns, page_template)
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
html = compile_and_render(liquid, layout, assigns, page_template)
|
||||||
|
end
|
||||||
|
|
||||||
# return the result and the MD5 of the content, this can be used to detect regressions between liquid version
|
# return the result and the MD5 of the content, this can be used to detect regressions between liquid version
|
||||||
$stdout.puts "* rendered template %s, content: %s" % [template_name, Digest::MD5.hexdigest(html)]
|
$stdout.puts "* rendered template %s, content: %s" % [template_name, Digest::MD5.hexdigest(html)] if profile
|
||||||
|
|
||||||
# Uncomment to dump html files to /tmp so that you can inspect for errors
|
# Uncomment to dump html files to /tmp so that you can inspect for errors
|
||||||
# File.open("/tmp/#{File.basename(template_name)}.html", "w+") { |fp| fp <<html}
|
# File.open("/tmp/#{File.basename(template_name)}.html", "w+") { |fp| fp <<html}
|
||||||
end
|
end
|
||||||
|
|
||||||
RubyProf.stop
|
RubyProf.stop if profile
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_and_render(template, layout, assigns, page_template)
|
def compile_and_render(template, layout, assigns, page_template)
|
||||||
@@ -72,21 +79,4 @@ class ThemeProfiler
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
profiler = ThemeProfiler.new
|
|
||||||
|
|
||||||
puts 'Running profiler...'
|
|
||||||
|
|
||||||
results = profiler.profile
|
|
||||||
|
|
||||||
puts 'Success'
|
|
||||||
puts
|
|
||||||
|
|
||||||
[RubyProf::FlatPrinter, RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter, RubyProf::CallTreePrinter].each do |klass|
|
|
||||||
filename = (ENV['TMP'] || '/tmp') + (klass.name.include?('Html') ? "/liquid.#{klass.name.downcase}.html" : "/liquid.#{klass.name.downcase}.txt")
|
|
||||||
filename.gsub!(/:+/, '_')
|
|
||||||
File.open(filename, "w+") { |fp| klass.new(results).print(fp) }
|
|
||||||
$stderr.puts "wrote #{klass.name} output to #{filename}"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user