class RuntimeProfiler::Profiler

Attributes

profiled_constants[RW]

Public Class Methods

new(constants) click to toggle source
# File lib/runtime_profiler/profiler.rb, line 11
def initialize(constants)
  self.profiled_constants = constants
  prepare_for_profiling
end

Public Instance Methods

profile!(key) { || ... } click to toggle source
# File lib/runtime_profiler/profiler.rb, line 16
def profile!(key)
  MethodMeter.measure!(key) { yield }
  save_profiling_data
end

Private Instance Methods

prepare_for_profiling() click to toggle source
# File lib/runtime_profiler/profiler.rb, line 23
def prepare_for_profiling
  subscribe_to_rails_event_notifications
  prepare_methods_to_profile
end
prepare_methods_to_profile() click to toggle source
# File lib/runtime_profiler/profiler.rb, line 48
def prepare_methods_to_profile
  profiled_constants
    .flatten
    .each { |constant| MethodMeter.observe(constant, RuntimeProfiler.excepted_methods) }
end
save_profiling_data() click to toggle source
# File lib/runtime_profiler/profiler.rb, line 54
def save_profiling_data
  unsubscribe_to_rails_event_notifications

  profiling_data = RuntimeProfiler::Data.new \
    controller_data: @action_controller_callback.controller_data,
    sql_data: @active_record_callback.data

  profiling_data.persist!
end
subscribe_to_rails_event_notifications() click to toggle source
# File lib/runtime_profiler/profiler.rb, line 28
def subscribe_to_rails_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_rails_event_notifications() click to toggle source
# File lib/runtime_profiler/profiler.rb, line 42
def unsubscribe_to_rails_event_notifications
  @subscribers.each do |subscriber|
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end
end