OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / codahale / hdrhistogram / hdr_test.go
1 package hdrhistogram_test
2
3 import (
4         "math"
5         "reflect"
6         "testing"
7
8         "github.com/codahale/hdrhistogram"
9 )
10
11 func TestHighSigFig(t *testing.T) {
12         input := []int64{
13                 459876, 669187, 711612, 816326, 931423, 1033197, 1131895, 2477317,
14                 3964974, 12718782,
15         }
16
17         hist := hdrhistogram.New(459876, 12718782, 5)
18         for _, sample := range input {
19                 hist.RecordValue(sample)
20         }
21
22         if v, want := hist.ValueAtQuantile(50), int64(1048575); v != want {
23                 t.Errorf("Median was %v, but expected %v", v, want)
24         }
25 }
26
27 func TestValueAtQuantile(t *testing.T) {
28         h := hdrhistogram.New(1, 10000000, 3)
29
30         for i := 0; i < 1000000; i++ {
31                 if err := h.RecordValue(int64(i)); err != nil {
32                         t.Fatal(err)
33                 }
34         }
35
36         data := []struct {
37                 q float64
38                 v int64
39         }{
40                 {q: 50, v: 500223},
41                 {q: 75, v: 750079},
42                 {q: 90, v: 900095},
43                 {q: 95, v: 950271},
44                 {q: 99, v: 990207},
45                 {q: 99.9, v: 999423},
46                 {q: 99.99, v: 999935},
47         }
48
49         for _, d := range data {
50                 if v := h.ValueAtQuantile(d.q); v != d.v {
51                         t.Errorf("P%v was %v, but expected %v", d.q, v, d.v)
52                 }
53         }
54 }
55
56 func TestMean(t *testing.T) {
57         h := hdrhistogram.New(1, 10000000, 3)
58
59         for i := 0; i < 1000000; i++ {
60                 if err := h.RecordValue(int64(i)); err != nil {
61                         t.Fatal(err)
62                 }
63         }
64
65         if v, want := h.Mean(), 500000.013312; v != want {
66                 t.Errorf("Mean was %v, but expected %v", v, want)
67         }
68 }
69
70 func TestStdDev(t *testing.T) {
71         h := hdrhistogram.New(1, 10000000, 3)
72
73         for i := 0; i < 1000000; i++ {
74                 if err := h.RecordValue(int64(i)); err != nil {
75                         t.Fatal(err)
76                 }
77         }
78
79         if v, want := h.StdDev(), 288675.1403682715; v != want {
80                 t.Errorf("StdDev was %v, but expected %v", v, want)
81         }
82 }
83
84 func TestTotalCount(t *testing.T) {
85         h := hdrhistogram.New(1, 10000000, 3)
86
87         for i := 0; i < 1000000; i++ {
88                 if err := h.RecordValue(int64(i)); err != nil {
89                         t.Fatal(err)
90                 }
91                 if v, want := h.TotalCount(), int64(i+1); v != want {
92                         t.Errorf("TotalCount was %v, but expected %v", v, want)
93                 }
94         }
95 }
96
97 func TestMax(t *testing.T) {
98         h := hdrhistogram.New(1, 10000000, 3)
99
100         for i := 0; i < 1000000; i++ {
101                 if err := h.RecordValue(int64(i)); err != nil {
102                         t.Fatal(err)
103                 }
104         }
105
106         if v, want := h.Max(), int64(1000447); v != want {
107                 t.Errorf("Max was %v, but expected %v", v, want)
108         }
109 }
110
111 func TestReset(t *testing.T) {
112         h := hdrhistogram.New(1, 10000000, 3)
113
114         for i := 0; i < 1000000; i++ {
115                 if err := h.RecordValue(int64(i)); err != nil {
116                         t.Fatal(err)
117                 }
118         }
119
120         h.Reset()
121
122         if v, want := h.Max(), int64(0); v != want {
123                 t.Errorf("Max was %v, but expected %v", v, want)
124         }
125 }
126
127 func TestMerge(t *testing.T) {
128         h1 := hdrhistogram.New(1, 1000, 3)
129         h2 := hdrhistogram.New(1, 1000, 3)
130
131         for i := 0; i < 100; i++ {
132                 if err := h1.RecordValue(int64(i)); err != nil {
133                         t.Fatal(err)
134                 }
135         }
136
137         for i := 100; i < 200; i++ {
138                 if err := h2.RecordValue(int64(i)); err != nil {
139                         t.Fatal(err)
140                 }
141         }
142
143         h1.Merge(h2)
144
145         if v, want := h1.ValueAtQuantile(50), int64(99); v != want {
146                 t.Errorf("Median was %v, but expected %v", v, want)
147         }
148 }
149
150 func TestMin(t *testing.T) {
151         h := hdrhistogram.New(1, 10000000, 3)
152
153         for i := 0; i < 1000000; i++ {
154                 if err := h.RecordValue(int64(i)); err != nil {
155                         t.Fatal(err)
156                 }
157         }
158
159         if v, want := h.Min(), int64(0); v != want {
160                 t.Errorf("Min was %v, but expected %v", v, want)
161         }
162 }
163
164 func TestByteSize(t *testing.T) {
165         h := hdrhistogram.New(1, 100000, 3)
166
167         if v, want := h.ByteSize(), 65604; v != want {
168                 t.Errorf("ByteSize was %v, but expected %d", v, want)
169         }
170 }
171
172 func TestRecordCorrectedValue(t *testing.T) {
173         h := hdrhistogram.New(1, 100000, 3)
174
175         if err := h.RecordCorrectedValue(10, 100); err != nil {
176                 t.Fatal(err)
177         }
178
179         if v, want := h.ValueAtQuantile(75), int64(10); v != want {
180                 t.Errorf("Corrected value was %v, but expected %v", v, want)
181         }
182 }
183
184 func TestRecordCorrectedValueStall(t *testing.T) {
185         h := hdrhistogram.New(1, 100000, 3)
186
187         if err := h.RecordCorrectedValue(1000, 100); err != nil {
188                 t.Fatal(err)
189         }
190
191         if v, want := h.ValueAtQuantile(75), int64(800); v != want {
192                 t.Errorf("Corrected value was %v, but expected %v", v, want)
193         }
194 }
195
196 func TestCumulativeDistribution(t *testing.T) {
197         h := hdrhistogram.New(1, 100000000, 3)
198
199         for i := 0; i < 1000000; i++ {
200                 if err := h.RecordValue(int64(i)); err != nil {
201                         t.Fatal(err)
202                 }
203         }
204
205         actual := h.CumulativeDistribution()
206         expected := []hdrhistogram.Bracket{
207                 hdrhistogram.Bracket{Quantile: 0, Count: 1, ValueAt: 0},
208                 hdrhistogram.Bracket{Quantile: 50, Count: 500224, ValueAt: 500223},
209                 hdrhistogram.Bracket{Quantile: 75, Count: 750080, ValueAt: 750079},
210                 hdrhistogram.Bracket{Quantile: 87.5, Count: 875008, ValueAt: 875007},
211                 hdrhistogram.Bracket{Quantile: 93.75, Count: 937984, ValueAt: 937983},
212                 hdrhistogram.Bracket{Quantile: 96.875, Count: 969216, ValueAt: 969215},
213                 hdrhistogram.Bracket{Quantile: 98.4375, Count: 984576, ValueAt: 984575},
214                 hdrhistogram.Bracket{Quantile: 99.21875, Count: 992256, ValueAt: 992255},
215                 hdrhistogram.Bracket{Quantile: 99.609375, Count: 996352, ValueAt: 996351},
216                 hdrhistogram.Bracket{Quantile: 99.8046875, Count: 998400, ValueAt: 998399},
217                 hdrhistogram.Bracket{Quantile: 99.90234375, Count: 999424, ValueAt: 999423},
218                 hdrhistogram.Bracket{Quantile: 99.951171875, Count: 999936, ValueAt: 999935},
219                 hdrhistogram.Bracket{Quantile: 99.9755859375, Count: 999936, ValueAt: 999935},
220                 hdrhistogram.Bracket{Quantile: 99.98779296875, Count: 999936, ValueAt: 999935},
221                 hdrhistogram.Bracket{Quantile: 99.993896484375, Count: 1000000, ValueAt: 1000447},
222                 hdrhistogram.Bracket{Quantile: 100, Count: 1000000, ValueAt: 1000447},
223         }
224
225         if !reflect.DeepEqual(actual, expected) {
226                 t.Errorf("CF was %#v, but expected %#v", actual, expected)
227         }
228 }
229
230 func TestDistribution(t *testing.T) {
231         h := hdrhistogram.New(8, 1024, 3)
232
233         for i := 0; i < 1024; i++ {
234                 if err := h.RecordValue(int64(i)); err != nil {
235                         t.Fatal(err)
236                 }
237         }
238
239         actual := h.Distribution()
240         if len(actual) != 128 {
241                 t.Errorf("Number of bars seen was %v, expected was 128", len(actual))
242         }
243         for _, b := range actual {
244                 if b.Count != 8 {
245                         t.Errorf("Count per bar seen was %v, expected was 8", b.Count)
246                 }
247         }
248 }
249
250 func TestNaN(t *testing.T) {
251         h := hdrhistogram.New(1, 100000, 3)
252         if math.IsNaN(h.Mean()) {
253                 t.Error("mean is NaN")
254         }
255         if math.IsNaN(h.StdDev()) {
256                 t.Error("stddev is NaN")
257         }
258 }
259
260 func TestSignificantFigures(t *testing.T) {
261         const sigFigs = 4
262         h := hdrhistogram.New(1, 10, sigFigs)
263         if h.SignificantFigures() != sigFigs {
264                 t.Errorf("Significant figures was %v, expected %d", h.SignificantFigures(), sigFigs)
265         }
266 }
267
268 func TestLowestTrackableValue(t *testing.T) {
269         const minVal = 2
270         h := hdrhistogram.New(minVal, 10, 3)
271         if h.LowestTrackableValue() != minVal {
272                 t.Errorf("LowestTrackableValue figures was %v, expected %d", h.LowestTrackableValue(), minVal)
273         }
274 }
275
276 func TestHighestTrackableValue(t *testing.T) {
277         const maxVal = 11
278         h := hdrhistogram.New(1, maxVal, 3)
279         if h.HighestTrackableValue() != maxVal {
280                 t.Errorf("HighestTrackableValue figures was %v, expected %d", h.HighestTrackableValue(), maxVal)
281         }
282 }
283
284 func BenchmarkHistogramRecordValue(b *testing.B) {
285         h := hdrhistogram.New(1, 10000000, 3)
286         for i := 0; i < 1000000; i++ {
287                 if err := h.RecordValue(int64(i)); err != nil {
288                         b.Fatal(err)
289                 }
290         }
291         b.ResetTimer()
292         b.ReportAllocs()
293
294         for i := 0; i < b.N; i++ {
295                 h.RecordValue(100)
296         }
297 }
298
299 func BenchmarkNew(b *testing.B) {
300         b.ReportAllocs()
301
302         for i := 0; i < b.N; i++ {
303                 hdrhistogram.New(1, 120000, 3) // this could track 1ms-2min
304         }
305 }
306
307 func TestUnitMagnitudeOverflow(t *testing.T) {
308         h := hdrhistogram.New(0, 200, 4)
309         if err := h.RecordValue(11); err != nil {
310                 t.Fatal(err)
311         }
312 }
313
314 func TestSubBucketMaskOverflow(t *testing.T) {
315         hist := hdrhistogram.New(2e7, 1e8, 5)
316         for _, sample := range [...]int64{1e8, 2e7, 3e7} {
317                 hist.RecordValue(sample)
318         }
319
320         for q, want := range map[float64]int64{
321                 50:    33554431,
322                 83.33: 33554431,
323                 83.34: 100663295,
324                 99:    100663295,
325         } {
326                 if got := hist.ValueAtQuantile(q); got != want {
327                         t.Errorf("got %d for %fth percentile. want: %d", got, q, want)
328                 }
329         }
330 }
331
332 func TestExportImport(t *testing.T) {
333         min := int64(1)
334         max := int64(10000000)
335         sigfigs := 3
336         h := hdrhistogram.New(min, max, sigfigs)
337         for i := 0; i < 1000000; i++ {
338                 if err := h.RecordValue(int64(i)); err != nil {
339                         t.Fatal(err)
340                 }
341         }
342
343         s := h.Export()
344
345         if v := s.LowestTrackableValue; v != min {
346                 t.Errorf("LowestTrackableValue was %v, but expected %v", v, min)
347         }
348
349         if v := s.HighestTrackableValue; v != max {
350                 t.Errorf("HighestTrackableValue was %v, but expected %v", v, max)
351         }
352
353         if v := int(s.SignificantFigures); v != sigfigs {
354                 t.Errorf("SignificantFigures was %v, but expected %v", v, sigfigs)
355         }
356
357         if imported := hdrhistogram.Import(s); !imported.Equals(h) {
358                 t.Error("Expected Histograms to be equivalent")
359         }
360
361 }
362
363 func TestEquals(t *testing.T) {
364         h1 := hdrhistogram.New(1, 10000000, 3)
365         for i := 0; i < 1000000; i++ {
366                 if err := h1.RecordValue(int64(i)); err != nil {
367                         t.Fatal(err)
368                 }
369         }
370
371         h2 := hdrhistogram.New(1, 10000000, 3)
372         for i := 0; i < 10000; i++ {
373                 if err := h1.RecordValue(int64(i)); err != nil {
374                         t.Fatal(err)
375                 }
376         }
377
378         if h1.Equals(h2) {
379                 t.Error("Expected Histograms to not be equivalent")
380         }
381
382         h1.Reset()
383         h2.Reset()
384
385         if !h1.Equals(h2) {
386                 t.Error("Expected Histograms to be equivalent")
387         }
388 }