github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/metrics/resetting_timer_test.go (about) 1 package metrics 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestResettingTimer(t *testing.T) { 9 tests := []struct { 10 values []int64 11 start int 12 end int 13 wantP50 int64 14 wantP95 int64 15 wantP99 int64 16 wantMean float64 17 wantMin int64 18 wantMax int64 19 }{ 20 { 21 values: []int64{}, 22 start: 1, 23 end: 11, 24 wantP50: 5, wantP95: 10, wantP99: 10, 25 wantMin: 1, wantMax: 10, wantMean: 5.5, 26 }, 27 { 28 values: []int64{}, 29 start: 1, 30 end: 101, 31 wantP50: 50, wantP95: 95, wantP99: 99, 32 wantMin: 1, wantMax: 100, wantMean: 50.5, 33 }, 34 { 35 values: []int64{1}, 36 start: 0, 37 end: 0, 38 wantP50: 1, wantP95: 1, wantP99: 1, 39 wantMin: 1, wantMax: 1, wantMean: 1, 40 }, 41 { 42 values: []int64{0}, 43 start: 0, 44 end: 0, 45 wantP50: 0, wantP95: 0, wantP99: 0, 46 wantMin: 0, wantMax: 0, wantMean: 0, 47 }, 48 { 49 values: []int64{}, 50 start: 0, 51 end: 0, 52 wantP50: 0, wantP95: 0, wantP99: 0, 53 wantMin: 0, wantMax: 0, wantMean: 0, 54 }, 55 { 56 values: []int64{1, 10}, 57 start: 0, 58 end: 0, 59 wantP50: 1, wantP95: 10, wantP99: 10, 60 wantMin: 1, wantMax: 10, wantMean: 5.5, 61 }, 62 } 63 for ind, tt := range tests { 64 timer := NewResettingTimer() 65 66 for i := tt.start; i < tt.end; i++ { 67 tt.values = append(tt.values, int64(i)) 68 } 69 70 for _, v := range tt.values { 71 timer.Update(time.Duration(v)) 72 } 73 74 snap := timer.Snapshot() 75 76 ps := snap.Percentiles([]float64{50, 95, 99}) 77 78 val := snap.Values() 79 80 if len(val) > 0 { 81 if tt.wantMin != val[0] { 82 t.Fatalf("%d: min: got %d, want %d", ind, val[0], tt.wantMin) 83 } 84 85 if tt.wantMax != val[len(val)-1] { 86 t.Fatalf("%d: max: got %d, want %d", ind, val[len(val)-1], tt.wantMax) 87 } 88 } 89 90 if tt.wantMean != snap.Mean() { 91 t.Fatalf("%d: mean: got %.2f, want %.2f", ind, snap.Mean(), tt.wantMean) 92 } 93 94 if tt.wantP50 != ps[0] { 95 t.Fatalf("%d: p50: got %d, want %d", ind, ps[0], tt.wantP50) 96 } 97 98 if tt.wantP95 != ps[1] { 99 t.Fatalf("%d: p95: got %d, want %d", ind, ps[1], tt.wantP95) 100 } 101 102 if tt.wantP99 != ps[2] { 103 t.Fatalf("%d: p99: got %d, want %d", ind, ps[2], tt.wantP99) 104 } 105 } 106 }