-
Notifications
You must be signed in to change notification settings - Fork 18
/
sink.go
346 lines (306 loc) · 7.9 KB
/
sink.go
1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package mock
import (
"fmt"
"math"
"sync"
"sync/atomic"
"testing"
)
type entry struct {
val uint64
count int64
}
// A Sink is a mock sink meant for testing that is safe for concurrent use.
type Sink struct {
counters sync.Map
timers sync.Map
gauges sync.Map
// write held only during Reset(), all map accesses must hold the read lock
mu sync.RWMutex
}
func NewSink() *Sink {
return new(Sink)
}
// Flush is a no-op method
func (*Sink) Flush() {}
func (s *Sink) Reset() {
s.mu.Lock()
s.counters = sync.Map{}
s.timers = sync.Map{}
s.gauges = sync.Map{}
s.mu.Unlock()
}
func (s *Sink) FlushCounter(name string, val uint64) {
s.mu.RLock()
v, ok := s.counters.Load(name)
if !ok {
v, _ = s.counters.LoadOrStore(name, new(entry))
}
s.mu.RUnlock()
p := v.(*entry)
atomic.AddUint64(&p.val, val)
atomic.AddInt64(&p.count, 1)
}
func (s *Sink) FlushGauge(name string, val uint64) {
s.mu.RLock()
v, ok := s.gauges.Load(name)
if !ok {
v, _ = s.gauges.LoadOrStore(name, new(entry))
}
s.mu.RUnlock()
p := v.(*entry)
atomic.AddUint64(&p.val, val)
atomic.AddInt64(&p.count, 1)
}
func atomicAddFloat64(dest *uint64, delta float64) {
for {
cur := atomic.LoadUint64(dest)
curVal := math.Float64frombits(cur)
nxtVal := curVal + delta
nxt := math.Float64bits(nxtVal)
if atomic.CompareAndSwapUint64(dest, cur, nxt) {
return
}
}
}
func (s *Sink) FlushTimer(name string, val float64) {
s.mu.RLock()
v, ok := s.timers.Load(name)
if !ok {
v, _ = s.timers.LoadOrStore(name, new(entry))
}
s.mu.RUnlock()
p := v.(*entry)
atomicAddFloat64(&p.val, val)
atomic.AddInt64(&p.count, 1)
}
func (s *Sink) LoadCounter(name string) (uint64, bool) {
s.mu.RLock()
v, ok := s.counters.Load(name)
s.mu.RUnlock()
if ok {
p := v.(*entry)
return atomic.LoadUint64(&p.val), true
}
return 0, false
}
func (s *Sink) LoadGauge(name string) (uint64, bool) {
s.mu.RLock()
v, ok := s.gauges.Load(name)
s.mu.RUnlock()
if ok {
p := v.(*entry)
return atomic.LoadUint64(&p.val), true
}
return 0, false
}
func (s *Sink) LoadTimer(name string) (float64, bool) {
s.mu.RLock()
v, ok := s.timers.Load(name)
s.mu.RUnlock()
if ok {
p := v.(*entry)
bits := atomic.LoadUint64(&p.val)
return math.Float64frombits(bits), true
}
return 0, false
}
// short-hand methods
func (s *Sink) Counter(name string) uint64 {
v, _ := s.LoadCounter(name)
return v
}
func (s *Sink) Gauge(name string) uint64 {
v, _ := s.LoadGauge(name)
return v
}
func (s *Sink) Timer(name string) float64 {
v, _ := s.LoadTimer(name)
return v
}
// these methods are mostly useful for testing
func (s *Sink) CounterCallCount(name string) int64 {
s.mu.RLock()
v, ok := s.counters.Load(name)
s.mu.RUnlock()
if ok {
return atomic.LoadInt64(&v.(*entry).count)
}
return 0
}
func (s *Sink) GaugeCallCount(name string) int64 {
s.mu.RLock()
v, ok := s.gauges.Load(name)
s.mu.RUnlock()
if ok {
return atomic.LoadInt64(&v.(*entry).count)
}
return 0
}
func (s *Sink) TimerCallCount(name string) int64 {
s.mu.RLock()
v, ok := s.timers.Load(name)
s.mu.RUnlock()
if ok {
return atomic.LoadInt64(&v.(*entry).count)
}
return 0
}
// test helpers
// AssertCounterEquals asserts that Counter name is present and has value exp.
func (s *Sink) AssertCounterEquals(tb testing.TB, name string, exp uint64) {
tb.Helper()
u, ok := s.LoadCounter(name)
if !ok {
tb.Errorf("gostats/mock: Counter (%q): not found", name)
return
}
if u != exp {
tb.Errorf("gostats/mock: Counter (%q): Expected: %d Got: %d", name, exp, u)
}
}
// AssertGaugeEquals asserts that Gauge name is present and has value exp.
func (s *Sink) AssertGaugeEquals(tb testing.TB, name string, exp uint64) {
tb.Helper()
u, ok := s.LoadGauge(name)
if !ok {
tb.Errorf("gostats/mock: Gauge (%q): not found", name)
return
}
if u != exp {
tb.Errorf("gostats/mock: Gauge (%q): Expected: %d Got: %d", name, exp, u)
}
}
// AssertTimerEquals asserts that Timer name is present and has value exp.
func (s *Sink) AssertTimerEquals(tb testing.TB, name string, exp float64) {
tb.Helper()
f, ok := s.LoadTimer(name)
if !ok {
tb.Errorf("gostats/mock: Timer (%q): not found", name)
return
}
if f != exp {
tb.Errorf("gostats/mock: Timer (%q): Expected: %f Got: %f", name, exp, f)
}
}
// AssertCounterExists asserts that Counter name exists.
func (s *Sink) AssertCounterExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadCounter(name); !ok {
tb.Errorf("gostats/mock: Counter (%q): should exist", name)
}
}
// AssertGaugeExists asserts that Gauge name exists.
func (s *Sink) AssertGaugeExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadGauge(name); !ok {
tb.Errorf("gostats/mock: Gauge (%q): should exist", name)
}
}
// AssertTimerExists asserts that Timer name exists.
func (s *Sink) AssertTimerExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadTimer(name); !ok {
tb.Errorf("gostats/mock: Timer (%q): should exist", name)
}
}
// AssertCounterNotExists asserts that Counter name does not exist.
func (s *Sink) AssertCounterNotExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadCounter(name); ok {
tb.Errorf("gostats/mock: Counter (%q): expected Counter to not exist", name)
}
}
// AssertGaugeNotExists asserts that Gauge name does not exist.
func (s *Sink) AssertGaugeNotExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadGauge(name); ok {
tb.Errorf("gostats/mock: Gauge (%q): expected Gauge to not exist", name)
}
}
// AssertTimerNotExists asserts that Timer name does not exist.
func (s *Sink) AssertTimerNotExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadTimer(name); ok {
tb.Errorf("gostats/mock: Timer (%q): expected Timer to not exist", name)
}
}
// AssertCounterCallCount asserts that Counter name was called exp times.
func (s *Sink) AssertCounterCallCount(tb testing.TB, name string, exp int) {
tb.Helper()
v, ok := s.counters.Load(name)
if !ok {
tb.Errorf("gostats/mock: Counter (%q): not found", name)
return
}
p := v.(*entry)
n := atomic.LoadInt64(&p.count)
if n != int64(exp) {
tb.Errorf("gostats/mock: Counter (%q) Call Count: Expected: %d Got: %d",
name, exp, n)
}
}
// AssertGaugeCallCount asserts that Gauge name was called exp times.
func (s *Sink) AssertGaugeCallCount(tb testing.TB, name string, exp int) {
tb.Helper()
v, ok := s.gauges.Load(name)
if !ok {
tb.Errorf("gostats/mock: Gauge (%q): not found", name)
return
}
p := v.(*entry)
n := atomic.LoadInt64(&p.count)
if n != int64(exp) {
tb.Errorf("gostats/mock: Gauge (%q) Call Count: Expected: %d Got: %d",
name, exp, n)
}
}
// AssertTimerCallCount asserts that Timer name was called exp times.
func (s *Sink) AssertTimerCallCount(tb testing.TB, name string, exp int) {
tb.Helper()
v, ok := s.timers.Load(name)
if !ok {
tb.Errorf("gostats/mock: Timer (%q): not found", name)
return
}
p := v.(*entry)
n := atomic.LoadInt64(&p.count)
if n != int64(exp) {
tb.Errorf("gostats/mock: Timer (%q) Call Count: Expected: %d Got: %d",
name, exp, n)
}
}
var (
_ testing.TB = (*fatalTest)(nil)
_ testing.TB = (*fatalBench)(nil)
)
type fatalTest testing.T
func (t *fatalTest) Errorf(format string, args ...interface{}) {
t.Fatalf(format, args...)
}
type fatalBench testing.B
func (t *fatalBench) Errorf(format string, args ...interface{}) {
t.Fatalf(format, args...)
}
// Fatal is a wrapper around *testing.T and *testing.B that causes Sink Assert*
// methods to immediately fail a test and stop execution. Otherwise, the Assert
// methods call tb.Errorf(), which marks the test as failed, but allows
// execution to continue.
//
// Examples of Fatal() can be found in the sink test code.
//
// var sink Sink
// var t *testing.T
// sink.AssertCounterEquals(Must(t), "name", 1)
//
func Fatal(tb testing.TB) testing.TB {
switch t := tb.(type) {
case *testing.T:
return (*fatalTest)(t)
case *testing.B:
return (*fatalBench)(t)
default:
fmt.Sprintf("invalid type for testing.TB: %T", tb)
}
panic("unreachable")
}