Debian Codegraph @

api_test.go

raw
1 package circonusllhist_test
2
3 import (
4 "math"
5 "math/rand"
6 "testing"
7 "time"
8
9 hist "github.com/circonus-labs/circonusllhist"
10 )
11
12 func fuzzy_equals(expected, actual float64) bool {
13 delta := math.Abs(expected / 100000.0)
14 if actual >= expected-delta && actual <= expected+delta {
15 return true
16 }
17 return false
18 }
19
20 var s1 = []float64{0.123, 0, 0.43, 0.41, 0.415, 0.2201, 0.3201, 0.125, 0.13}
21
22 func TestDecStrings(t *testing.T) {
23 h := hist.New()
24 for _, sample := range s1 {
25 h.RecordValue(sample)
26 }
27 out := h.DecStrings()
28 expect := []string{"H[0.0e+00]=1", "H[1.2e-01]=2", "H[1.3e-01]=1",
29 "H[2.2e-01]=1", "H[3.2e-01]=1", "H[4.1e-01]=2",
30 "H[4.3e-01]=1"}
31 for i, str := range expect {
32 if str != out[i] {
33 t.Errorf("DecString '%v' != '%v'", out[i], str)
34 }
35 }
36 }
37
38 func TestNewFromStrings(t *testing.T) {
39 strings := []string{"H[0.0e+00]=1", "H[1.2e-01]=2", "H[1.3e-01]=1",
40 "H[2.2e-01]=1", "H[3.2e-01]=1", "H[4.1e-01]=2", "H[4.3e-01]=1"}
41
42 // hist of single set of strings
43 singleHist, err := hist.NewFromStrings(strings, false)
44 if err != nil {
45 t.Errorf("error creating hist from strings '%v'", err)
46 }
47
48 // hist of multiple sets of strings
49 strings = append(strings, strings...)
50 doubleHist, err := hist.NewFromStrings(strings, false)
51 if err != nil {
52 t.Errorf("error creating hist from strings '%v'", err)
53 }
54
55 // sanity check the sums are doubled
56 if singleHist.ApproxSum()*2 != doubleHist.ApproxSum() {
57 t.Error("aggregate histogram approxSum failure")
58 }
59
60 if singleHist.Equals(doubleHist) {
61 t.Error("histograms should not be equal")
62 }
63 }
64
65 func TestMean(t *testing.T) {
66 h := hist.New()
67 for _, sample := range s1 {
68 h.RecordValue(sample)
69 }
70 mean := h.ApproxMean()
71 if !fuzzy_equals(0.2444444444, mean) {
72 t.Errorf("mean() -> %v != %v", mean, 0.24444)
73 }
74 }
75
76 func helpQTest(t *testing.T, vals, qin, qexpect []float64) {
77 h := hist.New()
78 for _, sample := range vals {
79 h.RecordValue(sample)
80 }
81 qout, _ := h.ApproxQuantile(qin)
82 if len(qout) != len(qexpect) {
83 t.Errorf("wrong number of quantiles")
84 }
85 for i, q := range qout {
86 if !fuzzy_equals(qexpect[i], q) {
87 t.Errorf("q(%v) -> %v != %v", qin[i], q, qexpect[i])
88 }
89 }
90 }
91
92 func TestQuantiles(t *testing.T) {
93 helpQTest(t, []float64{1}, []float64{0, 0.25, 0.5, 1}, []float64{1, 1.025, 1.05, 1.1})
94 helpQTest(t, s1, []float64{0, 0.95, 0.99, 1.0}, []float64{0, 0.4355, 0.4391, 0.44})
95 helpQTest(t, []float64{1.0, 2.0}, []float64{0.5}, []float64{1.1})
96 helpQTest(t, []float64{1.0, 1e200}, []float64{0, 1}, []float64{1.0, 1.1})
97 helpQTest(t, []float64{1e200, 1e200, 1e200, 0, 0, 1e-20, 1e-20, 1e-20, 1e-10}, []float64{0, 1},
98 []float64{0, 1.1e-10})
99 helpQTest(t, []float64{0, 1}, []float64{0, 0.1}, []float64{0, 0})
100 }
101
102 func BenchmarkHistogramRecordValue(b *testing.B) {
103 h := hist.NewNoLocks()
104 for i := 0; i < b.N; i++ {
105 h.RecordValue(float64(i % 1000))
106 }
107 b.ReportAllocs()
108 }
109
110 func BenchmarkHistogramTypical(b *testing.B) {
111 h := hist.NewNoLocks()
112 for i := 0; i < b.N; i++ {
113 h.RecordValue(float64(i % 1000))
114 }
115 b.ReportAllocs()
116 }
117
118 func BenchmarkHistogramRecordIntScale(b *testing.B) {
119 h := hist.NewNoLocks()
120 for i := 0; i < b.N; i++ {
121 h.RecordIntScale(int64(i%90+10), (i/1000)%3)
122 }
123 b.ReportAllocs()
124 }
125
126 func BenchmarkHistogramTypicalIntScale(b *testing.B) {
127 h := hist.NewNoLocks()
128 for i := 0; i < b.N; i++ {
129 h.RecordIntScale(int64(i%90+10), (i/1000)%3)
130 }
131 b.ReportAllocs()
132 }
133
134 func BenchmarkNew(b *testing.B) {
135 b.ReportAllocs()
136
137 for i := 0; i < b.N; i++ {
138 hist.New()
139 }
140 }
141
142 func TestCompare(t *testing.T) {
143 // var h1, h2 *Bin
144 }
145
146 func TestConcurrent(t *testing.T) {
147 h := hist.New()
148 for r := 0; r < 100; r++ {
149 go func() {
150 for j := 0; j < 100; j++ {
151 for i := 50; i < 100; i++ {
152 if err := h.RecordValue(float64(i)); err != nil {
153 t.Fatal(err)
154 }
155 }
156 }
157 }()
158 }
159 }
160
161 func TestRang(t *testing.T) {
162 h1 := hist.New()
163 src := rand.NewSource(time.Now().UnixNano())
164 rnd := rand.New(src)
165 for i := 0; i < 1000000; i++ {
166 h1.RecordValue(rnd.Float64() * 10)
167 }
168 }
169
170 func TestEquals(t *testing.T) {
171 h1 := hist.New()
172 for i := 0; i < 1000000; i++ {
173 if err := h1.RecordValue(float64(i)); err != nil {
174 t.Fatal(err)
175 }
176 }
177
178 h2 := hist.New()
179 for i := 0; i < 10000; i++ {
180 if err := h1.RecordValue(float64(i)); err != nil {
181 t.Fatal(err)
182 }
183 }
184
185 if h1.Equals(h2) {
186 t.Error("Expected Histograms to not be equivalent")
187 }
188
189 h1.Reset()
190 h2.Reset()
191
192 if !h1.Equals(h2) {
193 t.Error("Expected Histograms to be equivalent")
194 }
195 }
196
197 func TestMinMaxMean(t *testing.T) {
198 const (
199 minVal = 0
200 maxVal = 1000000
201 )
202
203 h := hist.New()
204 for i := minVal; i < maxVal; i++ {
205 if err := h.RecordValue(float64(i)); err != nil {
206 t.Fatal(err)
207 }
208 }
209
210 if h.Min() > minVal {
211 t.Error("incorrect min value")
212 }
213
214 if h.Max() < maxVal {
215 t.Error("incorrect max value")
216 }
217
218 round := func(val float64) int {
219 if val < 0 {
220 return int(val - 0.5)
221 }
222 return int(val + 0.5)
223 }
224
225 if round(h.Mean()) != round(maxVal/2) {
226 t.Errorf("incorrect mean value")
227 }
228 }
229
230 func TestCopy(t *testing.T) {
231 h1 := hist.New()
232 for i := 0; i < 1000000; i++ {
233 if err := h1.RecordValue(float64(i)); err != nil {
234 t.Fatal(err)
235 }
236 }
237
238 h2 := h1.Copy()
239 if !h2.Equals(h1) {
240 t.Errorf("expected copy: %v to equal original: %v", h2, h1)
241 }
242 }
243
244 func TestFullReset(t *testing.T) {
245 h1 := hist.New()
246 for i := 0; i < 1000000; i++ {
247 if err := h1.RecordValue(float64(i)); err != nil {
248 t.Fatal(err)
249 }
250 }
251
252 h1.Reset()
253 h2 := hist.New()
254 if !h2.Equals(h1) {
255 t.Errorf("expected reset value: %v to equal new value: %v", h1, h2)
256 }
257 }
258
259 func TestMerge(t *testing.T) {
260 h1 := hist.New()
261 h2 := hist.New()
262 expect := hist.New()
263
264 // record 0-100 values in both h1 and h2.
265 for i := 0; i < 100; i++ {
266 if err := h1.RecordValues(float64(i), 1); err != nil {
267 t.Fatal(err)
268 }
269 if err := h2.RecordValues(float64(i), 2); err != nil {
270 t.Fatal(err)
271 }
272 if err := expect.RecordValues(float64(i), 3); err != nil {
273 t.Fatal(err)
274 }
275 }
276 // record 100-200 values in h1.
277 for i := 100; i < 200; i++ {
278 if err := h1.RecordValues(float64(i), 1); err != nil {
279 t.Fatal(err)
280 }
281 if err := expect.RecordValues(float64(i), 1); err != nil {
282 t.Fatal(err)
283 }
284 }
285 // record 400-600 values in h2.
286 for i := 400; i < 600; i++ {
287 if err := h2.RecordValues(float64(i), 1); err != nil {
288 t.Fatal(err)
289 }
290 if err := expect.RecordValues(float64(i), 1); err != nil {
291 t.Fatal(err)
292 }
293 }
294
295 h1.Merge(h2)
296 if !h1.Equals(expect) {
297 t.Error("Expected histograms to be equivalent")
298 }
299 }
300
301 func BenchmarkHistogramMerge(b *testing.B) {
302 b.Run("random", func(b *testing.B) {
303 rand.Seed(time.Now().UnixNano())
304 b.ReportAllocs()
305 for i := 0; i < b.N; i++ {
306 h1 := hist.New()
307 for i := 0; i < 500; i++ {
308 h1.RecordIntScale(rand.Int63n(1000), 0)
309 }
310 h2 := hist.New()
311 for i := 0; i < 500; i++ {
312 h2.RecordIntScale(rand.Int63n(1000), 0)
313 }
314 h1.Merge(h2)
315 }
316 })
317
318 b.Run("large insert", func(b *testing.B) {
319 b.ReportAllocs()
320 for i := 0; i < b.N; i++ {
321 h1 := hist.New()
322 h1.RecordIntScale(1, 0)
323 h1.RecordIntScale(1000, 0)
324 h2 := hist.New()
325 for i := 10; i < 1000; i++ {
326 h2.RecordIntScale(int64(i), 0)
327 }
328 h1.Merge(h2)
329 }
330 })
331 }