Class: Revelio::RenderSubscriber

Inherits:
Object
  • Object
show all
Includes:
TemplateType
Defined in:
lib/revelio/render_subscriber.rb

Overview

Subscriber that tracks render start/finish with a stack to attribute queries and GC to each render.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateType

#resolve_template_info, #template_type

Constructor Details

#initialize(query_log) ⇒ RenderSubscriber

Returns a new instance of RenderSubscriber.



11
12
13
14
15
# File 'lib/revelio/render_subscriber.rb', line 11

def initialize(query_log)
  @query_log = query_log
  @stack = []
  @renders = []
end

Instance Attribute Details

#rendersObject (readonly)

Returns the value of attribute renders.



9
10
11
# File 'lib/revelio/render_subscriber.rb', line 9

def renders
  @renders
end

Instance Method Details

#finish(_name, _id, _payload) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/revelio/render_subscriber.rb', line 26

def finish(_name, _id, _payload)
  ctx = @stack.pop
  return unless ctx

  duration = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - ctx[:time_start]) * 1000).round(2)
  queries_during = @query_log[ctx[:query_index]..]
  gc_delta = GC.stat(:total_allocated_objects) - ctx[:gc_start]

  @renders << {
    template: ctx[:template],
    duration: duration,
    queries: queries_during.size,
    query_time: queries_during.sum { |q| q[:duration] }.round(1),
    gc_objects: gc_delta
  }
end

#start(_name, _id, payload) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/revelio/render_subscriber.rb', line 17

def start(_name, _id, payload)
  @stack.push({
    template: resolve_id(payload),
    query_index: @query_log.size,
    gc_start: GC.stat(:total_allocated_objects),
    time_start: Process.clock_gettime(Process::CLOCK_MONOTONIC)
  })
end