Class: Profiler::Collectors::PerformanceCollector

Inherits:
BaseCollector
  • Object
show all
Defined in:
lib/profiler/collectors/performance_collector.rb

Instance Attribute Summary

Attributes inherited from BaseCollector

#profile

Instance Method Summary collapse

Methods inherited from BaseCollector

descendants, #has_data?, inherited, #name, #panel_content, #render_html, #render_mode

Constructor Details

#initialize(profile) ⇒ PerformanceCollector

Returns a new instance of PerformanceCollector.



9
10
11
12
13
# File 'lib/profiler/collectors/performance_collector.rb', line 9

def initialize(profile)
  super
  @events = []
  @subscriptions = []
end

Instance Method Details

#collectObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/profiler/collectors/performance_collector.rb', line 80

def collect
  # Unsubscribe from all notifications
  @subscriptions.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }

  data = {
    total_events: @events.size,
    total_duration: @events.sum(&:duration).round(2),
    events: @events.map(&:to_h)
  }

  store_data(data)
end

#iconObject



15
16
17
# File 'lib/profiler/collectors/performance_collector.rb', line 15

def icon
  ""
end

#priorityObject



19
20
21
# File 'lib/profiler/collectors/performance_collector.rb', line 19

def priority
  30
end

#subscribeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/profiler/collectors/performance_collector.rb', line 34

def subscribe
  return unless defined?(ActiveSupport::Notifications)

  # Subscribe to controller processing
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("process_action.action_controller") do |name, started, finished, unique_id, payload|
    @events << Models::TimelineEvent.new(
      name: "Controller: #{payload[:controller]}##{payload[:action]}",
      started_at: started,
      finished_at: finished,
      payload: {
        controller: payload[:controller],
        action: payload[:action],
        format: payload[:format],
        method: payload[:method],
        path: payload[:path],
        status: payload[:status]
      }
    )
  end

  # Subscribe to view rendering
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("render_template.action_view") do |name, started, finished, unique_id, payload|
    @events << Models::TimelineEvent.new(
      name: "Render: #{payload[:identifier]}",
      started_at: started,
      finished_at: finished,
      payload: {
        identifier: payload[:identifier],
        layout: payload[:layout]
      }
    )
  end

  # Subscribe to partial rendering
  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("render_partial.action_view") do |name, started, finished, unique_id, payload|
    @events << Models::TimelineEvent.new(
      name: "Partial: #{payload[:identifier]}",
      started_at: started,
      finished_at: finished,
      payload: {
        identifier: payload[:identifier]
      }
    )
  end
end

#tab_configObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/profiler/collectors/performance_collector.rb', line 23

def tab_config
  {
    key: "performance",
    label: "Performance",
    icon: icon,
    priority: priority,
    enabled: true,
    default_active: false
  }
end

#toolbar_summaryObject



93
94
95
96
97
98
99
100
# File 'lib/profiler/collectors/performance_collector.rb', line 93

def toolbar_summary
  duration = @events.sum(&:duration).round(2)

  {
    text: "#{@events.size} events (#{duration}ms)",
    color: "blue"
  }
end