From 8038c0fb01a541a0285c5ff1e9ad3f10bd328544 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 11 Sep 2023 20:30:12 +0200 Subject: [PATCH] metrics: cache sample variance, rm ineffectual clause rthistogram --- metrics/runtimehistogram.go | 3 +-- metrics/sample.go | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/metrics/runtimehistogram.go b/metrics/runtimehistogram.go index 6b0c6a4c40..2d10c04c03 100644 --- a/metrics/runtimehistogram.go +++ b/metrics/runtimehistogram.go @@ -102,8 +102,7 @@ func (h *runtimeHistogramSnapshot) calc() { if c == 0 { continue } - - if c > 0 && !minSet { + if !minSet { minSet = true min = int64(math.Floor(h.internal.Buckets[i])) } diff --git a/metrics/sample.go b/metrics/sample.go index d204024f6b..152ad0de7b 100644 --- a/metrics/sample.go +++ b/metrics/sample.go @@ -187,10 +187,11 @@ type sampleSnapshot struct { count int64 values []int64 - max int64 - min int64 - mean float64 - sum int64 + max int64 + min int64 + mean float64 + sum int64 + variance float64 } // newSampleSnapshotPrecalculated creates a read-only sampleSnapShot, using @@ -265,7 +266,12 @@ func (s *sampleSnapshot) Snapshot() SampleSnapshot { return s } // StdDev returns the standard deviation of values at the time the snapshot was // taken. -func (s *sampleSnapshot) StdDev() float64 { return SampleStdDev(s.mean, s.values) } +func (s *sampleSnapshot) StdDev() float64 { + if s.variance == 0.0 { + s.variance = SampleVariance(s.mean, s.values) + } + return math.Sqrt(s.variance) +} // Sum returns the sum of values at the time the snapshot was taken. func (s *sampleSnapshot) Sum() int64 { return s.sum } @@ -278,11 +284,11 @@ func (s *sampleSnapshot) Values() []int64 { } // Variance returns the variance of values at the time the snapshot was taken. -func (s *sampleSnapshot) Variance() float64 { return SampleVariance(s.mean, s.values) } - -// SampleStdDev returns the standard deviation of the slice of int64. -func SampleStdDev(mean float64, values []int64) float64 { - return math.Sqrt(SampleVariance(mean, values)) +func (s *sampleSnapshot) Variance() float64 { + if s.variance == 0.0 { + s.variance = SampleVariance(s.mean, s.values) + } + return s.variance } // SampleVariance returns the variance of the slice of int64.