gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/resetting_timer_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 ( 20 "testing" 21 "time" 22 ) 23 24 func TestResettingTimer(t *testing.T) { 25 tests := []struct { 26 values []int64 27 start int 28 end int 29 wantP50 int64 30 wantP95 int64 31 wantP99 int64 32 wantMean float64 33 wantMin int64 34 wantMax int64 35 }{ 36 { 37 values: []int64{}, 38 start: 1, 39 end: 11, 40 wantP50: 5, wantP95: 10, wantP99: 10, 41 wantMin: 1, wantMax: 10, wantMean: 5.5, 42 }, 43 { 44 values: []int64{}, 45 start: 1, 46 end: 101, 47 wantP50: 50, wantP95: 95, wantP99: 99, 48 wantMin: 1, wantMax: 100, wantMean: 50.5, 49 }, 50 { 51 values: []int64{1}, 52 start: 0, 53 end: 0, 54 wantP50: 1, wantP95: 1, wantP99: 1, 55 wantMin: 1, wantMax: 1, wantMean: 1, 56 }, 57 { 58 values: []int64{0}, 59 start: 0, 60 end: 0, 61 wantP50: 0, wantP95: 0, wantP99: 0, 62 wantMin: 0, wantMax: 0, wantMean: 0, 63 }, 64 { 65 values: []int64{}, 66 start: 0, 67 end: 0, 68 wantP50: 0, wantP95: 0, wantP99: 0, 69 wantMin: 0, wantMax: 0, wantMean: 0, 70 }, 71 { 72 values: []int64{1, 10}, 73 start: 0, 74 end: 0, 75 wantP50: 1, wantP95: 10, wantP99: 10, 76 wantMin: 1, wantMax: 10, wantMean: 5.5, 77 }, 78 } 79 for ind, tt := range tests { 80 timer := NewResettingTimer() 81 82 for i := tt.start; i < tt.end; i++ { 83 tt.values = append(tt.values, int64(i)) 84 } 85 86 for _, v := range tt.values { 87 timer.Update(time.Duration(v)) 88 } 89 90 snap := timer.Snapshot() 91 92 ps := snap.Percentiles([]float64{50, 95, 99}) 93 94 val := snap.Values() 95 96 if len(val) > 0 { 97 if tt.wantMin != val[0] { 98 t.Fatalf("%d: min: got %d, want %d", ind, val[0], tt.wantMin) 99 } 100 101 if tt.wantMax != val[len(val)-1] { 102 t.Fatalf("%d: max: got %d, want %d", ind, val[len(val)-1], tt.wantMax) 103 } 104 } 105 106 if tt.wantMean != snap.Mean() { 107 t.Fatalf("%d: mean: got %.2f, want %.2f", ind, snap.Mean(), tt.wantMean) 108 } 109 110 if tt.wantP50 != ps[0] { 111 t.Fatalf("%d: p50: got %d, want %d", ind, ps[0], tt.wantP50) 112 } 113 114 if tt.wantP95 != ps[1] { 115 t.Fatalf("%d: p95: got %d, want %d", ind, ps[1], tt.wantP95) 116 } 117 118 if tt.wantP99 != ps[2] { 119 t.Fatalf("%d: p99: got %d, want %d", ind, ps[2], tt.wantP99) 120 } 121 } 122 }