metrics: optimize resetting timer, make influx resetting-timer span type use integer format

This commit is contained in:
Martin Holst Swende 2023-09-01 18:58:59 +02:00
parent 17765d22bb
commit 40f291b043
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
4 changed files with 16 additions and 19 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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))
}