Class: RenderGuardian::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/render_guardian/profiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(budget) ⇒ Profiler

Returns a new instance of Profiler.



12
13
14
15
# File 'lib/render_guardian/profiler.rb', line 12

def initialize(budget)
  @budget = budget
  @events = []
end

Instance Attribute Details

#budgetObject (readonly)

Returns the value of attribute budget.



10
11
12
# File 'lib/render_guardian/profiler.rb', line 10

def budget
  @budget
end

#eventsObject (readonly)

Returns the value of attribute events.



10
11
12
# File 'lib/render_guardian/profiler.rb', line 10

def events
  @events
end

Instance Method Details

#ingest(events) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/render_guardian/profiler.rb', line 17

def ingest(events)
  Array(events).each do |e|
    @events << RenderEvent.new(
      template:       e[:template].to_s,
      partial:        e[:partial].to_s,
      duration_ms:    e[:duration_ms].to_f,
      db_queries:     Array(e[:db_queries]),
      helpers_called: Array(e[:helpers_called]),
      timestamp:      e[:timestamp] || Time.now
    )
  end
end

#slow_partialsObject



62
63
64
65
# File 'lib/render_guardian/profiler.rb', line 62

def slow_partials
  max_ms = @budget.limit_for(:max_partial_time_ms)
  @events.select { |e| !e.partial.empty? && e.duration_ms > max_ms }
end

#slow_templatesObject



57
58
59
60
# File 'lib/render_guardian/profiler.rb', line 57

def slow_templates
  max_ms = @budget.limit_for(:max_template_time_ms)
  @events.select { |e| e.template && e.duration_ms > max_ms }
end

#stats_by_partialObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/render_guardian/profiler.rb', line 44

def stats_by_partial
  @events.select { |e| !e.partial.empty? }.group_by(&:partial).transform_values do |evts|
    durations = evts.map(&:duration_ms)
    {
      count:       durations.size,
      total_ms:    durations.sum,
      avg_ms:      durations.sum / durations.size,
      max_ms:      durations.max,
      total_queries: evts.sum { |e| e.db_queries.size }
    }
  end
end

#stats_by_templateObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/render_guardian/profiler.rb', line 30

def stats_by_template
  @events.group_by(&:template).transform_values do |evts|
    durations = evts.map(&:duration_ms)
    {
      count:       durations.size,
      total_ms:    durations.sum,
      avg_ms:      durations.sum / durations.size,
      max_ms:      durations.max,
      min_ms:      durations.min,
      total_queries: evts.sum { |e| e.db_queries.size }
    }
  end
end