Compare commits

..

1 Commits

Author SHA1 Message Date
Dylan Thacker-Smith
d7234cb346 Benchmark marshal load and render against parse and render. 2014-09-12 09:46:30 -04:00
5 changed files with 60 additions and 96 deletions

View File

@@ -30,11 +30,6 @@ module Liquid
@attributes[key] = value
end
if partial_parsable_at_parse_time?
source = read_template_from_file_system_at_parse
@partial = Liquid::Template.parse(source, pass_options)
end
else
raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
end
@@ -71,21 +66,17 @@ module Liquid
template_name = context[@template_name]
if cached = cached_partials[template_name]
cached
else
if @partial && context.registers[:file_system].nil?
partial = @partial
else
partial = Liquid::Template.parse(read_template_from_file_system(context), pass_options)
end
cached_partials[template_name] = partial
context.registers[:cached_partials] = cached_partials
partial
return cached
end
source = read_template_from_file_system(context)
partial = Liquid::Template.parse(source, pass_options)
cached_partials[template_name] = partial
context.registers[:cached_partials] = cached_partials
partial
end
def read_template_from_file_system(context)
file_system = context.registers[:file_system] || parsed_file_system || Liquid::Template.file_system
file_system = context.registers[:file_system] || Liquid::Template.file_system
# make read_template_file call backwards-compatible.
case file_system.method(:read_template_file).arity
@@ -98,23 +89,6 @@ module Liquid
end
end
def read_template_from_file_system_at_parse
parsed_file_system.read_template_file(parsed_template_name)
end
def parsed_file_system
options[:file_system]
end
def partial_parsable_at_parse_time?
template_name_is_string_constant = parsed_template_name.is_a?(String)
options[:file_system] && template_name_is_string_constant
end
def parsed_template_name
Expression.parse(@template_name)
end
def pass_options
dont_pass = @options[:include_options_blacklist]
return {locale: @options[:locale]} if dont_pass == true

View File

@@ -1,17 +1,14 @@
require 'benchmark/ips'
require 'benchmark'
require File.dirname(__FILE__) + '/theme_runner'
Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
profiler = ThemeRunner.new
Benchmark.ips do |x|
x.time = 60
x.warmup = 5
puts
puts "Running benchmark for #{x.time} seconds (with #{x.warmup} seconds warmup)."
puts
x.report("parse:") { profiler.compile }
x.report("parse & run:") { profiler.run }
N = 100
Benchmark.bmbm do |x|
x.report("parse:") { N.times { profiler.parse } }
x.report("marshal load:") { N.times { profiler.marshal_load } }
x.report("render:") { N.times { profiler.render } }
x.report("marshal load & render:") { N.times { profiler.load_and_render } }
x.report("parse & render:") { N.times { profiler.parse_and_render } }
end

View File

@@ -3,13 +3,13 @@ require File.dirname(__FILE__) + '/theme_runner'
Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first
profiler = ThemeRunner.new
profiler.run
profiler.parse_and_render
[:cpu, :object].each do |profile_type|
puts "Profiling in #{profile_type.to_s} mode..."
results = StackProf.run(mode: profile_type) do
100.times do
profiler.run
profiler.parse_and_render
end
end
StackProf::Report.new(results).print_text(false, 20)

View File

@@ -32,45 +32,59 @@ class ThemeRunner
[File.read(test), (File.file?(theme_path) ? File.read(theme_path) : nil), test]
end.compact
end
def compile
# Dup assigns because will make some changes to them
@tests.each do |liquid, layout, template_name|
tmpl = Liquid::Template.new
tmpl.parse(liquid)
tmpl = Liquid::Template.new
tmpl.parse(layout)
@parsed = @tests.map do |liquid, layout, template_name|
[Liquid::Template.parse(liquid), Liquid::Template.parse(layout), template_name]
end
@marshaled = @parsed.map do |liquid, layout, template_name|
[Marshal.dump(liquid), Marshal.dump(layout), template_name]
end
end
def run
def parse
@tests.each do |liquid, layout, template_name|
Liquid::Template.parse(liquid)
Liquid::Template.parse(layout)
end
end
def marshal_load
@marshaled.each do |liquid, layout, template_name|
Marshal.load(liquid)
Marshal.load(layout)
end
end
def render
@parsed.each do |liquid, layout, template_name|
render_once(liquid, layout, template_name)
end
end
def load_and_render
@marshaled.each do |liquid, layout, template_name|
render_once(Marshal.load(liquid), Marshal.load(layout), template_name)
end
end
def parse_and_render
@tests.each do |liquid, layout, template_name|
render_once(Liquid::Template.parse(liquid), Liquid::Template.parse(layout), template_name)
end
end
def render_once(template, layout, template_name)
# Dup assigns because will make some changes to them
assigns = Database.tables.dup
@tests.each do |liquid, layout, template_name|
assigns['page_title'] = 'Page title'
assigns['template'] = File.basename(template_name, File.extname(template_name))
template.registers[:file_system] = ThemeRunner::FileSystem.new(File.dirname(template_name))
# Compute page_tempalte outside of profiler run, uninteresting to profiler
page_template = File.basename(template_name, File.extname(template_name))
compile_and_render(liquid, layout, assigns, page_template, template_name)
end
end
def compile_and_render(template, layout, assigns, page_template, template_file)
tmpl = Liquid::Template.new
tmpl.assigns['page_title'] = 'Page title'
tmpl.assigns['template'] = page_template
tmpl.registers[:file_system] = ThemeRunner::FileSystem.new(File.dirname(template_file))
content_for_layout = tmpl.parse(template).render!(assigns)
content_for_layout = template.render!(assigns)
if layout
assigns['content_for_layout'] = content_for_layout
tmpl.parse(layout).render!(assigns)
layout.render!(assigns)
else
content_for_layout
end

View File

@@ -33,13 +33,6 @@ class TestFileSystem
end
end
# FileSystems at parse time don't have a context
class ParseFileSystem
def read_template_file(template_path)
'from ParseFileSystem'
end
end
class OtherFileSystem
def read_template_file(template_path, context)
'from OtherFileSystem'
@@ -84,20 +77,6 @@ class IncludeTagTest < Minitest::Test
Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => OtherFileSystem.new})
end
def test_include_tag_can_use_file_system_at_parse_so_it_can_be_parsed_before_rendered
assert_equal 'from ParseFileSystem',
Template.parse("{% include 'pick_a_source' %}",file_system: ParseFileSystem.new).render!({})
end
def test_include_tag_looks_at_file_system_passed_in_registers_over_parse_options
assert_equal 'from OtherFileSystem',
Template.parse("{% include 'pick_a_source' %}",file_system: ParseFileSystem.new).render!({}, :registers => {:file_system => OtherFileSystem.new})
end
def test_include_tag_use_parse_option_file_system_even_if_partial_can_be_preparsed
assert_equal 'from ParseFileSystem',
Template.parse("{% include template %}",file_system: ParseFileSystem.new).render!({})
end
def test_include_tag_with
assert_template_result "Product: Draft 151cm ",