Add liquid profile attributes

Attribute testing

Add partial name support
This commit is contained in:
uchoudh
2019-09-19 16:04:38 -04:00
parent 1aa7d3d2ba
commit fefee4c675
3 changed files with 30 additions and 4 deletions

View File

@@ -46,7 +46,7 @@ module Liquid
include Enumerable
class Timing
attr_reader :code, :partial, :line_number, :children
attr_reader :code, :partial, :line_number, :children, :total_time, :self_time
def initialize(node, partial)
@code = node.respond_to?(:raw) ? node.raw : node
@@ -65,6 +65,17 @@ module Liquid
def finish
@end_time = Time.now
@total_time = @end_time - @start_time
if @children.empty?
@self_time = @total_time
else
total_children_time = 0
@children.each do |child|
total_children_time += child.total_time
end
@self_time = @total_time - total_children_time
end
end
def render_time
@@ -98,8 +109,8 @@ module Liquid
Thread.current[:liquid_profiler]
end
def initialize
@partial_stack = ["<root>"]
def initialize(partial_name = "<root>")
@partial_stack = [partial_name]
@root_timing = Timing.new("", current_partial)
@timing_stack = [@root_timing]

View File

@@ -254,7 +254,7 @@ module Liquid
if @profiling && !context.partial
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
@profiler = Profiler.new
@profiler = Profiler.new(context.template_name)
@profiler.start
begin

View File

@@ -153,4 +153,19 @@ class RenderProfilingTest < Minitest::Test
# Will profile each invocation of the for block
assert_equal 2, t.profiler[0].children.length
end
def test_total_time_equals_self_time_in_leaf
t = Template.parse("{% for item in collection %} {{ item }} {% endfor %}", profile: true)
t.render!("collection" => ["one", "two"])
leaf = t.profiler[0].children[0]
assert_equal leaf.total_time, leaf.self_time
end
def test_total_time_greater_than_self_time_in_node
t = Template.parse("{% if true %} {% increment test %} {{ test }} {% endif %}", profile: true)
t.render!
assert_operator t.profiler[0].total_time, :>, t.profiler[0].self_time
end
end