From fefee4c67537115b97a2735e939c21daf07cd43e Mon Sep 17 00:00:00 2001 From: uchoudh Date: Thu, 19 Sep 2019 16:04:38 -0400 Subject: [PATCH] Add liquid profile attributes Attribute testing Add partial name support --- lib/liquid/profiler.rb | 17 ++++++++++++++--- lib/liquid/template.rb | 2 +- test/integration/render_profiling_test.rb | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/liquid/profiler.rb b/lib/liquid/profiler.rb index dc3f1db..422c716 100644 --- a/lib/liquid/profiler.rb +++ b/lib/liquid/profiler.rb @@ -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 = [""] + def initialize(partial_name = "") + @partial_stack = [partial_name] @root_timing = Timing.new("", current_partial) @timing_stack = [@root_timing] diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index e23df24..8631bae 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -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 diff --git a/test/integration/render_profiling_test.rb b/test/integration/render_profiling_test.rb index 753b2be..1d9720e 100644 --- a/test/integration/render_profiling_test.rb +++ b/test/integration/render_profiling_test.rb @@ -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