aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--metrics/exp/exp.go13
-rw-r--r--metrics/resetting_timer.go2
-rw-r--r--metrics/resetting_timer_test.go110
-rw-r--r--node/api.go30
4 files changed, 154 insertions, 1 deletions
diff --git a/metrics/exp/exp.go b/metrics/exp/exp.go
index c19d00a94..625ffd4e8 100644
--- a/metrics/exp/exp.go
+++ b/metrics/exp/exp.go
@@ -134,6 +134,17 @@ func (exp *exp) publishTimer(name string, metric metrics.Timer) {
exp.getFloat(name + ".mean-rate").Set(t.RateMean())
}
+func (exp *exp) publishResettingTimer(name string, metric metrics.ResettingTimer) {
+ t := metric.Snapshot()
+ ps := t.Percentiles([]float64{50, 75, 95, 99})
+ exp.getInt(name + ".count").Set(int64(len(t.Values())))
+ exp.getFloat(name + ".mean").Set(t.Mean())
+ exp.getInt(name + ".50-percentile").Set(ps[0])
+ exp.getInt(name + ".75-percentile").Set(ps[1])
+ exp.getInt(name + ".95-percentile").Set(ps[2])
+ exp.getInt(name + ".99-percentile").Set(ps[3])
+}
+
func (exp *exp) syncToExpvar() {
exp.registry.Each(func(name string, i interface{}) {
switch i.(type) {
@@ -149,6 +160,8 @@ func (exp *exp) syncToExpvar() {
exp.publishMeter(name, i.(metrics.Meter))
case metrics.Timer:
exp.publishTimer(name, i.(metrics.Timer))
+ case metrics.ResettingTimer:
+ exp.publishResettingTimer(name, i.(metrics.ResettingTimer))
default:
panic(fmt.Sprintf("unsupported type for '%s': %T", name, i))
}
diff --git a/metrics/resetting_timer.go b/metrics/resetting_timer.go
index 57bcb3134..f33a9f8aa 100644
--- a/metrics/resetting_timer.go
+++ b/metrics/resetting_timer.go
@@ -210,7 +210,7 @@ func (t *ResettingTimerSnapshot) calc(percentiles []float64) {
// poor man's math.Round(x):
// math.Floor(x + 0.5)
indexOfPerc := int(math.Floor(((abs / 100.0) * float64(count)) + 0.5))
- if pct >= 0 {
+ if pct >= 0 && indexOfPerc > 0 {
indexOfPerc -= 1 // index offset=0
}
thresholdBoundary = t.values[indexOfPerc]
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)
+ }
+ }
+}
diff --git a/node/api.go b/node/api.go
index da9da5bd7..989d3884a 100644
--- a/node/api.go
+++ b/node/api.go
@@ -338,6 +338,21 @@ func (api *PublicDebugAPI) Metrics(raw bool) (map[string]interface{}, error) {
},
}
+ case metrics.ResettingTimer:
+ t := metric.Snapshot()
+ ps := t.Percentiles([]float64{5, 20, 50, 80, 95})
+ root[name] = map[string]interface{}{
+ "Measurements": len(t.Values()),
+ "Mean": time.Duration(t.Mean()).String(),
+ "Percentiles": map[string]interface{}{
+ "5": time.Duration(ps[0]).String(),
+ "20": time.Duration(ps[1]).String(),
+ "50": time.Duration(ps[2]).String(),
+ "80": time.Duration(ps[3]).String(),
+ "95": time.Duration(ps[4]).String(),
+ },
+ }
+
default:
root[name] = "Unknown metric type"
}
@@ -373,6 +388,21 @@ func (api *PublicDebugAPI) Metrics(raw bool) (map[string]interface{}, error) {
},
}
+ case metrics.ResettingTimer:
+ t := metric.Snapshot()
+ ps := t.Percentiles([]float64{5, 20, 50, 80, 95})
+ root[name] = map[string]interface{}{
+ "Measurements": len(t.Values()),
+ "Mean": time.Duration(t.Mean()).String(),
+ "Percentiles": map[string]interface{}{
+ "5": time.Duration(ps[0]).String(),
+ "20": time.Duration(ps[1]).String(),
+ "50": time.Duration(ps[2]).String(),
+ "80": time.Duration(ps[3]).String(),
+ "95": time.Duration(ps[4]).String(),
+ },
+ }
+
default:
root[name] = "Unknown metric type"
}