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 { if c == 0 {
continue continue
} }
if !minSet {
if c > 0 && !minSet {
minSet = true minSet = true
min = int64(math.Floor(h.internal.Buckets[i])) min = int64(math.Floor(h.internal.Buckets[i]))
} }

View file

@ -187,10 +187,11 @@ type sampleSnapshot struct {
count int64 count int64
values []int64 values []int64
max int64 max int64
min int64 min int64
mean float64 mean float64
sum int64 sum int64
variance float64
} }
// newSampleSnapshotPrecalculated creates a read-only sampleSnapShot, using // 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 // StdDev returns the standard deviation of values at the time the snapshot was
// taken. // 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. // Sum returns the sum of values at the time the snapshot was taken.
func (s *sampleSnapshot) Sum() int64 { return s.sum } 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. // Variance returns the variance of values at the time the snapshot was taken.
func (s *sampleSnapshot) Variance() float64 { return SampleVariance(s.mean, s.values) } func (s *sampleSnapshot) Variance() float64 {
if s.variance == 0.0 {
// SampleStdDev returns the standard deviation of the slice of int64. s.variance = SampleVariance(s.mean, s.values)
func SampleStdDev(mean float64, values []int64) float64 { }
return math.Sqrt(SampleVariance(mean, values)) return s.variance
} }
// SampleVariance returns the variance of the slice of int64. // SampleVariance returns the variance of the slice of int64.