method_meter 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 968a9343be92f7570f1a15dc5c341d39b64fd76b3922263605d008b5f6057e34
4
- data.tar.gz: 86223aa04becb4ac8c8ef8c37f2d08489429d88f2438509250df02082386cc12
3
+ metadata.gz: 5b21e5d9564b1e2716490f84775bf00d5381d89259ffd4d5f5ebf5df90390e29
4
+ data.tar.gz: be86e7ffbc8f9fba9c0b50200625b9c62c66b2ff8196d279e5072796149c50b8
5
5
  SHA512:
6
- metadata.gz: 45291068c777e3885de2a94767494b3e9c25dac036fe8dbe115a50c33b73e4ca148c54fa569c6afd094fa90900c45f7a901c9f8cf9341ca657cffa345a5c707a
7
- data.tar.gz: 2d2344afed7ffc5b0acfa067859dd38297306d77781944f15b227b89a8b2176fb67a22a9ab4591c8bfa6938a66cd999beb91677801ebc5cedab1bbb0191775e1
6
+ metadata.gz: a3120f5ecf09523ac2a02a34a35bb80fc43fb2fa4b34d094606bb4c371542f57294a769bc0f743c62827ec6baf9059807267a54a57bda5d22baed79a606ede75
7
+ data.tar.gz: b41f1de2cdad17844289822ac761d4e2f057fd776f34432d166e8e6eea31492e1b26c7c9e0220e3024187833fbc01e3094facab2581d0f43dc2d658b6aadf535
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'method_meter'
5
+ require 'awesome_print'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -9,62 +9,37 @@ module MethodMeter
9
9
 
10
10
  class << self
11
11
  def observe(object, excepted_methods=[])
12
- self.events = [] if self.events.nil?
13
- self.exceptions = [] if self.exceptions.nil?
14
- self.exceptions |= excepted_methods
12
+ init excepted_methods
15
13
 
16
14
  DefinedMethods.in(object).each do |group|
17
15
  group[:object].module_eval do
18
16
  group[:methods].each do |method|
19
- next if MethodMeter.exceptions.include?(method)
20
-
21
- method_with_profiling = method.to_s + '_with_profiling'
22
- method_without_profiling = method.to_s + '_without_profiling'
23
- event_name = DefinedMethods.fqmn(group, method)
24
-
25
- next if event_name =~ /_profiling/
26
- next if MethodMeter.events.include?(event_name)
27
-
28
- MethodMeter.events << event_name
29
-
30
- define_method(method_with_profiling) do |*args, &block|
31
- ActiveSupport::Notifications.instrument(event_name, args) do
32
- send(method_without_profiling, *args, &block)
33
- end
34
- end
35
-
36
- alias_method method_without_profiling, method
37
- alias_method method, method_with_profiling
38
-
39
- private method_with_profiling if group[:private]
40
- protected method_with_profiling if group[:protected]
17
+ MethodMeter.define_instrumented_method(group[:object], method, group[:private], group[:protected], group[:singleton])
41
18
  end
42
19
  end
43
20
  end
44
21
  end
45
22
 
46
23
  def measure!(key)
47
- self.subscribers = []
48
- self.data = {} if self.data.blank?
49
- self.data[key] = {}
24
+ data[key] = {}
50
25
 
51
- self.events.each do |event|
52
- self.subscribers << ActiveSupport::Notifications.subscribe(event) do |_, started_at, finished_at, _, _|
53
- self.data[key][event] = [] unless self.data[key].has_key?(event)
54
- self.data[key][event] << (finished_at - started_at)
26
+ events.each do |event|
27
+ subscribers << ActiveSupport::Notifications.subscribe(event) do |_, started_at, finished_at, _, _|
28
+ data[key][event] = [] unless data[key].has_key?(event)
29
+ data[key][event] << (finished_at - started_at)
55
30
  end
56
31
  end
57
32
 
58
33
  yield
59
34
 
60
- self.subscribers.each do |subscriber|
35
+ subscribers.each do |subscriber|
61
36
  ActiveSupport::Notifications.unsubscribe(subscriber)
62
37
  end
63
38
  end
64
39
 
65
40
  def measurement
66
41
  @measurement ||= begin
67
- self.data.collect do |key, measurement_records|
42
+ data.collect do |key, measurement_records|
68
43
  _measurement = measurement_records.collect do |method_name, records|
69
44
  total_calls = records.size
70
45
  total_runtime = records.reduce(:+) * 1000
@@ -84,5 +59,48 @@ module MethodMeter
84
59
  end
85
60
  end
86
61
  end
62
+
63
+ def profiling_method_names(method)
64
+ method_with_profiling = method.to_s + '_with_profiling'
65
+ method_without_profiling = method.to_s + '_without_profiling'
66
+ [method_with_profiling, method_without_profiling]
67
+ end
68
+
69
+ def instrument_method?(method, event_name)
70
+ !exceptions.include?(method) && !events.include?(event_name) && (event_name =~ /_profiling/).nil?
71
+ end
72
+
73
+ def define_instrumented_method(object, method, is_private, is_protected, is_singleton)
74
+ object.module_eval do
75
+ method_with_profiling, method_without_profiling = MethodMeter.profiling_method_names(method)
76
+ event_name = DefinedMethods.fqmn(object.to_s, method, is_singleton)
77
+
78
+ return unless MethodMeter.instrument_method?(method, event_name)
79
+
80
+ MethodMeter.events << event_name
81
+
82
+ define_method(method_with_profiling) do |*args, &block|
83
+ ActiveSupport::Notifications.instrument(event_name, args) do
84
+ send(method_without_profiling, *args, &block)
85
+ end
86
+ end
87
+
88
+ alias_method method_without_profiling, method
89
+ alias_method method, method_with_profiling
90
+
91
+ private method_with_profiling if is_private
92
+ protected method_with_profiling if is_protected
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def init(excepted_methods)
99
+ self.events = [] if events.nil?
100
+ self.exceptions = [] if exceptions.nil?
101
+ self.exceptions |= excepted_methods
102
+ self.subscribers = []
103
+ self.data = {} if data.blank?
104
+ end
87
105
  end
88
106
  end
@@ -1,3 +1,3 @@
1
1
  module MethodMeter
2
- VERSION = '0.3.5'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_meter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wilfrido T. Nuqui Jr.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-01 00:00:00.000000000 Z
11
+ date: 2021-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport