-
Notifications
You must be signed in to change notification settings - Fork 18
/
circonusllhist_test.go
414 lines (368 loc) · 10.6 KB
/
circonusllhist_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
package circonusllhist
import (
"bytes"
"encoding/json"
"fmt"
"math"
"math/rand"
"reflect"
"strings"
"testing"
"time"
)
func TestCreate(t *testing.T) {
h := New()
/*
for j := 0; j < 100000; j++ {
h.RecordIntScale(rand.Intn(1000), 0)
}
*/
_ = h.RecordIntScales(99, 0, int64(rand.Intn(2))+1)
buf := bytes.NewBuffer([]byte{})
if err := h.Serialize(buf); err != nil {
t.Error(err)
}
h2, err := Deserialize(buf)
if err != nil {
t.Error(err)
}
for j := uint16(0); j < h2.used; j++ {
if h2.bvs[j].exp < 1 && (h2.bvs[j].val%10) != 0 {
t.Errorf("bad bin[%v] %ve%v", j, float64(h2.bvs[j].val)/10.0, h2.bvs[j].exp)
}
}
}
func TestSerialize(t *testing.T) {
h, err := NewFromStrings([]string{
"H[0.0e+00]=1",
"H[1.0e+01]=1",
"H[2.0e+02]=1",
}, false)
if err != nil {
t.Error("could not read from strings for test")
}
buf := bytes.NewBuffer([]byte{})
if err = h.Serialize(buf); err != nil {
t.Error(err)
}
h2, err := Deserialize(buf)
if err != nil {
t.Error(h2, err)
}
if !h.Equals(h2) {
t.Log(h.DecStrings())
t.Log(h2.DecStrings())
t.Error("histograms do not match")
}
}
func TestCount(t *testing.T) {
h, err := NewFromStrings([]string{
"H[0.0e+00]=1",
"H[1.0e+01]=1",
"H[2.0e+02]=1",
}, true)
if err != nil {
t.Error("could not read from strings for test")
}
if h.Count() != 3 {
t.Error("the count is incorrect")
}
err = h.RecordValue(10)
if err != nil {
t.Error("could not record new value to histogram")
}
if h.Count() != 4 {
t.Error("the count is incorrect")
}
}
func TestBinCount(t *testing.T) {
h, err := NewFromStrings([]string{
"H[0.0e+00]=1",
"H[1.0e+01]=1",
"H[2.0e+02]=1",
}, true)
if err != nil {
t.Error("could not read from strings for test")
}
if h.BinCount() != 3 {
t.Error("bin count is incorrect")
}
}
func TestJSON(t *testing.T) {
h, err := NewFromStrings([]string{
"H[0.0e+00]=1",
"H[1.0e+01]=1",
"H[2.0e+02]=1",
}, false)
if err != nil {
t.Errorf("could not read from strings for test error = %v", err)
}
jh, err := json.Marshal(h)
if err != nil {
t.Errorf("could not marshall json for test error = %v", err)
}
h2 := &Histogram{}
if err := json.Unmarshal(jh, h2); err != nil {
t.Errorf("could not unmarshall json for test error = %v", err)
}
if !h.Equals(h2) {
t.Log(h.DecStrings())
t.Log(h2.DecStrings())
t.Error("histograms do not match")
}
}
func helpTestBin(t *testing.T, v float64, val, exp int8) {
b := newBinFromFloat64(v)
if b.val != val || b.exp != exp {
t.Errorf("%v -> [%v,%v] expected, but got [%v,%v]", v, val, exp, b.val, b.exp)
}
}
func fuzzyEquals(expected, actual float64) bool {
delta := math.Abs(expected / 100000.0)
if actual >= expected-delta && actual <= expected+delta {
return true
}
return false
}
func TestBins(t *testing.T) {
helpTestBin(t, 0.0, 0, 0)
helpTestBin(t, 100, 10, 2)
helpTestBin(t, 9.9999e-129, 0, 0)
helpTestBin(t, 1e-128, 10, -128)
helpTestBin(t, 1.00001e-128, 10, -128)
helpTestBin(t, 1.09999e-128, 10, -128)
helpTestBin(t, 1.1e-128, 11, -128)
helpTestBin(t, 1e127, 10, 127)
helpTestBin(t, 9.999e127, 99, 127)
helpTestBin(t, 1e128, -1, 0)
helpTestBin(t, -9.9999e-129, 0, 0)
helpTestBin(t, -1e-128, -10, -128)
helpTestBin(t, -1.00001e-128, -10, -128)
helpTestBin(t, -1.09999e-128, -10, -128)
helpTestBin(t, -1.1e-128, -11, -128)
helpTestBin(t, -1e127, -10, 127)
helpTestBin(t, -9.999e127, -99, 127)
helpTestBin(t, -1e128, -1, 0)
helpTestBin(t, 9.999e127, 99, 127)
h := New()
_ = h.RecordIntScale(100, 0)
if h.bvs[0].val != 10 || h.bvs[0].exp != 2 {
t.Errorf("100 not added correctly")
}
h = New()
_ = h.RecordValue(100.0)
if h.bvs[0].val != 10 || h.bvs[0].exp != 2 {
t.Errorf("100.0 not added correctly")
}
}
func TestRecordDuration(t *testing.T) {
tests := []struct {
input []time.Duration
inputUnit time.Duration
approxSum time.Duration
approxMean time.Duration
tolerance time.Duration
}{
{
input: []time.Duration{time.Nanosecond},
approxSum: time.Nanosecond,
approxMean: time.Nanosecond,
},
{
input: []time.Duration{3 * time.Nanosecond},
approxSum: 3 * time.Nanosecond,
approxMean: 3 * time.Nanosecond,
},
{
input: []time.Duration{1000 * time.Second},
approxSum: 1000 * time.Second,
approxMean: 1000 * time.Second,
},
{
input: []time.Duration{
4 * time.Second,
8 * time.Second,
},
approxSum: 12.0 * time.Second,
approxMean: 6.0 * time.Second,
},
}
fuzzyEquals := func(expected, actual time.Duration) bool {
diff := math.Abs(float64(expected) - float64(actual))
return (diff / math.Max(float64(expected), float64(actual))) <= 0.05
}
for n, test := range tests {
test := test
t.Run(fmt.Sprintf("%d", n), func(t *testing.T) {
h := New()
for _, dur := range test.input {
_ = h.RecordDuration(dur)
}
if v := time.Duration(1000000000.0 * h.ApproxSum()); !fuzzyEquals(v, test.approxSum) {
t.Fatalf("%v approx sum bad: have=%v want=%v", test.input, h.ApproxSum(), test.approxSum)
}
if v := time.Duration(1000000000.0 * h.ApproxMean()); !fuzzyEquals(v, test.approxMean) {
t.Fatalf("%v approx mean bad: have=%v want=%v", test.input, v, test.approxMean)
}
})
}
}
func helpTestVB(t *testing.T, v, b, w float64) {
bin := newBinFromFloat64(v)
out := bin.value()
interval := bin.binWidth()
if out < 0 {
interval *= -1.0
}
if !fuzzyEquals(b, out) {
t.Errorf("%v -> %v != %v\n", v, out, b)
}
if !fuzzyEquals(w, interval) {
t.Errorf("%v -> [%v] != [%v]\n", v, interval, w)
}
}
func TestBinSizes(t *testing.T) {
helpTestVB(t, 43.3, 43.0, 1.0)
helpTestVB(t, 99.9, 99.0, 1.0)
helpTestVB(t, 10.0, 10.0, 1.0)
helpTestVB(t, 1.0, 1.0, 0.1)
helpTestVB(t, 0.0002, 0.0002, 0.00001)
helpTestVB(t, 0.003, 0.003, 0.0001)
helpTestVB(t, 0.3201, 0.32, 0.01)
helpTestVB(t, 0.0035, 0.0035, 0.0001)
helpTestVB(t, -1.0, -1.0, -0.1)
helpTestVB(t, -0.00123, -0.0012, -0.0001)
helpTestVB(t, -987324, -980000, -10000)
}
// preloadedTester knows how to preload values, then use them to benchmark a histogram.
type preloadedTester interface {
preload(n int)
run(histogram *Histogram) error
}
// intScale knows how to benchmark RecordIntScale.
type intScale struct {
// integers hold the integers we will feed RecordIntScale
integers []int64
// scales hold the scales we will feed RecordIntScale
scales []int
// scale is the scale of the distribution of values - this allows the benchmark
// to tease apart differences in the usage of a histogram in different applications
// where it may be storing fairly homogenous values or any value whatsoever
scale int
n int
}
func (t *intScale) preload(n int) {
t.n = 0
t.integers = make([]int64, n)
t.scales = make([]int, n)
scaleMin := rand.Intn(math.MaxInt64 - t.scale)
for i := 0; i < n; i++ {
t.integers[i] = rand.Int63() * (rand.Int63n(2) - 1) // allow negatives!
t.scales[i] = rand.Intn(t.scale) + scaleMin
}
}
func (t *intScale) run(histogram *Histogram) error {
n := t.n
t.n++
return histogram.RecordIntScale(t.integers[n], t.scales[n])
}
// value knows how to benchmark RecordValue.
type value struct {
// values hold the integers we will feed RecordValue
values []float64
// stddev is the standard deviation of the distribution of values - this allows the
// benchmark to tease apart differences in the usage of a histogram in different
// applications where it may be storing fairly homogenous values or any value whatsoever
stddev float64
n int
}
func (t *value) preload(n int) {
t.n = 0
t.values = make([]float64, n)
mean := float64(rand.Int63() * (rand.Int63n(2) - 1)) // allow negatives!
for i := 0; i < n; i++ {
t.values[i] = rand.NormFloat64()*t.stddev + mean
}
}
func (t *value) run(histogram *Histogram) error {
n := t.n
t.n++
return histogram.RecordValue(t.values[n])
}
func BenchmarkRecord(b *testing.B) {
benchmarkForHist(b, func() *Histogram {
return New()
})
}
func BenchmarkRecordWithoutLookups(b *testing.B) {
benchmarkForHist(b, func() *Histogram {
return New(NoLookup())
})
}
func benchmarkForHist(b *testing.B, constructor func() *Histogram) {
rand.Seed(time.Now().UnixNano())
for _, scale := range []int{1, 2, 4, 8, 16, 32, 64} {
for _, tester := range []preloadedTester{
&intScale{scale: scale},
&value{stddev: math.Pow10(scale)},
} {
name := fmt.Sprintf("%T", tester)
b.Run(fmt.Sprintf("%s_%d", name[strings.Index(name, ".")+1:], scale), func(b *testing.B) {
histogram := constructor()
tester.preload(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := tester.run(histogram); err != nil {
b.Error(err)
}
}
})
}
}
}
// TestCustomRoundTripping tests that clients using the HistogramWithoutLookups
// structure for custom serialization and deserialization get interchangeable
// behavior with the default spec.
func TestCustomRoundTripping(t *testing.T) {
h := New()
rand.Seed(time.Now().UnixNano())
for i := 0; i < 100; i++ {
if err := h.RecordIntScale(rand.Int63(), rand.Int()); err != nil {
t.Fatalf("could not record numeric value: %v", err)
}
}
defaultBytes, err := json.Marshal(h)
if err != nil {
t.Fatalf("could not marshal histogram: %v", err)
}
withoutLookupBytes, err := json.Marshal(&HistogramWithoutLookups{histogram: h})
if err != nil {
t.Fatalf("could not marshal histogram: %v", err)
}
if !reflect.DeepEqual(defaultBytes, withoutLookupBytes) {
t.Fatalf("histogram without lookups serialized into something different than default: expected %v, got %v", defaultBytes, withoutLookupBytes)
}
for source, data := range map[string][]byte{
"default": defaultBytes,
"withoutLookups": withoutLookupBytes,
} {
var deserializedWithoutLookups HistogramWithoutLookups
if err := json.Unmarshal(data, &deserializedWithoutLookups); err != nil {
t.Fatalf("could not deserialize %s bytes into custom struct: %v", source, err)
}
if deserializedWithoutLookups.histogram.useLookup != false || deserializedWithoutLookups.histogram.lookup != nil {
t.Errorf("after deserializing %s bytes into custom struct, got allocated lookup table", source)
}
extracted := deserializedWithoutLookups.HistogramWithLookups()
if extracted.useLookup != true || len(extracted.lookup) != 256 {
t.Errorf("after deserializing %s bytes into cutom struct and extracting with lookups, did not get allocated lookup table", source)
}
var deserializedDefault Histogram
if err := json.Unmarshal(data, &deserializedDefault); err != nil {
t.Fatalf("could not deserialize %s bytes into default struct: %v", source, err)
}
if deserializedDefault.useLookup != true || len(deserializedDefault.lookup) != 256 {
t.Errorf("after deserializing %s bytes into default struct, did not get allocated lookup table", source)
}
}
}