From 40f291b0439a3e0498d20f328e29a4d98d3607e2 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 1 Sep 2023 18:58:59 +0200 Subject: [PATCH] metrics: optimize resetting timer, make influx resetting-timer span type use integer format --- metrics/influxdb/influxdb.go | 6 +++--- metrics/influxdb/testdata/influxdbv1.want | 2 +- metrics/influxdb/testdata/influxdbv2.want | 2 +- metrics/resetting_timer.go | 25 ++++++++++------------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/metrics/influxdb/influxdb.go b/metrics/influxdb/influxdb.go index 227c2bd572..bbc4fc024b 100644 --- a/metrics/influxdb/influxdb.go +++ b/metrics/influxdb/influxdb.go @@ -109,9 +109,9 @@ func readMeter(namespace, name string, i interface{}) (string, map[string]interf "max": t.Max(), "mean": t.Mean(), "min": t.Min(), - "p50": ps[0], - "p95": ps[1], - "p99": ps[2], + "p50": int(ps[0]), + "p95": int(ps[1]), + "p99": int(ps[2]), } return measurement, fields } diff --git a/metrics/influxdb/testdata/influxdbv1.want b/metrics/influxdb/testdata/influxdbv1.want index 3bf30737b9..83ab88af67 100644 --- a/metrics/influxdb/testdata/influxdbv1.want +++ b/metrics/influxdb/testdata/influxdbv1.want @@ -5,5 +5,5 @@ goth.test/gauge_float64.gauge value=34567.89 978307200000000000 goth.test/gauge_info.gauge value="{\"arch\":\"amd64\",\"commit\":\"7caa2d8163ae3132c1c2d6978c76610caee2d949\",\"os\":\"linux\",\"protocol_versions\":\"64 65 66\",\"version\":\"1.10.18-unstable\"}" 978307200000000000 goth.test/histogram.histogram count=3i,max=3i,mean=2,min=1i,p25=1,p50=2,p75=3,p95=3,p99=3,p999=3,p9999=3,stddev=0.816496580927726,variance=0.6666666666666666 978307200000000000 goth.test/meter.meter count=0i,m1=0,m15=0,m5=0,mean=0 978307200000000000 -goth.test/resetting_timer.span count=6i,max=120000000i,mean=30000000,min=10000000i,p50=12500000,p95=120000000,p99=120000000 978307200000000000 +goth.test/resetting_timer.span count=6i,max=120000000i,mean=30000000,min=10000000i,p50=12500000i,p95=120000000i,p99=120000000i 978307200000000000 goth.test/timer.timer count=6i,m1=0,m15=0,m5=0,max=120000000i,mean=38333333.333333336,meanrate=0,min=20000000i,p50=22500000,p75=48000000,p95=120000000,p99=120000000,p999=120000000,p9999=120000000,stddev=36545253.529775314,variance=1335555555555555.2 978307200000000000 diff --git a/metrics/influxdb/testdata/influxdbv2.want b/metrics/influxdb/testdata/influxdbv2.want index 3bf30737b9..83ab88af67 100644 --- a/metrics/influxdb/testdata/influxdbv2.want +++ b/metrics/influxdb/testdata/influxdbv2.want @@ -5,5 +5,5 @@ goth.test/gauge_float64.gauge value=34567.89 978307200000000000 goth.test/gauge_info.gauge value="{\"arch\":\"amd64\",\"commit\":\"7caa2d8163ae3132c1c2d6978c76610caee2d949\",\"os\":\"linux\",\"protocol_versions\":\"64 65 66\",\"version\":\"1.10.18-unstable\"}" 978307200000000000 goth.test/histogram.histogram count=3i,max=3i,mean=2,min=1i,p25=1,p50=2,p75=3,p95=3,p99=3,p999=3,p9999=3,stddev=0.816496580927726,variance=0.6666666666666666 978307200000000000 goth.test/meter.meter count=0i,m1=0,m15=0,m5=0,mean=0 978307200000000000 -goth.test/resetting_timer.span count=6i,max=120000000i,mean=30000000,min=10000000i,p50=12500000,p95=120000000,p99=120000000 978307200000000000 +goth.test/resetting_timer.span count=6i,max=120000000i,mean=30000000,min=10000000i,p50=12500000i,p95=120000000i,p99=120000000i 978307200000000000 goth.test/timer.timer count=6i,m1=0,m15=0,m5=0,max=120000000i,mean=38333333.333333336,meanrate=0,min=20000000i,p50=22500000,p75=48000000,p95=120000000,p99=120000000,p999=120000000,p9999=120000000,stddev=36545253.529775314,variance=1335555555555555.2 978307200000000000 diff --git a/metrics/resetting_timer.go b/metrics/resetting_timer.go index 8fb27c8a5c..ef1162fed8 100644 --- a/metrics/resetting_timer.go +++ b/metrics/resetting_timer.go @@ -71,19 +71,22 @@ func (NilResettingTimer) Count() int { return 0 } // and Meter. type StandardResettingTimer struct { values []int64 - mutex sync.Mutex + sum int64 // sum is a running count of the total sum, used later to calculate mean + + mutex sync.Mutex } // Snapshot resets the timer and returns a read-only copy of its contents. func (t *StandardResettingTimer) Snapshot() ResettingTimerSnapshot { t.mutex.Lock() defer t.mutex.Unlock() - currentValues := t.values - t.values = make([]int64, 0, InitialResettingTimerSliceCap) - - return &resettingTimerSnapshot{ - values: currentValues, + snapshot := &resettingTimerSnapshot{ + values: t.values, + mean: float64(t.sum) / float64(len(t.values)), } + t.values = make([]int64, 0, InitialResettingTimerSliceCap) + t.sum = 0 + return snapshot } // Record the duration of the execution of the given function. @@ -98,13 +101,12 @@ func (t *StandardResettingTimer) Update(d time.Duration) { t.mutex.Lock() defer t.mutex.Unlock() t.values = append(t.values, int64(d)) + t.sum += int64(d) } // Record the duration of an event that started at a time and ends now. func (t *StandardResettingTimer) UpdateSince(ts time.Time) { - t.mutex.Lock() - defer t.mutex.Unlock() - t.values = append(t.values, int64(time.Since(ts))) + t.Update(time.Since(ts)) } // resettingTimerSnapshot is a point-in-time copy of another ResettingTimer. @@ -165,9 +167,4 @@ func (t *resettingTimerSnapshot) calc(percentiles []float64) { } t.min = t.values[0] t.max = t.values[len(t.values)-1] - var sum int64 - for _, v := range t.values { - sum += v - } - t.mean = float64(sum) / float64(len(t.values)) }