Use monotonic time to measure durations in Liquid::Profiler (#1362)

This commit is contained in:
Dylan Thacker-Smith
2020-12-09 10:00:44 -05:00
committed by GitHub
parent 3358a892f2
commit f18084203d
+17 -14
View File
@@ -47,6 +47,7 @@ module Liquid
class Timing class Timing
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
def initialize(node, partial) def initialize(node, partial)
@code = node.respond_to?(:raw) ? node.raw : node @code = node.respond_to?(:raw) ? node.raw : node
@@ -60,12 +61,11 @@ module Liquid
end end
def start def start
@start_time = Time.now @start_time = monotonic_time
end end
def finish def finish
@end_time = Time.now @total_time = monotonic_time - @start_time
@total_time = @end_time - @start_time
if @children.empty? if @children.empty?
@self_time = @total_time @self_time = @total_time
@@ -78,8 +78,10 @@ module Liquid
end end
end end
def render_time private
@end_time - @start_time
def monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end end
end end
@@ -109,28 +111,23 @@ module Liquid
Thread.current[:liquid_profiler] Thread.current[:liquid_profiler]
end end
attr_reader :total_render_time
def initialize(partial_name = "<root>") def initialize(partial_name = "<root>")
@partial_stack = [partial_name] @partial_stack = [partial_name]
@root_timing = Timing.new("", current_partial) @root_timing = Timing.new("", current_partial)
@timing_stack = [@root_timing] @timing_stack = [@root_timing]
@render_start_at = Time.now
@render_end_at = @render_start_at
end end
def start def start
Thread.current[:liquid_profiler] = self Thread.current[:liquid_profiler] = self
@render_start_at = Time.now @render_start_at = monotonic_time
end end
def stop def stop
Thread.current[:liquid_profiler] = nil Thread.current[:liquid_profiler] = nil
@render_end_at = Time.now @total_render_time = monotonic_time - @render_start_at
end
def total_render_time
@render_end_at - @render_start_at
end end
def each(&block) def each(&block)
@@ -167,5 +164,11 @@ module Liquid
def pop_partial def pop_partial
@partial_stack.pop @partial_stack.pop
end end
private
def monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end end
end end