Skip to content

Commit

Permalink
Add liquid profile attributes
Browse files Browse the repository at this point in the history
Attribute testing

Add partial name support
  • Loading branch information
umair-choudhary committed Oct 3, 2019
1 parent 1aa7d3d commit fefee4c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/liquid/profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Profiler
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
Expand All @@ -65,6 +65,17 @@ def start

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
Expand Down Expand Up @@ -98,8 +109,8 @@ def self.current_profile
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]
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def with_profiling(context)
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
Expand Down
15 changes: 15 additions & 0 deletions test/integration/render_profiling_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,19 @@ def test_profiling_marks_children_of_for_blocks
# 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

0 comments on commit fefee4c

Please sign in to comment.