Skip to content

Commit

Permalink
[refactor] Renaming, private methods, method movements
Browse files Browse the repository at this point in the history
  • Loading branch information
wnuqui committed Jan 30, 2021
1 parent fbe09b5 commit b1c329b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
13 changes: 4 additions & 9 deletions lib/runtime_profiler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'active_support'

require 'runtime_profiler/profiler'
require 'method_meter'

module RuntimeProfiler
include ActiveSupport::Configurable
Expand Down Expand Up @@ -40,14 +39,10 @@ def configure
yield self if block_given?
end

def profile!(key, konstants)
konstants = konstants.is_a?(Array) ? konstants : [konstants]
profiler = Profiler.new(konstants)
profiler.prepare_for_profiling

MethodMeter.measure!(key) { yield }

profiler.save_profiling_data
def profile!(key, constants)
constants = constants.is_a?(Array) ? constants : [constants]
profiler = Profiler.new(constants)
profiler.profile!(key) { yield }
end
end
end
23 changes: 16 additions & 7 deletions lib/runtime_profiler/profiler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# require 'method_profiler'
require 'method_meter'

require 'runtime_profiler/callbacks/active_record'
require 'runtime_profiler/callbacks/action_controller'
Expand All @@ -10,14 +10,22 @@ class Profiler

def initialize(constants)
self.profiled_constants = constants
prepare_for_profiling
end

def profile!(key)
MethodMeter.measure!(key) { yield }
save_profiling_data
end

private

def prepare_for_profiling
subscribe_to_event_notifications
subscribe_to_rails_event_notifications
prepare_methods_to_profile
end

def subscribe_to_event_notifications
def subscribe_to_rails_event_notifications
@subscribers = []

@active_record_callback = Callback::ActiveRecord.new
Expand All @@ -31,19 +39,20 @@ def subscribe_to_event_notifications
.subscribe('process_action.action_controller', @action_controller_callback)
end

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

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

def save_profiling_data
unsubscribe_to_event_notifications
unsubscribe_to_rails_event_notifications

profiling_data = RuntimeProfiler::Data.new \
controller_data: @action_controller_callback.controller_data,
Expand Down

0 comments on commit b1c329b

Please sign in to comment.