aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/resetting_timer_test.go
diff options
context:
space:
mode:
authorAnton Evangelatov <anton.evangelatov@gmail.com>2018-06-04 18:05:16 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-06-04 18:05:16 +0800
commitbe2aec092d9c24c24b8d22d684ed0d11653c3cfc (patch)
tree74db6c965c6c0d8c235b92710bdacc8fb95ba309 /metrics/resetting_timer_test.go
parent143c4341d8a2231deade6d7341c668d609bd3486 (diff)
downloaddexon-be2aec092d9c24c24b8d22d684ed0d11653c3cfc.tar
dexon-be2aec092d9c24c24b8d22d684ed0d11653c3cfc.tar.gz
dexon-be2aec092d9c24c24b8d22d684ed0d11653c3cfc.tar.bz2
dexon-be2aec092d9c24c24b8d22d684ed0d11653c3cfc.tar.lz
dexon-be2aec092d9c24c24b8d22d684ed0d11653c3cfc.tar.xz
dexon-be2aec092d9c24c24b8d22d684ed0d11653c3cfc.tar.zst
dexon-be2aec092d9c24c24b8d22d684ed0d11653c3cfc.zip
metrics: expvar support for ResettingTimer (#16878)
* metrics: expvar support for ResettingTimer * metrics: use integers for percentiles; remove Overall * metrics: fix edge-case panic for index-out-of-range
Diffstat (limited to 'metrics/resetting_timer_test.go')
-rw-r--r--metrics/resetting_timer_test.go110
1 files changed, 110 insertions, 0 deletions
diff --git a/metrics/resetting_timer_test.go b/metrics/resetting_timer_test.go
index 58fd47f35..77c49dc38 100644
--- a/metrics/resetting_timer_test.go
+++ b/metrics/resetting_timer_test.go
@@ -104,3 +104,113 @@ func TestResettingTimer(t *testing.T) {
}
}
}
+
+func TestResettingTimerWithFivePercentiles(t *testing.T) {
+ tests := []struct {
+ values []int64
+ start int
+ end int
+ wantP05 int64
+ wantP20 int64
+ wantP50 int64
+ wantP95 int64
+ wantP99 int64
+ wantMean float64
+ wantMin int64
+ wantMax int64
+ }{
+ {
+ values: []int64{},
+ start: 1,
+ end: 11,
+ wantP05: 1, wantP20: 2, wantP50: 5, wantP95: 10, wantP99: 10,
+ wantMin: 1, wantMax: 10, wantMean: 5.5,
+ },
+ {
+ values: []int64{},
+ start: 1,
+ end: 101,
+ wantP05: 5, wantP20: 20, wantP50: 50, wantP95: 95, wantP99: 99,
+ wantMin: 1, wantMax: 100, wantMean: 50.5,
+ },
+ {
+ values: []int64{1},
+ start: 0,
+ end: 0,
+ wantP05: 1, wantP20: 1, wantP50: 1, wantP95: 1, wantP99: 1,
+ wantMin: 1, wantMax: 1, wantMean: 1,
+ },
+ {
+ values: []int64{0},
+ start: 0,
+ end: 0,
+ wantP05: 0, wantP20: 0, wantP50: 0, wantP95: 0, wantP99: 0,
+ wantMin: 0, wantMax: 0, wantMean: 0,
+ },
+ {
+ values: []int64{},
+ start: 0,
+ end: 0,
+ wantP05: 0, wantP20: 0, wantP50: 0, wantP95: 0, wantP99: 0,
+ wantMin: 0, wantMax: 0, wantMean: 0,
+ },
+ {
+ values: []int64{1, 10},
+ start: 0,
+ end: 0,
+ wantP05: 1, wantP20: 1, wantP50: 1, wantP95: 10, wantP99: 10,
+ wantMin: 1, wantMax: 10, wantMean: 5.5,
+ },
+ }
+ for ind, tt := range tests {
+ timer := NewResettingTimer()
+
+ for i := tt.start; i < tt.end; i++ {
+ tt.values = append(tt.values, int64(i))
+ }
+
+ for _, v := range tt.values {
+ timer.Update(time.Duration(v))
+ }
+
+ snap := timer.Snapshot()
+
+ ps := snap.Percentiles([]float64{5, 20, 50, 95, 99})
+
+ val := snap.Values()
+
+ if len(val) > 0 {
+ if tt.wantMin != val[0] {
+ t.Fatalf("%d: min: got %d, want %d", ind, val[0], tt.wantMin)
+ }
+
+ if tt.wantMax != val[len(val)-1] {
+ t.Fatalf("%d: max: got %d, want %d", ind, val[len(val)-1], tt.wantMax)
+ }
+ }
+
+ if tt.wantMean != snap.Mean() {
+ t.Fatalf("%d: mean: got %.2f, want %.2f", ind, snap.Mean(), tt.wantMean)
+ }
+
+ if tt.wantP05 != ps[0] {
+ t.Fatalf("%d: p05: got %d, want %d", ind, ps[0], tt.wantP05)
+ }
+
+ if tt.wantP20 != ps[1] {
+ t.Fatalf("%d: p20: got %d, want %d", ind, ps[1], tt.wantP20)
+ }
+
+ if tt.wantP50 != ps[2] {
+ t.Fatalf("%d: p50: got %d, want %d", ind, ps[2], tt.wantP50)
+ }
+
+ if tt.wantP95 != ps[3] {
+ t.Fatalf("%d: p95: got %d, want %d", ind, ps[3], tt.wantP95)
+ }
+
+ if tt.wantP99 != ps[4] {
+ t.Fatalf("%d: p99: got %d, want %d", ind, ps[4], tt.wantP99)
+ }
+ }
+}