metrics: cache sample variance, rm ineffectual clause rthistogram

This commit is contained in:
Martin Holst Swende 2023-09-11 20:30:12 +02:00
parent 267656019b
commit 8038c0fb01
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 17 additions and 12 deletions

View file

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

View file

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