File: api_test.go

package info (click to toggle)
golang-github-circonus-labs-circonusllhist 0.0~git20191022.ec08cde-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 96 kB
  • sloc: makefile: 2
file content (331 lines) | stat: -rw-r--r-- 7,134 bytes parent folder | download
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
package circonusllhist_test

import (
	"math"
	"math/rand"
	"testing"
	"time"

	hist "github.com/circonus-labs/circonusllhist"
)

func fuzzy_equals(expected, actual float64) bool {
	delta := math.Abs(expected / 100000.0)
	if actual >= expected-delta && actual <= expected+delta {
		return true
	}
	return false
}

var s1 = []float64{0.123, 0, 0.43, 0.41, 0.415, 0.2201, 0.3201, 0.125, 0.13}

func TestDecStrings(t *testing.T) {
	h := hist.New()
	for _, sample := range s1 {
		h.RecordValue(sample)
	}
	out := h.DecStrings()
	expect := []string{"H[0.0e+00]=1", "H[1.2e-01]=2", "H[1.3e-01]=1",
		"H[2.2e-01]=1", "H[3.2e-01]=1", "H[4.1e-01]=2",
		"H[4.3e-01]=1"}
	for i, str := range expect {
		if str != out[i] {
			t.Errorf("DecString '%v' != '%v'", out[i], str)
		}
	}
}

func TestNewFromStrings(t *testing.T) {
	strings := []string{"H[0.0e+00]=1", "H[1.2e-01]=2", "H[1.3e-01]=1",
		"H[2.2e-01]=1", "H[3.2e-01]=1", "H[4.1e-01]=2", "H[4.3e-01]=1"}

	// hist of single set of strings
	singleHist, err := hist.NewFromStrings(strings, false)
	if err != nil {
		t.Errorf("error creating hist from strings '%v'", err)
	}

	// hist of multiple sets of strings
	strings = append(strings, strings...)
	doubleHist, err := hist.NewFromStrings(strings, false)
	if err != nil {
		t.Errorf("error creating hist from strings '%v'", err)
	}

	// sanity check the sums are doubled
	if singleHist.ApproxSum()*2 != doubleHist.ApproxSum() {
		t.Error("aggregate histogram approxSum failure")
	}

	if singleHist.Equals(doubleHist) {
		t.Error("histograms should not be equal")
	}
}

func TestMean(t *testing.T) {
	h := hist.New()
	for _, sample := range s1 {
		h.RecordValue(sample)
	}
	mean := h.ApproxMean()
	if !fuzzy_equals(0.2444444444, mean) {
		t.Errorf("mean() -> %v != %v", mean, 0.24444)
	}
}

func helpQTest(t *testing.T, vals, qin, qexpect []float64) {
	h := hist.New()
	for _, sample := range vals {
		h.RecordValue(sample)
	}
	qout, _ := h.ApproxQuantile(qin)
	if len(qout) != len(qexpect) {
		t.Errorf("wrong number of quantiles")
	}
	for i, q := range qout {
		if !fuzzy_equals(qexpect[i], q) {
			t.Errorf("q(%v) -> %v != %v", qin[i], q, qexpect[i])
		}
	}
}

func TestQuantiles(t *testing.T) {
	helpQTest(t, []float64{1}, []float64{0, 0.25, 0.5, 1}, []float64{1, 1.025, 1.05, 1.1})
	helpQTest(t, s1, []float64{0, 0.95, 0.99, 1.0}, []float64{0, 0.4355, 0.4391, 0.44})
	helpQTest(t, []float64{1.0, 2.0}, []float64{0.5}, []float64{1.1})
	helpQTest(t, []float64{1.0, 1e200}, []float64{0, 1}, []float64{1.0, 1.1})
	helpQTest(t, []float64{1e200, 1e200, 1e200, 0, 0, 1e-20, 1e-20, 1e-20, 1e-10}, []float64{0, 1},
		[]float64{0, 1.1e-10})
	helpQTest(t, []float64{0, 1}, []float64{0, 0.1}, []float64{0, 0})
}

func BenchmarkHistogramRecordValue(b *testing.B) {
	h := hist.NewNoLocks()
	for i := 0; i < b.N; i++ {
		h.RecordValue(float64(i % 1000))
	}
	b.ReportAllocs()
}

func BenchmarkHistogramTypical(b *testing.B) {
	h := hist.NewNoLocks()
	for i := 0; i < b.N; i++ {
		h.RecordValue(float64(i % 1000))
	}
	b.ReportAllocs()
}

func BenchmarkHistogramRecordIntScale(b *testing.B) {
	h := hist.NewNoLocks()
	for i := 0; i < b.N; i++ {
		h.RecordIntScale(int64(i%90+10), (i/1000)%3)
	}
	b.ReportAllocs()
}

func BenchmarkHistogramTypicalIntScale(b *testing.B) {
	h := hist.NewNoLocks()
	for i := 0; i < b.N; i++ {
		h.RecordIntScale(int64(i%90+10), (i/1000)%3)
	}
	b.ReportAllocs()
}

func BenchmarkNew(b *testing.B) {
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		hist.New()
	}
}

func TestCompare(t *testing.T) {
	// var h1, h2 *Bin
}

func TestConcurrent(t *testing.T) {
	h := hist.New()
	for r := 0; r < 100; r++ {
		go func() {
			for j := 0; j < 100; j++ {
				for i := 50; i < 100; i++ {
					if err := h.RecordValue(float64(i)); err != nil {
						t.Fatal(err)
					}
				}
			}
		}()
	}
}

func TestRang(t *testing.T) {
	h1 := hist.New()
	src := rand.NewSource(time.Now().UnixNano())
	rnd := rand.New(src)
	for i := 0; i < 1000000; i++ {
		h1.RecordValue(rnd.Float64() * 10)
	}
}

func TestEquals(t *testing.T) {
	h1 := hist.New()
	for i := 0; i < 1000000; i++ {
		if err := h1.RecordValue(float64(i)); err != nil {
			t.Fatal(err)
		}
	}

	h2 := hist.New()
	for i := 0; i < 10000; i++ {
		if err := h1.RecordValue(float64(i)); err != nil {
			t.Fatal(err)
		}
	}

	if h1.Equals(h2) {
		t.Error("Expected Histograms to not be equivalent")
	}

	h1.Reset()
	h2.Reset()

	if !h1.Equals(h2) {
		t.Error("Expected Histograms to be equivalent")
	}
}

func TestMinMaxMean(t *testing.T) {
	const (
		minVal = 0
		maxVal = 1000000
	)

	h := hist.New()
	for i := minVal; i < maxVal; i++ {
		if err := h.RecordValue(float64(i)); err != nil {
			t.Fatal(err)
		}
	}

	if h.Min() > minVal {
		t.Error("incorrect min value")
	}

	if h.Max() < maxVal {
		t.Error("incorrect max value")
	}

	round := func(val float64) int {
		if val < 0 {
			return int(val - 0.5)
		}
		return int(val + 0.5)
	}

	if round(h.Mean()) != round(maxVal/2) {
		t.Errorf("incorrect mean value")
	}
}

func TestCopy(t *testing.T) {
	h1 := hist.New()
	for i := 0; i < 1000000; i++ {
		if err := h1.RecordValue(float64(i)); err != nil {
			t.Fatal(err)
		}
	}

	h2 := h1.Copy()
	if !h2.Equals(h1) {
		t.Errorf("expected copy: %v to equal original: %v", h2, h1)
	}
}

func TestFullReset(t *testing.T) {
	h1 := hist.New()
	for i := 0; i < 1000000; i++ {
		if err := h1.RecordValue(float64(i)); err != nil {
			t.Fatal(err)
		}
	}

	h1.Reset()
	h2 := hist.New()
	if !h2.Equals(h1) {
		t.Errorf("expected reset value: %v to equal new value: %v", h1, h2)
	}
}

func TestMerge(t *testing.T) {
	h1 := hist.New()
	h2 := hist.New()
	expect := hist.New()

	// record 0-100 values in both h1 and h2.
	for i := 0; i < 100; i++ {
		if err := h1.RecordValues(float64(i), 1); err != nil {
			t.Fatal(err)
		}
		if err := h2.RecordValues(float64(i), 2); err != nil {
			t.Fatal(err)
		}
		if err := expect.RecordValues(float64(i), 3); err != nil {
			t.Fatal(err)
		}
	}
	// record 100-200 values in h1.
	for i := 100; i < 200; i++ {
		if err := h1.RecordValues(float64(i), 1); err != nil {
			t.Fatal(err)
		}
		if err := expect.RecordValues(float64(i), 1); err != nil {
			t.Fatal(err)
		}
	}
	// record 400-600 values in h2.
	for i := 400; i < 600; i++ {
		if err := h2.RecordValues(float64(i), 1); err != nil {
			t.Fatal(err)
		}
		if err := expect.RecordValues(float64(i), 1); err != nil {
			t.Fatal(err)
		}
	}

	h1.Merge(h2)
	if !h1.Equals(expect) {
		t.Error("Expected histograms to be equivalent")
	}
}

func BenchmarkHistogramMerge(b *testing.B) {
	b.Run("random", func(b *testing.B) {
		rand.Seed(time.Now().UnixNano())
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			h1 := hist.New()
			for i := 0; i < 500; i++ {
				h1.RecordIntScale(rand.Int63n(1000), 0)
			}
			h2 := hist.New()
			for i := 0; i < 500; i++ {
				h2.RecordIntScale(rand.Int63n(1000), 0)
			}
			h1.Merge(h2)
		}
	})

	b.Run("large insert", func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			h1 := hist.New()
			h1.RecordIntScale(1, 0)
			h1.RecordIntScale(1000, 0)
			h2 := hist.New()
			for i := 10; i < 1000; i++ {
				h2.RecordIntScale(int64(i), 0)
			}
			h1.Merge(h2)
		}
	})
}