github.com/theQRL/go-zond@v0.1.1/metrics/runtimehistogram_test.go (about) 1 package metrics 2 3 import ( 4 "bytes" 5 "encoding/gob" 6 "fmt" 7 "math" 8 "reflect" 9 "runtime/metrics" 10 "testing" 11 "time" 12 ) 13 14 var _ Histogram = (*runtimeHistogram)(nil) 15 16 type runtimeHistogramTest struct { 17 h metrics.Float64Histogram 18 19 Count int64 20 Min int64 21 Max int64 22 Sum int64 23 Mean float64 24 Variance float64 25 StdDev float64 26 Percentiles []float64 // .5 .8 .9 .99 .995 27 } 28 29 // This test checks the results of statistical functions implemented 30 // by runtimeHistogramSnapshot. 31 func TestRuntimeHistogramStats(t *testing.T) { 32 tests := []runtimeHistogramTest{ 33 0: { 34 h: metrics.Float64Histogram{ 35 Counts: []uint64{}, 36 Buckets: []float64{}, 37 }, 38 Count: 0, 39 Max: 0, 40 Min: 0, 41 Sum: 0, 42 Mean: 0, 43 Variance: 0, 44 StdDev: 0, 45 Percentiles: []float64{0, 0, 0, 0, 0}, 46 }, 47 1: { 48 // This checks the case where the highest bucket is +Inf. 49 h: metrics.Float64Histogram{ 50 Counts: []uint64{0, 1, 2}, 51 Buckets: []float64{0, 0.5, 1, math.Inf(1)}, 52 }, 53 Count: 3, 54 Max: 1, 55 Min: 0, 56 Sum: 3, 57 Mean: 0.9166666, 58 Percentiles: []float64{1, 1, 1, 1, 1}, 59 Variance: 0.020833, 60 StdDev: 0.144433, 61 }, 62 2: { 63 h: metrics.Float64Histogram{ 64 Counts: []uint64{8, 6, 3, 1}, 65 Buckets: []float64{12, 16, 18, 24, 25}, 66 }, 67 Count: 18, 68 Max: 25, 69 Min: 12, 70 Sum: 270, 71 Mean: 16.75, 72 Variance: 10.3015, 73 StdDev: 3.2096, 74 Percentiles: []float64{16, 18, 18, 24, 24}, 75 }, 76 } 77 78 for i, test := range tests { 79 t.Run(fmt.Sprint(i), func(t *testing.T) { 80 s := RuntimeHistogramFromData(1.0, &test.h).Snapshot() 81 82 if v := s.Count(); v != test.Count { 83 t.Errorf("Count() = %v, want %v", v, test.Count) 84 } 85 if v := s.Min(); v != test.Min { 86 t.Errorf("Min() = %v, want %v", v, test.Min) 87 } 88 if v := s.Max(); v != test.Max { 89 t.Errorf("Max() = %v, want %v", v, test.Max) 90 } 91 if v := s.Sum(); v != test.Sum { 92 t.Errorf("Sum() = %v, want %v", v, test.Sum) 93 } 94 if v := s.Mean(); !approxEqual(v, test.Mean, 0.0001) { 95 t.Errorf("Mean() = %v, want %v", v, test.Mean) 96 } 97 if v := s.Variance(); !approxEqual(v, test.Variance, 0.0001) { 98 t.Errorf("Variance() = %v, want %v", v, test.Variance) 99 } 100 if v := s.StdDev(); !approxEqual(v, test.StdDev, 0.0001) { 101 t.Errorf("StdDev() = %v, want %v", v, test.StdDev) 102 } 103 ps := []float64{.5, .8, .9, .99, .995} 104 if v := s.Percentiles(ps); !reflect.DeepEqual(v, test.Percentiles) { 105 t.Errorf("Percentiles(%v) = %v, want %v", ps, v, test.Percentiles) 106 } 107 }) 108 } 109 } 110 111 func approxEqual(x, y, ε float64) bool { 112 if math.IsInf(x, -1) && math.IsInf(y, -1) { 113 return true 114 } 115 if math.IsInf(x, 1) && math.IsInf(y, 1) { 116 return true 117 } 118 if math.IsNaN(x) && math.IsNaN(y) { 119 return true 120 } 121 return math.Abs(x-y) < ε 122 } 123 124 // This test verifies that requesting Percentiles in unsorted order 125 // returns them in the requested order. 126 func TestRuntimeHistogramStatsPercentileOrder(t *testing.T) { 127 s := RuntimeHistogramFromData(1.0, &metrics.Float64Histogram{ 128 Counts: []uint64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 129 Buckets: []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 130 }).Snapshot() 131 result := s.Percentiles([]float64{1, 0.2, 0.5, 0.1, 0.2}) 132 expected := []float64{10, 2, 5, 1, 2} 133 if !reflect.DeepEqual(result, expected) { 134 t.Fatal("wrong result:", result) 135 } 136 } 137 138 func BenchmarkRuntimeHistogramSnapshotRead(b *testing.B) { 139 var sLatency = "7\xff\x81\x03\x01\x01\x10Float64Histogram\x01\xff\x82\x00\x01\x02\x01\x06Counts\x01\xff\x84\x00\x01\aBuckets\x01\xff\x86\x00\x00\x00\x16\xff\x83\x02\x01\x01\b[]uint64\x01\xff\x84\x00\x01\x06\x00\x00\x17\xff\x85\x02\x01\x01\t[]float64\x01\xff\x86\x00\x01\b\x00\x00\xfe\x06T\xff\x82\x01\xff\xa2\x00\xfe\r\xef\x00\x01\x02\x02\x04\x05\x04\b\x15\x17 B?6.L;$!2) \x1a? \x190aH7FY6#\x190\x1d\x14\x10\x1b\r\t\x04\x03\x01\x01\x00\x03\x02\x00\x03\x05\x05\x02\x02\x06\x04\v\x06\n\x15\x18\x13'&.\x12=H/L&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\xa3\xfe\xf0\xff\x00\xf8\x95\xd6&\xe8\v.q>\xf8\x95\xd6&\xe8\v.\x81>\xf8\xdfA:\xdc\x11ʼn>\xf8\x95\xd6&\xe8\v.\x91>\xf8:\x8c0\xe2\x8ey\x95>\xf8\xdfA:\xdc\x11ř>\xf8\x84\xf7C֔\x10\x9e>\xf8\x95\xd6&\xe8\v.\xa1>\xf8:\x8c0\xe2\x8ey\xa5>\xf8\xdfA:\xdc\x11ũ>\xf8\x84\xf7C֔\x10\xae>\xf8\x95\xd6&\xe8\v.\xb1>\xf8:\x8c0\xe2\x8ey\xb5>\xf8\xdfA:\xdc\x11Ź>\xf8\x84\xf7C֔\x10\xbe>\xf8\x95\x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0\xe2\x8ey\xe5@\xf8\xdfA:\xdc\x11\xc5\xe9@\xf8\x84\xf7C֔\x10\xee@\xf8\x95\xd6&\xe8\v.\xf1@\xf8:\x8c0\xe2\x8ey\xf5@\xf8\xdfA:\xdc\x11\xc5\xf9@\xf8\x84\xf7C֔\x10\xfe@\xf8\x95\xd6&\xe8\v.\x01A\xfe\xf0\x7f\x00" 140 141 dserialize := func(data string) *metrics.Float64Histogram { 142 var res metrics.Float64Histogram 143 if err := gob.NewDecoder(bytes.NewReader([]byte(data))).Decode(&res); err != nil { 144 panic(err) 145 } 146 return &res 147 } 148 latency := RuntimeHistogramFromData(float64(time.Second), dserialize(sLatency)) 149 b.ResetTimer() 150 b.ReportAllocs() 151 for i := 0; i < b.N; i++ { 152 snap := latency.Snapshot() 153 // These are the fields that influxdb accesses 154 _ = snap.Count() 155 _ = snap.Max() 156 _ = snap.Mean() 157 _ = snap.Min() 158 _ = snap.StdDev() 159 _ = snap.Variance() 160 _ = snap.Percentiles([]float64{0.25, 0.5, 0.75, 0.95, 0.99, 0.999, 0.9999}) 161 } 162 }