gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/histogram_test.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package metrics 18 19 import "testing" 20 21 func BenchmarkHistogram(b *testing.B) { 22 h := NewHistogram(NewUniformSample(100)) 23 b.ResetTimer() 24 for i := 0; i < b.N; i++ { 25 h.Update(int64(i)) 26 } 27 } 28 29 func TestGetOrRegisterHistogram(t *testing.T) { 30 r := NewRegistry() 31 s := NewUniformSample(100) 32 NewRegisteredHistogram("foo", r, s).Update(47) 33 if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() { 34 t.Fatal(h) 35 } 36 } 37 38 func TestHistogram10000(t *testing.T) { 39 h := NewHistogram(NewUniformSample(100000)) 40 for i := 1; i <= 10000; i++ { 41 h.Update(int64(i)) 42 } 43 testHistogram10000(t, h) 44 } 45 46 func TestHistogramEmpty(t *testing.T) { 47 h := NewHistogram(NewUniformSample(100)) 48 if count := h.Count(); 0 != count { 49 t.Errorf("h.Count(): 0 != %v\n", count) 50 } 51 if min := h.Min(); 0 != min { 52 t.Errorf("h.Min(): 0 != %v\n", min) 53 } 54 if max := h.Max(); 0 != max { 55 t.Errorf("h.Max(): 0 != %v\n", max) 56 } 57 if mean := h.Mean(); 0.0 != mean { 58 t.Errorf("h.Mean(): 0.0 != %v\n", mean) 59 } 60 if stdDev := h.StdDev(); 0.0 != stdDev { 61 t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev) 62 } 63 ps := h.Percentiles([]float64{0.5, 0.75, 0.99}) 64 if 0.0 != ps[0] { 65 t.Errorf("median: 0.0 != %v\n", ps[0]) 66 } 67 if 0.0 != ps[1] { 68 t.Errorf("75th percentile: 0.0 != %v\n", ps[1]) 69 } 70 if 0.0 != ps[2] { 71 t.Errorf("99th percentile: 0.0 != %v\n", ps[2]) 72 } 73 } 74 75 func TestHistogramSnapshot(t *testing.T) { 76 h := NewHistogram(NewUniformSample(100000)) 77 for i := 1; i <= 10000; i++ { 78 h.Update(int64(i)) 79 } 80 snapshot := h.Snapshot() 81 h.Update(0) 82 testHistogram10000(t, snapshot) 83 } 84 85 func testHistogram10000(t *testing.T, h Histogram) { 86 if count := h.Count(); 10000 != count { 87 t.Errorf("h.Count(): 10000 != %v\n", count) 88 } 89 if min := h.Min(); 1 != min { 90 t.Errorf("h.Min(): 1 != %v\n", min) 91 } 92 if max := h.Max(); 10000 != max { 93 t.Errorf("h.Max(): 10000 != %v\n", max) 94 } 95 if mean := h.Mean(); 5000.5 != mean { 96 t.Errorf("h.Mean(): 5000.5 != %v\n", mean) 97 } 98 if stdDev := h.StdDev(); 2886.751331514372 != stdDev { 99 t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev) 100 } 101 ps := h.Percentiles([]float64{0.5, 0.75, 0.99}) 102 if 5000.5 != ps[0] { 103 t.Errorf("median: 5000.5 != %v\n", ps[0]) 104 } 105 if 7500.75 != ps[1] { 106 t.Errorf("75th percentile: 7500.75 != %v\n", ps[1]) 107 } 108 if 9900.99 != ps[2] { 109 t.Errorf("99th percentile: 9900.99 != %v\n", ps[2]) 110 } 111 }