Class: RuntimeProfiler::Profiler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(konstants) ⇒ Profiler

Returns a new instance of Profiler.



11
12
13
# File 'lib/runtime_profiler/profiler.rb', line 11

def initialize(konstants)
  self.instrumented_constants = konstants
end

Instance Attribute Details

#instrumented_constantsObject

Returns the value of attribute instrumented_constants.



9
10
11
# File 'lib/runtime_profiler/profiler.rb', line 9

def instrumented_constants
  @instrumented_constants
end

Instance Method Details

#prepare_for_instrumentationObject



15
16
17
18
# File 'lib/runtime_profiler/profiler.rb', line 15

def prepare_for_instrumentation
  subscribe_to_event_notifications
  prepare_methods_to_instrument
end

#prepare_methods_to_instrumentObject



40
41
42
43
# File 'lib/runtime_profiler/profiler.rb', line 40

def prepare_methods_to_instrument
  instrumented_constants.flatten
                        .each { |constant| MethodMeter.observe(constant, RuntimeProfiler.excepted_methods) }
end

#save_instrumentation_dataObject



45
46
47
48
49
50
51
52
53
# File 'lib/runtime_profiler/profiler.rb', line 45

def save_instrumentation_data
  unsubscribe_to_event_notifications

  instrumentation_data = RuntimeProfiler::InstrumentationData.new \
    controller_data: @action_controller_callback.controller_data,
    sql_data: @active_record_callback.data

  instrumentation_data.persist!
end

#subscribe_to_event_notificationsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/runtime_profiler/profiler.rb', line 20

def subscribe_to_event_notifications
  @subscribers = []

  @active_record_callback = Callback::ActiveRecord.new

  @subscribers << ActiveSupport::Notifications
                  .subscribe('sql.active_record', @active_record_callback)

  @action_controller_callback = Callback::ActionController.new

  @subscribers << ActiveSupport::Notifications
                  .subscribe('process_action.action_controller', @action_controller_callback)
end

#unsubscribe_to_event_notificationsObject



34
35
36
37
38
# File 'lib/runtime_profiler/profiler.rb', line 34

def unsubscribe_to_event_notifications
  @subscribers.each do |subscriber|
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end
end