ddmemoize 1.0.0a2 → 1.0.0a3

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: eecfd413a7d098193d530f6b890934993b41a9bc5ce83caddf6c34a94309db22
4
- data.tar.gz: a5f07ddba37a86bf0dc6af6d34426ca9d1b16003f7ae0e20116de174be73afdf
3
+ metadata.gz: 075c0a1dd9addf300b84957a864cdd5e24a7a7131e0eb69a9c175351d82eee09
4
+ data.tar.gz: a398a8b1b884a404b413d837dd81f43057d5c1771cdb893e0838cafa4ff16b4a
5
5
  SHA512:
6
- metadata.gz: e3dcd35b56f321b3abfffe266d765d78ff969622dff3ec7930a580f2023a32ade26a9dcb642715caca800a9eaf466aae6ec66a6e1c091b01dae3fec901f060e3
7
- data.tar.gz: ed990b6c32a034c7aab031a634ca7331556863a4eea6aa5e623c545f8de22a594bc70e4f8a7a336c5555dff9da73c44021064106697035a6a3705314a5471fe6
6
+ metadata.gz: c74d84354fc652fcaec00c1bfeb60ae3e0a718cc8a7431879aefbb4e59e051015ca36e75aec99b913a33646cd93a67558a10317e53d498ce2557bf3289f57bd2
7
+ data.tar.gz: 1492b6c2cebc1398af73e3d0108967459bacece5bad8f0ed4777a549097a83b33adf97296655640dcdf5ff7effda5590adfadc9c99476165fe7a246d375b32f9
data/NEWS.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # DDMemoize news
2
2
 
3
+ ## 1.0.0a3 (2017-12-18)
4
+
5
+ Changes:
6
+
7
+ * Simplified telemetry support
8
+
3
9
  ## 1.0.0a2 (2017-12-10)
4
10
 
5
11
  Features:
data/README.md CHANGED
@@ -83,24 +83,18 @@ Do not memoize functions that depend on mutable state.
83
83
 
84
84
  ### Telemetry
85
85
 
86
- To activate telemetry for a given class, call `DDMemoize.activate` with a `telemetry` keyword argument, passing in a telemetry instance:
86
+ To activate telemetry, call `DDMemoize.enable_telemetry` after requiring `ddmemoize`.
87
87
 
88
- ```ruby
89
- require 'ddmemoize'
90
-
91
- TELEMETRY = DDTelemetry.new
88
+ To print the collected metrics, call `DDMemoize.print_telemetry`:
92
89
 
93
- class FibFast
94
- DDMemoize.activate(self, telemetry: TELEMETRY)
95
-
96
- # …
97
- end
90
+ ```ruby
91
+ DDMemoize.print_telemetry
98
92
  ```
99
93
 
100
- To print the collected metrics, call `DDMemoize.print_telemetry`, passing in the telemetry instance:
101
-
102
- ```ruby
103
- DDMemoize.print_telemetry(TELEMETRY)
94
+ ```
95
+ memoization │ hit miss %
96
+ ────────────┼───────────────────
97
+ FibFast#fib │ 998 1001 49.9%
104
98
  ```
105
99
 
106
100
  ## Development
data/ddmemoize.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
 
15
15
  spec.required_ruby_version = '>= 2.3.0'
16
16
 
17
- spec.add_runtime_dependency('ddtelemetry', '= 1.0.0a1') # TODO: upgrade
17
+ spec.add_runtime_dependency('ddtelemetry', '= 1.0.0a2') # TODO: upgrade
18
18
  spec.add_runtime_dependency('ref', '~> 2.0')
19
19
 
20
20
  spec.files = `git ls-files -z`.split("\x0")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DDMemoize
4
- VERSION = '1.0.0a2'
4
+ VERSION = '1.0.0a3'
5
5
  end
data/lib/ddmemoize.rb CHANGED
@@ -15,39 +15,31 @@ module DDMemoize
15
15
  end
16
16
  end
17
17
 
18
- # @api private
19
- class TelemetryMap
20
- include Singleton
18
+ NONE = Object.new
21
19
 
22
- def initialize
23
- @map = {}
24
- end
20
+ def self.activate(mod)
21
+ mod.extend(Mixin)
22
+ end
25
23
 
26
- def [](mod)
27
- @map[mod]
24
+ class << self
25
+ def enable_telemetry
26
+ @telemetry_enabled = true
28
27
  end
29
28
 
30
- def []=(mod, telemetry)
31
- @map[mod] = telemetry
29
+ def telemetry_enabled?
30
+ @telemetry_enabled
32
31
  end
33
- end
34
-
35
- NONE = Object.new
36
-
37
- def self.activate(mod, telemetry: nil)
38
- mod.extend(Mixin)
39
- TelemetryMap.instance[mod] = telemetry
40
- end
41
32
 
42
- def self.telemetry_for(mod)
43
- TelemetryMap.instance[mod]
33
+ def telemetry_counter
34
+ @_telemetry_counter ||= DDTelemetry::Counter.new
35
+ end
44
36
  end
45
37
 
46
- def self.print_telemetry(telemetry)
38
+ def self.print_telemetry
47
39
  headers = %w[memoization hit miss %]
48
40
 
49
- rows_raw = telemetry.counter(:memoization).map do |(name, type), counter|
50
- { name: name, type: type, count: counter.value }
41
+ rows_raw = DDMemoize.telemetry_counter.map do |(name, type), count|
42
+ { name: name, type: type, count: count }
51
43
  end
52
44
 
53
45
  rows = rows_raw.group_by { |r| r[:name] }.map do |name, rows_for_name|
@@ -70,7 +62,7 @@ module DDMemoize
70
62
  alias_method original_method_name, method_name
71
63
 
72
64
  instance_cache = Hash.new { |hash, key| hash[key] = {} }
73
- telemetry = DDMemoize.telemetry_for(self)
65
+ counter_label = "#{self}##{method_name}"
74
66
 
75
67
  define_method(method_name) do |*args|
76
68
  instance_method_cache = instance_cache[self]
@@ -81,13 +73,11 @@ module DDMemoize
81
73
  value = object ? object.value : NONE
82
74
  end
83
75
 
84
- if telemetry
85
- counter_label = is_a?(Class) ? "#{self}.#{method_name}" : "#{self.class}##{method_name}"
86
-
76
+ if DDMemoize.telemetry_enabled?
87
77
  if NONE.equal?(value)
88
- telemetry.counter(:memoization).increment([counter_label, :miss])
78
+ DDMemoize.telemetry_counter.increment([counter_label, :miss])
89
79
  else
90
- telemetry.counter(:memoization).increment([counter_label, :hit])
80
+ DDMemoize.telemetry_counter.increment([counter_label, :hit])
91
81
  end
92
82
  end
93
83
 
data/samples/fib.rb CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  require 'ddmemoize'
4
4
 
5
- TELEMETRY = DDTelemetry.new
5
+ DDMemoize.enable_telemetry
6
6
 
7
7
  class FibFast
8
- DDMemoize.activate(self, telemetry: TELEMETRY)
8
+ DDMemoize.activate(self)
9
9
 
10
10
  memoized def fib(n)
11
11
  case n
@@ -21,4 +21,4 @@ end
21
21
 
22
22
  p FibFast.new.fib(1000)
23
23
 
24
- DDMemoize.print_telemetry(TELEMETRY)
24
+ DDMemoize.print_telemetry
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ DDMemoize.enable_telemetry
4
+
3
5
  describe DDMemoize do
4
6
  it 'has a version number' do
5
7
  expect(DDMemoize::VERSION).not_to be nil
@@ -41,8 +43,7 @@ describe DDMemoize do
41
43
  end
42
44
 
43
45
  class MemoizationSpecEqual
44
- TELEMETRY = ::DDTelemetry.new
45
- DDMemoize.activate(self, telemetry: TELEMETRY)
46
+ DDMemoize.activate(self)
46
47
 
47
48
  class EqualToEverythingValue
48
49
  def equal?(*)
@@ -79,8 +80,7 @@ describe DDMemoize do
79
80
  end
80
81
 
81
82
  class MemoizationSpecWithTelemetry
82
- TELEMETRY = ::DDTelemetry.new
83
- DDMemoize.activate(self, telemetry: TELEMETRY)
83
+ DDMemoize.activate(self)
84
84
 
85
85
  def run(value)
86
86
  value.upcase
@@ -115,11 +115,10 @@ describe DDMemoize do
115
115
  sample.run
116
116
  sample.run
117
117
 
118
- telemetry = MemoizationSpecEqual::TELEMETRY
119
- counter = telemetry.counter(:memoization)
118
+ counter = DDMemoize.telemetry_counter
120
119
 
121
- expect(counter.value(['MemoizationSpecEqual#run', :miss])).to eq(1)
122
- expect(counter.value(['MemoizationSpecEqual#run', :hit])).to eq(2)
120
+ expect(counter.get(['MemoizationSpecEqual#run', :miss])).to eq(1)
121
+ expect(counter.get(['MemoizationSpecEqual#run', :hit])).to eq(2)
123
122
  end
124
123
 
125
124
  it 'supports memoized def … syntax' do
@@ -150,11 +149,10 @@ describe DDMemoize do
150
149
  sample.run('denis')
151
150
  sample.run('defreyne')
152
151
 
153
- telemetry = MemoizationSpecWithTelemetry::TELEMETRY
154
- counter = telemetry.counter(:memoization)
152
+ counter = DDMemoize.telemetry_counter
155
153
 
156
- expect(counter.value(['MemoizationSpecWithTelemetry#run', :miss])).to eq(2)
157
- expect(counter.value(['MemoizationSpecWithTelemetry#run', :hit])).to eq(1)
154
+ expect(counter.get(['MemoizationSpecWithTelemetry#run', :miss])).to eq(2)
155
+ expect(counter.get(['MemoizationSpecWithTelemetry#run', :hit])).to eq(1)
158
156
  end
159
157
 
160
158
  it 'prints recorded telemetry' do
@@ -164,13 +162,6 @@ describe DDMemoize do
164
162
  sample.run('denis')
165
163
  sample.run('defreyne')
166
164
 
167
- expected = <<~OUTPUT
168
- memoization │ hit miss %
169
- ─────────────────────────────────┼───────────────────
170
- MemoizationSpecWithTelemetry#run │ 2 4 33.3%
171
- OUTPUT
172
-
173
- telemetry = MemoizationSpecWithTelemetry::TELEMETRY
174
- expect { DDMemoize.print_telemetry(telemetry) }.to output(expected).to_stdout
165
+ expect { DDMemoize.print_telemetry }.to output(/memoization │ hit\s+miss\s+%/).to_stdout
175
166
  end
176
167
  end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,6 @@ require 'fuubar'
12
12
 
13
13
  RSpec.configure do |c|
14
14
  c.fuubar_progress_bar_options = {
15
- format: '%c/%C |<%b>%i| %p%%',
15
+ format: '%c/%C |<%b>%i| %p%%', # rubocop:disable Style/FormatStringToken
16
16
  }
17
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddmemoize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0a2
4
+ version: 1.0.0a3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Defreyne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-10 00:00:00.000000000 Z
11
+ date: 2017-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ddtelemetry
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0a1
19
+ version: 1.0.0a2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.0a1
26
+ version: 1.0.0a2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ref
29
29
  requirement: !ruby/object:Gem::Requirement