Module: MethodMeter

Defined in:
lib/method_meter.rb,
lib/method_meter/version.rb

Constant Summary collapse

VERSION =
'0.2.5'

Class Method Summary collapse

Class Method Details

.measure!(key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/method_meter.rb', line 42

def measure!(key)
  self.subscribers  = []
  self.data         = {} if self.data.blank?
  self.data[key]    = {}

  self.events.each do |event|
    self.subscribers << ActiveSupport::Notifications.subscribe(event) do |_, started_at, finished_at, _, _|
      self.data[key][event] = [] unless self.data[key].has_key?(event)
      self.data[key][event] << (finished_at - started_at)
    end
  end

  yield

  self.subscribers.each do |subscriber|
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end
end

.measurementObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/method_meter.rb', line 61

def measurement
  @measurement ||= begin
    self.data.collect do |key, measurement_records|
      _measurement = measurement_records.collect do |method_name, records|
        total_calls   = records.size
        total_runtime = records.reduce(:+) * 1000
        average       = total_runtime / total_calls

        {
          method: method_name,
          min: records.min * 1000,
          max: records.max * 1000,
          average: average,
          total_runtime: total_runtime,
          total_calls: total_calls,
        }
      end

      { key => _measurement }
    end
  end
end

.observe(object) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/method_meter.rb', line 11

def observe(object)
  self.events = [] if self.events.blank?

  DefinedMethods.in(object).each do |group|
    group[:object].module_eval do
      group[:methods].each do |method|
        method_with_profiling     = method.to_s + '_with_profiling'
        method_without_profiling  = method.to_s + '_without_profiling'
        event_name                = DefinedMethods.fqmn(group, method)

        next if event_name =~ /_profiling/
        next if MethodMeter.events.include?(event_name)

        MethodMeter.events << event_name

        define_method(method_with_profiling) do |*args, &block|
          ActiveSupport::Notifications.instrument(event_name, args) do
            send(method_without_profiling, *args, &block)
          end
        end

        alias_method method_without_profiling, method
        alias_method method, method_with_profiling

        private method_with_profiling if group[:private]
        protected method_with_profiling if group[:protected]
      end
    end
  end
end