-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathavgsamplerate_test.go
445 lines (414 loc) · 9.96 KB
/
avgsamplerate_test.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package dynsampler
import (
"crypto/rand"
"fmt"
"math"
mrand "math/rand"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAvgSampleUpdateMaps(t *testing.T) {
a := &AvgSampleRate{
GoalSampleRate: 20,
}
tsts := []struct {
inputSampleCount map[string]float64
expectedSavedSampleRates map[string]int
}{
{
map[string]float64{
"one": 1,
"two": 1,
"three": 2,
"four": 5,
"five": 8,
"six": 15,
"seven": 45,
"eight": 612,
"nine": 2000,
"ten": 10000,
},
map[string]int{
"one": 1,
"two": 1,
"three": 1,
"four": 1,
"five": 1,
"six": 1,
"seven": 1,
"eight": 6,
"nine": 14,
"ten": 47,
},
},
{
map[string]float64{
"one": 1,
"two": 1,
"three": 2,
"four": 5,
"five": 8,
"six": 15,
"seven": 45,
"eight": 50,
"nine": 60,
},
map[string]int{
"one": 1,
"two": 1,
"three": 2,
"four": 5,
"five": 8,
"six": 11,
"seven": 24,
"eight": 26,
"nine": 30,
},
},
{
map[string]float64{
"one": 1,
"two": 1,
"three": 2,
"four": 5,
"five": 7,
},
map[string]int{
"one": 1,
"two": 1,
"three": 2,
"four": 5,
"five": 7,
},
},
{
map[string]float64{
"one": 1000,
"two": 1000,
"three": 2000,
"four": 5000,
"five": 7000,
},
map[string]int{
"one": 7,
"two": 7,
"three": 13,
"four": 29,
"five": 39,
},
},
{
map[string]float64{
"one": 6000,
"two": 6000,
"three": 6000,
"four": 6000,
"five": 6000,
},
map[string]int{
"one": 20,
"two": 20,
"three": 20,
"four": 20,
"five": 20,
},
},
{
map[string]float64{
"one": 12000,
},
map[string]int{
"one": 20,
},
},
{
map[string]float64{},
map[string]int{},
},
{
map[string]float64{
"one": 10,
"two": 1,
"three": 1,
"four": 1,
"five": 1,
"six": 1,
"seven": 1,
"eight": 1,
"nine": 1,
"ten": 1,
"eleven": 1,
"twelve": 1,
"thirteen": 1,
"fourteen": 1,
"fifteen": 1,
"sixteen": 1,
"seventeen": 1,
"eighteen": 1,
"nineteen": 1,
"twenty": 1,
},
map[string]int{
"one": 7,
"two": 1,
"three": 1,
"four": 1,
"five": 1,
"six": 1,
"seven": 1,
"eight": 1,
"nine": 1,
"ten": 1,
"eleven": 1,
"twelve": 1,
"thirteen": 1,
"fourteen": 1,
"fifteen": 1,
"sixteen": 1,
"seventeen": 1,
"eighteen": 1,
"nineteen": 1,
"twenty": 1,
},
},
}
for i, tst := range tsts {
a.currentCounts = tst.inputSampleCount
a.updateMaps()
assert.Equal(t, len(a.currentCounts), 0)
assert.Equal(t, a.savedSampleRates, tst.expectedSavedSampleRates, fmt.Sprintf("test %d failed", i))
}
}
func TestAvgSampleGetSampleRateStartup(t *testing.T) {
a := &AvgSampleRate{
GoalSampleRate: 10,
currentCounts: map[string]float64{},
}
rate := a.GetSampleRate("key")
assert.Equal(t, rate, 10)
// and the counters still get bumped
assert.Equal(t, a.currentCounts["key"], 1.0)
}
func TestAvgSampleRace(t *testing.T) {
a := &AvgSampleRate{
GoalSampleRate: 2,
currentCounts: map[string]float64{},
savedSampleRates: map[string]int{},
haveData: true,
}
wg := sync.WaitGroup{}
wg.Add(1)
wg.Add(1)
// set up 100 parallel readers, each reading 1000 times
go func() {
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
for j := 0; j < 1000; j++ {
rate := a.GetSampleRate("key" + strconv.Itoa(i))
assert.NotEqual(t, rate <= 0, true, "rate should never be lte zero")
}
wg.Done()
}(i)
}
wg.Done()
}()
go func() {
for i := 0; i < 100; i++ {
a.updateMaps()
}
wg.Done()
}()
wg.Wait()
}
func TestAvgSampleRateGetSampleRate(t *testing.T) {
a := &AvgSampleRate{
haveData: true,
}
a.currentCounts = map[string]float64{
"one": 5,
"two": 8,
}
a.savedSampleRates = map[string]int{
"one": 10,
"two": 1,
"three": 5,
}
tsts := []struct {
inputKey string
expectedSampleRate int
expectedCurrentCountForKey float64
}{
{"one", 10, 6},
{"two", 1, 9},
{"two", 1, 10},
{"three", 5, 1}, // key missing from current counts
{"three", 5, 2},
{"four", 1, 1}, // key missing from current and saved counts
{"four", 1, 2},
}
for _, tst := range tsts {
rate := a.GetSampleRate(tst.inputKey)
assert.Equal(t, rate, tst.expectedSampleRate)
assert.Equal(t, a.currentCounts[tst.inputKey], tst.expectedCurrentCountForKey)
}
}
func TestAvgSampleRateMaxKeys(t *testing.T) {
a := &AvgSampleRate{
MaxKeys: 3,
}
a.currentCounts = map[string]float64{
"one": 1,
"two": 1,
}
a.savedSampleRates = map[string]int{}
// with MaxKeys 3, we are under the key limit, so three should get added
a.GetSampleRate("three")
assert.Equal(t, 3, len(a.currentCounts))
assert.Equal(t, 1., a.currentCounts["three"])
// Now we're at 3 keys - four should not be added
a.GetSampleRate("four")
assert.Equal(t, 3, len(a.currentCounts))
_, found := a.currentCounts["four"]
assert.Equal(t, false, found)
// We should still support bumping counts for existing keys
a.GetSampleRate("one")
assert.Equal(t, 3, len(a.currentCounts))
assert.Equal(t, 2., a.currentCounts["one"])
}
func TestAvgSampleRateSaveState(t *testing.T) {
var sampler Sampler
asr := &AvgSampleRate{}
// ensure the interface is implemented
sampler = asr
err := sampler.Start()
assert.Nil(t, err)
asr.lock.Lock()
asr.savedSampleRates = map[string]int{"foo": 2, "bar": 4}
asr.haveData = true
asr.lock.Unlock()
assert.Equal(t, 2, sampler.GetSampleRate("foo"))
assert.Equal(t, 4, sampler.GetSampleRate("bar"))
state, err := sampler.SaveState()
assert.Nil(t, err)
var newSampler Sampler = &AvgSampleRate{}
err = newSampler.LoadState(state)
assert.Nil(t, err)
err = newSampler.Start()
assert.Nil(t, err)
assert.Equal(t, 2, newSampler.GetSampleRate("foo"))
assert.Equal(t, 4, newSampler.GetSampleRate("bar"))
}
// This is a long test because we generate a lot of random data and run it through the sampler
// The goal is to determine if we actually hit the specified target rate (within a tolerance) an acceptable
// number of times. Most of the time, the average sample rate of observations kept should be close
// to the target rate
func TestAvgSampleRateHitsTargetRate(t *testing.T) {
mrand.Seed(time.Now().Unix())
testRates := []int{50, 100}
testKeyCount := []int{10, 100}
tolerancePct := float64(0.2)
for _, rate := range testRates {
tolerance := float64(rate) * tolerancePct
toleranceUpper := float64(rate) + tolerance
toleranceLower := float64(rate) - tolerance
for _, keyCount := range testKeyCount {
sampler := &AvgSampleRate{GoalSampleRate: rate, currentCounts: make(map[string]float64)}
// build a consistent set of keys to use
keys := make([]string, keyCount)
for i := 0; i < keyCount; i++ {
keys[i] = randomString(8)
}
for i, key := range keys {
// generate key counts of different magnitudes - keys reliably get the same magnitude
// so that count ranges are reasonable (i.e. they don't go from 1 to 10000 back to 100)
base := math.Pow10(i%3 + 1)
count := float64(((i%10)+1))*base + float64(mrand.Intn(int(base)))
sampler.currentCounts[key] = count
}
// build an initial set of sample rates so we don't just return the target rate
sampler.updateMaps()
var success float64
for i := 0; i < 100; i++ {
totalSampleRate := 0
totalKeptObservations := 0
for j, key := range keys {
base := math.Pow10(j%3 + 1)
count := float64(((j%10)+1))*base + float64(mrand.Intn(int(base)))
for k := 0; k < int(count); k++ {
rate := sampler.GetSampleRate(key)
if mrand.Intn(rate) == 0 {
totalSampleRate += rate
totalKeptObservations++
}
}
}
avgSampleRate := float64(totalSampleRate) / float64(totalKeptObservations)
if avgSampleRate <= toleranceUpper && avgSampleRate >= toleranceLower {
success++
}
sampler.updateMaps()
}
assert.True(t, success/100.0 >= 0.95, "target rate test %d with key count %d failed with success rate of only %f", rate, keyCount, success/100.0)
}
}
}
func TestAvgSampleUpdateMapsSparseCounts(t *testing.T) {
a := &AvgSampleRate{
GoalSampleRate: 20,
}
a.savedSampleRates = make(map[string]int)
for i := 0; i <= 100; i++ {
input := make(map[string]float64)
// simulate steady stream of input from one key
input["largest_count"] = 20
// sporadic keys with single counts that come and go with each interval
for j := 0; j < 5; j++ {
key := randomString(8)
input[key] = 1
}
a.currentCounts = input
a.updateMaps()
}
assert.Equal(t, 16, a.savedSampleRates["largest_count"])
}
func randomString(length int) string {
b := make([]byte, length/2)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
func TestAvgSampleRate_Start(t *testing.T) {
tests := []struct {
name string
ClearFrequencySec int
ClearFrequencyDuration time.Duration
wantDuration time.Duration
wantErr bool
}{
{"sec only", 2, 0, 2 * time.Second, false},
{"dur only", 0, 1003 * time.Millisecond, 1003 * time.Millisecond, false},
{"default", 0, 0, 30 * time.Second, false},
{"both", 2, 2 * time.Second, 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &AvgSampleRate{
ClearFrequencySec: tt.ClearFrequencySec,
ClearFrequencyDuration: tt.ClearFrequencyDuration,
}
err := a.Start()
if (err != nil) != tt.wantErr {
t.Errorf("AvgSampleRate error = %v, wantErr %v", err, tt.wantErr)
}
if err == nil {
defer a.Stop()
if tt.wantDuration != a.ClearFrequencyDuration {
t.Errorf("AvgSampleRate duration mismatch = want %v, got %v", tt.wantDuration, a.ClearFrequencyDuration)
}
}
})
}
}