1 package hdrhistogram_test
8 "github.com/codahale/hdrhistogram"
11 func TestHighSigFig(t *testing.T) {
13 459876, 669187, 711612, 816326, 931423, 1033197, 1131895, 2477317,
17 hist := hdrhistogram.New(459876, 12718782, 5)
18 for _, sample := range input {
19 hist.RecordValue(sample)
22 if v, want := hist.ValueAtQuantile(50), int64(1048575); v != want {
23 t.Errorf("Median was %v, but expected %v", v, want)
27 func TestValueAtQuantile(t *testing.T) {
28 h := hdrhistogram.New(1, 10000000, 3)
30 for i := 0; i < 1000000; i++ {
31 if err := h.RecordValue(int64(i)); err != nil {
46 {q: 99.99, v: 999935},
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)
56 func TestMean(t *testing.T) {
57 h := hdrhistogram.New(1, 10000000, 3)
59 for i := 0; i < 1000000; i++ {
60 if err := h.RecordValue(int64(i)); err != nil {
65 if v, want := h.Mean(), 500000.013312; v != want {
66 t.Errorf("Mean was %v, but expected %v", v, want)
70 func TestStdDev(t *testing.T) {
71 h := hdrhistogram.New(1, 10000000, 3)
73 for i := 0; i < 1000000; i++ {
74 if err := h.RecordValue(int64(i)); err != nil {
79 if v, want := h.StdDev(), 288675.1403682715; v != want {
80 t.Errorf("StdDev was %v, but expected %v", v, want)
84 func TestTotalCount(t *testing.T) {
85 h := hdrhistogram.New(1, 10000000, 3)
87 for i := 0; i < 1000000; i++ {
88 if err := h.RecordValue(int64(i)); err != nil {
91 if v, want := h.TotalCount(), int64(i+1); v != want {
92 t.Errorf("TotalCount was %v, but expected %v", v, want)
97 func TestMax(t *testing.T) {
98 h := hdrhistogram.New(1, 10000000, 3)
100 for i := 0; i < 1000000; i++ {
101 if err := h.RecordValue(int64(i)); err != nil {
106 if v, want := h.Max(), int64(1000447); v != want {
107 t.Errorf("Max was %v, but expected %v", v, want)
111 func TestReset(t *testing.T) {
112 h := hdrhistogram.New(1, 10000000, 3)
114 for i := 0; i < 1000000; i++ {
115 if err := h.RecordValue(int64(i)); err != nil {
122 if v, want := h.Max(), int64(0); v != want {
123 t.Errorf("Max was %v, but expected %v", v, want)
127 func TestMerge(t *testing.T) {
128 h1 := hdrhistogram.New(1, 1000, 3)
129 h2 := hdrhistogram.New(1, 1000, 3)
131 for i := 0; i < 100; i++ {
132 if err := h1.RecordValue(int64(i)); err != nil {
137 for i := 100; i < 200; i++ {
138 if err := h2.RecordValue(int64(i)); err != nil {
145 if v, want := h1.ValueAtQuantile(50), int64(99); v != want {
146 t.Errorf("Median was %v, but expected %v", v, want)
150 func TestMin(t *testing.T) {
151 h := hdrhistogram.New(1, 10000000, 3)
153 for i := 0; i < 1000000; i++ {
154 if err := h.RecordValue(int64(i)); err != nil {
159 if v, want := h.Min(), int64(0); v != want {
160 t.Errorf("Min was %v, but expected %v", v, want)
164 func TestByteSize(t *testing.T) {
165 h := hdrhistogram.New(1, 100000, 3)
167 if v, want := h.ByteSize(), 65604; v != want {
168 t.Errorf("ByteSize was %v, but expected %d", v, want)
172 func TestRecordCorrectedValue(t *testing.T) {
173 h := hdrhistogram.New(1, 100000, 3)
175 if err := h.RecordCorrectedValue(10, 100); err != nil {
179 if v, want := h.ValueAtQuantile(75), int64(10); v != want {
180 t.Errorf("Corrected value was %v, but expected %v", v, want)
184 func TestRecordCorrectedValueStall(t *testing.T) {
185 h := hdrhistogram.New(1, 100000, 3)
187 if err := h.RecordCorrectedValue(1000, 100); err != nil {
191 if v, want := h.ValueAtQuantile(75), int64(800); v != want {
192 t.Errorf("Corrected value was %v, but expected %v", v, want)
196 func TestCumulativeDistribution(t *testing.T) {
197 h := hdrhistogram.New(1, 100000000, 3)
199 for i := 0; i < 1000000; i++ {
200 if err := h.RecordValue(int64(i)); err != nil {
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},
225 if !reflect.DeepEqual(actual, expected) {
226 t.Errorf("CF was %#v, but expected %#v", actual, expected)
230 func TestDistribution(t *testing.T) {
231 h := hdrhistogram.New(8, 1024, 3)
233 for i := 0; i < 1024; i++ {
234 if err := h.RecordValue(int64(i)); err != nil {
239 actual := h.Distribution()
240 if len(actual) != 128 {
241 t.Errorf("Number of bars seen was %v, expected was 128", len(actual))
243 for _, b := range actual {
245 t.Errorf("Count per bar seen was %v, expected was 8", b.Count)
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")
255 if math.IsNaN(h.StdDev()) {
256 t.Error("stddev is NaN")
260 func TestSignificantFigures(t *testing.T) {
262 h := hdrhistogram.New(1, 10, sigFigs)
263 if h.SignificantFigures() != sigFigs {
264 t.Errorf("Significant figures was %v, expected %d", h.SignificantFigures(), sigFigs)
268 func TestLowestTrackableValue(t *testing.T) {
270 h := hdrhistogram.New(minVal, 10, 3)
271 if h.LowestTrackableValue() != minVal {
272 t.Errorf("LowestTrackableValue figures was %v, expected %d", h.LowestTrackableValue(), minVal)
276 func TestHighestTrackableValue(t *testing.T) {
278 h := hdrhistogram.New(1, maxVal, 3)
279 if h.HighestTrackableValue() != maxVal {
280 t.Errorf("HighestTrackableValue figures was %v, expected %d", h.HighestTrackableValue(), maxVal)
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 {
294 for i := 0; i < b.N; i++ {
299 func BenchmarkNew(b *testing.B) {
302 for i := 0; i < b.N; i++ {
303 hdrhistogram.New(1, 120000, 3) // this could track 1ms-2min
307 func TestUnitMagnitudeOverflow(t *testing.T) {
308 h := hdrhistogram.New(0, 200, 4)
309 if err := h.RecordValue(11); err != nil {
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)
320 for q, want := range map[float64]int64{
326 if got := hist.ValueAtQuantile(q); got != want {
327 t.Errorf("got %d for %fth percentile. want: %d", got, q, want)
332 func TestExportImport(t *testing.T) {
334 max := int64(10000000)
336 h := hdrhistogram.New(min, max, sigfigs)
337 for i := 0; i < 1000000; i++ {
338 if err := h.RecordValue(int64(i)); err != nil {
345 if v := s.LowestTrackableValue; v != min {
346 t.Errorf("LowestTrackableValue was %v, but expected %v", v, min)
349 if v := s.HighestTrackableValue; v != max {
350 t.Errorf("HighestTrackableValue was %v, but expected %v", v, max)
353 if v := int(s.SignificantFigures); v != sigfigs {
354 t.Errorf("SignificantFigures was %v, but expected %v", v, sigfigs)
357 if imported := hdrhistogram.Import(s); !imported.Equals(h) {
358 t.Error("Expected Histograms to be equivalent")
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 {
371 h2 := hdrhistogram.New(1, 10000000, 3)
372 for i := 0; i < 10000; i++ {
373 if err := h1.RecordValue(int64(i)); err != nil {
379 t.Error("Expected Histograms to not be equivalent")
386 t.Error("Expected Histograms to be equivalent")