-
Notifications
You must be signed in to change notification settings - Fork 10
/
perkeythroughput_test.go
276 lines (268 loc) · 5.48 KB
/
perkeythroughput_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
package dynsampler
import (
"fmt"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPerKeyThroughputUpdateMaps(t *testing.T) {
p := &PerKeyThroughput{
ClearFrequencyDuration: 30 * time.Second,
PerKeyThroughputPerSec: 5,
}
tsts := []struct {
inputCount map[string]int
expectedSavedSampleRates map[string]int
}{
{
map[string]int{
"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": 4,
"nine": 13,
"ten": 66,
},
},
{
map[string]int{
"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": 1,
"four": 1,
"five": 1,
"six": 1,
"seven": 1,
"eight": 1,
"nine": 1,
},
},
{
map[string]int{
"one": 1,
"two": 1,
"three": 2,
"four": 5,
"five": 7,
},
map[string]int{
"one": 1,
"two": 1,
"three": 1,
"four": 1,
"five": 1,
},
},
{
map[string]int{
"one": 1000,
"two": 1000,
"three": 2000,
"four": 5000,
"five": 7000,
},
map[string]int{
"one": 6,
"two": 6,
"three": 13,
"four": 33,
"five": 46,
},
},
{
map[string]int{
"one": 1000,
"two": 1000,
"three": 2000,
"four": 5000,
"five": 70000,
},
map[string]int{
"one": 6,
"two": 6,
"three": 13,
"four": 33,
"five": 466,
},
},
{
map[string]int{
"one": 6000,
"two": 6000,
"three": 6000,
"four": 6000,
"five": 6000,
},
map[string]int{
"one": 40,
"two": 40,
"three": 40,
"four": 40,
"five": 40,
},
},
{
map[string]int{
"one": 12000,
},
map[string]int{
"one": 80,
},
},
{
map[string]int{},
map[string]int{},
},
}
for i, tst := range tsts {
p.currentCounts = tst.inputCount
p.updateMaps()
assert.Equal(t, len(p.currentCounts), 0)
assert.Equal(t, p.savedSampleRates, tst.expectedSavedSampleRates, fmt.Sprintf("test %d failed", i))
}
}
func TestPerKeyThroughputGetSampleRate(t *testing.T) {
p := &PerKeyThroughput{}
p.currentCounts = map[string]int{
"one": 5,
"two": 8,
}
p.savedSampleRates = map[string]int{
"one": 10,
"two": 1,
"three": 5,
}
tsts := []struct {
inputKey string
expectedSampleRate int
expectedCurrentCountForKey int
}{
{"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 := p.GetSampleRate(tst.inputKey)
assert.Equal(t, rate, tst.expectedSampleRate)
assert.Equal(t, p.currentCounts[tst.inputKey], tst.expectedCurrentCountForKey)
}
}
func TestPerKeyThroughputRace(t *testing.T) {
p := &PerKeyThroughput{
PerKeyThroughputPerSec: 2,
currentCounts: map[string]int{},
savedSampleRates: map[string]int{},
}
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 := p.GetSampleRate("key" + strconv.Itoa(i))
assert.NotEqual(t, rate, 0, "rate should never be zero")
}
wg.Done()
}(i)
}
wg.Done()
}()
go func() {
for i := 0; i < 100; i++ {
p.updateMaps()
}
wg.Done()
}()
wg.Wait()
}
func TestPerKeyThroughputMaxKeys(t *testing.T) {
p := &PerKeyThroughput{
MaxKeys: 3,
}
p.currentCounts = map[string]int{
"one": 1,
"two": 1,
}
p.savedSampleRates = map[string]int{}
// with MaxKeys 3, we are under the key limit, so three should get added
p.GetSampleRate("three")
assert.Equal(t, 3, len(p.currentCounts))
assert.Equal(t, 1, p.currentCounts["three"])
// Now we're at 3 keys - four should not be added
p.GetSampleRate("four")
assert.Equal(t, 3, len(p.currentCounts))
_, found := p.currentCounts["four"]
assert.Equal(t, false, found)
// We should still support bumping counts for existing keys
p.GetSampleRate("one")
assert.Equal(t, 3, len(p.currentCounts))
assert.Equal(t, 2, p.currentCounts["one"])
}
func TestPerKeyThroughput_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 := &PerKeyThroughput{
ClearFrequencySec: tt.ClearFrequencySec,
ClearFrequencyDuration: tt.ClearFrequencyDuration,
}
err := a.Start()
if (err != nil) != tt.wantErr {
t.Errorf("PerKeyThroughput error = %v, wantErr %v", err, tt.wantErr)
}
if err == nil {
defer a.Stop()
if tt.wantDuration != a.ClearFrequencyDuration {
t.Errorf("PerKeyThroughput duration mismatch = want %v, got %v", tt.wantDuration, a.ClearFrequencyDuration)
}
}
})
}
}