lib/liquid/profiler.rb in liquid-3.0.6 vs lib/liquid/profiler.rb in liquid-4.0.0.rc1
- old
+ new
@@ -1,11 +1,13 @@
-module Liquid
+require 'liquid/profiler/hooks'
+module Liquid
# Profiler enables support for profiling template rendering to help track down performance issues.
#
- # To enable profiling, pass the <tt>profile: true</tt> option to <tt>Liquid::Template.parse</tt>. Then, after
- # <tt>Liquid::Template#render</tt> is called, the template object makes available an instance of this
+ # To enable profiling, first require 'liquid/profiler'.
+ # Then, to profile a parse/render cycle, pass the <tt>profile: true</tt> option to <tt>Liquid::Template.parse</tt>.
+ # After <tt>Liquid::Template#render</tt> is called, the template object makes available an instance of this
# class via the <tt>Liquid::Template#profiler</tt> method.
#
# template = Liquid::Template.parse(template_content, profile: true)
# output = template.render
# profile = template.profiler
@@ -15,11 +17,11 @@
#
# This is a tree structure that is Enumerable all the way down, and keeps track of tags and rendering times
# inside of <tt>{% include %}</tt> tags.
#
# profile.each do |node|
- # # Access to the token itself
+ # # Access to the node itself
# node.code
#
# # Which template and line number of this node.
# # If top level, this will be "<root>".
# node.partial
@@ -42,21 +44,19 @@
include Enumerable
class Timing
attr_reader :code, :partial, :line_number, :children
- def initialize(token, partial)
- @code = token.respond_to?(:raw) ? token.raw : token
+ def initialize(node, partial)
+ @code = node.respond_to?(:raw) ? node.raw : node
@partial = partial
- @line_number = token.respond_to?(:line_number) ? token.line_number : nil
+ @line_number = node.respond_to?(:line_number) ? node.line_number : nil
@children = []
end
- def self.start(token, partial)
- new(token, partial).tap do |t|
- t.start
- end
+ def self.start(node, partial)
+ new(node, partial).tap(&:start)
end
def start
@start_time = Time.now
end
@@ -68,15 +68,15 @@
def render_time
@end_time - @start_time
end
end
- def self.profile_token_render(token)
- if Profiler.current_profile && token.respond_to?(:render)
- Profiler.current_profile.start_token(token)
+ def self.profile_node_render(node)
+ if Profiler.current_profile && node.respond_to?(:render)
+ Profiler.current_profile.start_node(node)
output = yield
- Profiler.current_profile.end_token(token)
+ Profiler.current_profile.end_node(node)
output
else
yield
end
end
@@ -130,15 +130,15 @@
def length
@root_timing.children.length
end
- def start_token(token)
- @timing_stack.push(Timing.start(token, current_partial))
+ def start_node(node)
+ @timing_stack.push(Timing.start(node, current_partial))
end
- def end_token(token)
+ def end_node(_node)
timing = @timing_stack.pop
timing.finish
@timing_stack.last.children << timing
end
@@ -152,8 +152,7 @@
end
def pop_partial
@partial_stack.pop
end
-
end
end