Fix template name in profile result for render tag timing objects

This commit is contained in:
Dylan Thacker-Smith
2020-12-09 10:01:16 -05:00
parent f18084203d
commit 900e3a6491
3 changed files with 25 additions and 48 deletions
+13 -38
View File
@@ -25,7 +25,7 @@ module Liquid
# node.code # node.code
# #
# # Which template and line number of this node. # # Which template and line number of this node.
# # If top level, this will be "<root>". # # The top-level template name is `nil` by default, but can be set in the Liquid::Context before rendering.
# node.partial # node.partial
# node.line_number # node.line_number
# #
@@ -49,15 +49,15 @@ module Liquid
attr_reader :code, :partial, :line_number, :children, :total_time, :self_time attr_reader :code, :partial, :line_number, :children, :total_time, :self_time
alias_method :render_time, :total_time alias_method :render_time, :total_time
def initialize(node, partial) def initialize(node, template_name)
@code = node.respond_to?(:raw) ? node.raw : node @code = node.respond_to?(:raw) ? node.raw : node
@partial = partial @partial = template_name
@line_number = node.respond_to?(:line_number) ? node.line_number : nil @line_number = node.respond_to?(:line_number) ? node.line_number : nil
@children = [] @children = []
end end
def self.start(node, partial) def self.start(node, template_name)
new(node, partial).tap(&:start) new(node, template_name).tap(&:start)
end end
def start def start
@@ -85,22 +85,11 @@ module Liquid
end end
end end
def self.profile_node_render(node) def self.profile_node_render(node, template_name)
if Profiler.current_profile && node.respond_to?(:render) if Profiler.current_profile && node.respond_to?(:render)
Profiler.current_profile.start_node(node) Profiler.current_profile.start_node(node, template_name)
output = yield output = yield
Profiler.current_profile.end_node(node) Profiler.current_profile.end_node
output
else
yield
end
end
def self.profile_children(template_name)
if Profiler.current_profile
Profiler.current_profile.push_partial(template_name)
output = yield
Profiler.current_profile.pop_partial
output output
else else
yield yield
@@ -113,10 +102,8 @@ module Liquid
attr_reader :total_render_time attr_reader :total_render_time
def initialize(partial_name = "<root>") def initialize(template_name)
@partial_stack = [partial_name] @root_timing = Timing.new("", template_name)
@root_timing = Timing.new("", current_partial)
@timing_stack = [@root_timing] @timing_stack = [@root_timing]
end end
@@ -142,29 +129,17 @@ module Liquid
@root_timing.children.length @root_timing.children.length
end end
def start_node(node) def start_node(node, template_name)
@timing_stack.push(Timing.start(node, current_partial)) @timing_stack.push(Timing.start(node, template_name))
end end
def end_node(_node) def end_node
timing = @timing_stack.pop timing = @timing_stack.pop
timing.finish timing.finish
@timing_stack.last.children << timing @timing_stack.last.children << timing
end end
def current_partial
@partial_stack.last
end
def push_partial(partial_name)
@partial_stack.push(partial_name)
end
def pop_partial
@partial_stack.pop
end
private private
def monotonic_time def monotonic_time
+1 -10
View File
@@ -3,19 +3,10 @@
module Liquid module Liquid
module BlockBodyProfilingHook module BlockBodyProfilingHook
def render_node(context, output, node) def render_node(context, output, node)
Profiler.profile_node_render(node) do Profiler.profile_node_render(node, context.template_name) do
super super
end end
end end
end end
BlockBody.prepend(BlockBodyProfilingHook) BlockBody.prepend(BlockBodyProfilingHook)
module IncludeProfilingHook
def render_to_output_buffer(context, output)
Profiler.profile_children(context.evaluate(@template_name_expr).to_s) do
super
end
end
end
Include.prepend(IncludeProfilingHook)
end end
+11
View File
@@ -62,6 +62,17 @@ class RenderProfilingTest < Minitest::Test
assert_equal(2, included_children[1].line_number) assert_equal(2, included_children[1].line_number)
end end
def test_profiling_render_tag
t = Template.parse("{% render 'a_template' %}", profile: true)
t.render!
render_children = t.profiler[0].children
render_children.each do |timing|
assert_equal('a_template', timing.partial)
end
assert_equal([1, 2], render_children.map(&:line_number))
end
def test_profiling_times_the_rendering_of_tokens def test_profiling_times_the_rendering_of_tokens
t = Template.parse("{% include 'a_template' %}", profile: true) t = Template.parse("{% include 'a_template' %}", profile: true)
t.render! t.render!