/
worker_test.go
289 lines (254 loc) · 7.18 KB
/
worker_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
package statsd
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldSample(t *testing.T) {
rates := []float64{0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99, 1.0}
iterations := 50_000
for _, rate := range rates {
rate := rate // Capture range variable.
t.Run(fmt.Sprintf("Rate %0.2f", rate), func(t *testing.T) {
t.Parallel()
worker := newWorker(newBufferPool(1, 1, 1), nil)
count := 0
for i := 0; i < iterations; i++ {
if worker.shouldSample(rate) {
count++
}
}
assert.InDelta(t, rate, float64(count)/float64(iterations), 0.01)
})
}
}
func BenchmarkShouldSample(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
worker := newWorker(newBufferPool(1, 1, 1), nil)
for pb.Next() {
worker.shouldSample(0.1)
}
})
}
func initWorker(bufferSize int) (*bufferPool, *sender, *worker) {
pool := newBufferPool(10, bufferSize, 5)
// manually create the sender so the sender loop is not started. All we
// need is the queue
s := &sender{
queue: make(chan *statsdBuffer, 10),
pool: pool,
}
w := newWorker(pool, s)
return pool, s, w
}
func testWorker(t *testing.T, m metric, expectedBuffer string) {
_, s, w := initWorker(100)
err := w.processMetric(m)
assert.Nil(t, err)
w.flush()
data := <-s.queue
assert.Equal(t, expectedBuffer, string(data.buffer))
}
func TestWorkerGauge(t *testing.T) {
testWorker(
t,
metric{
metricType: gauge,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_gauge",
fvalue: 21,
tags: []string{"tag1", "tag2"},
rate: 1,
},
"namespace.test_gauge:21|g|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerCount(t *testing.T) {
testWorker(
t,
metric{
metricType: count,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_count",
ivalue: 21,
tags: []string{"tag1", "tag2"},
rate: 1,
},
"namespace.test_count:21|c|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerHistogram(t *testing.T) {
testWorker(
t,
metric{
metricType: histogram,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_histogram",
fvalue: 21,
tags: []string{"tag1", "tag2"},
rate: 1,
},
"namespace.test_histogram:21|h|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerDistribution(t *testing.T) {
testWorker(
t,
metric{
metricType: distribution,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_distribution",
fvalue: 21,
tags: []string{"tag1", "tag2"},
rate: 1,
},
"namespace.test_distribution:21|d|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerSet(t *testing.T) {
testWorker(
t,
metric{
metricType: set,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_set",
svalue: "value:1",
tags: []string{"tag1", "tag2"},
rate: 1,
},
"namespace.test_set:value:1|s|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerTiming(t *testing.T) {
testWorker(
t,
metric{
metricType: timing,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_timing",
fvalue: 1.2,
tags: []string{"tag1", "tag2"},
rate: 1,
},
"namespace.test_timing:1.200000|ms|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerHistogramAggregated(t *testing.T) {
testWorker(
t,
metric{
metricType: histogramAggregated,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_histogram",
fvalues: []float64{1.2},
stags: "tag1,tag2",
rate: 1,
},
"namespace.test_histogram:1.2|h|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerHistogramAggregatedMultiple(t *testing.T) {
_, s, w := initWorker(100)
m := metric{
metricType: histogramAggregated,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_histogram",
fvalues: []float64{1.1, 2.2, 3.3, 4.4},
stags: "tag1,tag2",
rate: 1,
}
err := w.processMetric(m)
assert.Nil(t, err)
w.flush()
data := <-s.queue
assert.Equal(t, "namespace.test_histogram:1.1:2.2:3.3:4.4|h|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
// reducing buffer size so not all values fit in one packet
_, s, w = initWorker(70)
err = w.processMetric(m)
assert.Nil(t, err)
w.flush()
data = <-s.queue
assert.Equal(t, "namespace.test_histogram:1.1:2.2|h|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
data = <-s.queue
assert.Equal(t, "namespace.test_histogram:3.3:4.4|h|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
}
func TestWorkerDistributionAggregated(t *testing.T) {
testWorker(
t,
metric{
metricType: distributionAggregated,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_distribution",
fvalues: []float64{1.2},
stags: "tag1,tag2",
rate: 1,
},
"namespace.test_distribution:1.2|d|#globalTags,globalTags2,tag1,tag2",
)
}
func TestWorkerDistributionAggregatedMultiple(t *testing.T) {
_, s, w := initWorker(100)
m := metric{
metricType: distributionAggregated,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_distribution",
fvalues: []float64{1.1, 2.2, 3.3, 4.4},
stags: "tag1,tag2",
rate: 1,
}
err := w.processMetric(m)
assert.Nil(t, err)
w.flush()
data := <-s.queue
assert.Equal(t, "namespace.test_distribution:1.1:2.2:3.3:4.4|d|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
// reducing buffer size so not all values fit in one packet
_, s, w = initWorker(72)
err = w.processMetric(m)
assert.Nil(t, err)
w.flush()
data = <-s.queue
assert.Equal(t, "namespace.test_distribution:1.1:2.2|d|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
data = <-s.queue
assert.Equal(t, "namespace.test_distribution:3.3:4.4|d|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
}
func TestWorkerMultipleDifferentDistributionAggregated(t *testing.T) {
// first metric will fit but not the second one
_, s, w := initWorker(160)
m := metric{
metricType: distributionAggregated,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_distribution",
fvalues: []float64{1.1, 2.2, 3.3, 4.4},
stags: "tag1,tag2",
rate: 1,
}
err := w.processMetric(m)
assert.Nil(t, err)
m = metric{
metricType: distributionAggregated,
namespace: "namespace.",
globalTags: []string{"globalTags", "globalTags2"},
name: "test_distribution_2",
fvalues: []float64{1.1, 2.2, 3.3, 4.4},
stags: "tag1,tag2",
rate: 1,
}
err = w.processMetric(m)
assert.Nil(t, err)
w.flush()
data := <-s.queue
assert.Equal(t, "namespace.test_distribution:1.1:2.2:3.3:4.4|d|#globalTags,globalTags2,tag1,tag2\nnamespace.test_distribution_2:1.1:2.2:3.3|d|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
data = <-s.queue
assert.Equal(t, "namespace.test_distribution_2:4.4|d|#globalTags,globalTags2,tag1,tag2", string(data.buffer))
}