metrics: fix ewma

This commit is contained in:
Martin Holst Swende 2024-11-26 20:36:01 +01:00
parent bea923556b
commit c1c47eea90
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
4 changed files with 20 additions and 40 deletions

View file

@ -7,56 +7,37 @@ import (
"time" "time"
) )
type EWMASnapshot interface { // EWMASnapshot is a read-only copy of an EWMA.
Rate() float64 type EWMASnapshot float64
}
// EWMAs continuously calculate an exponentially-weighted moving average // Rate returns the rate of events per second at the time the snapshot was
// based on an outside source of clock ticks. // taken.
type EWMA interface { func (a EWMASnapshot) Rate() float64 { return float64(a) }
Snapshot() EWMASnapshot
Tick()
Update(int64)
}
// NewEWMA constructs a new EWMA with the given alpha. // NewEWMA constructs a new EWMA with the given alpha.
func NewEWMA(alpha float64) EWMA { func NewEWMA(alpha float64) *EWMA {
return &StandardEWMA{alpha: alpha} return &EWMA{alpha: alpha}
} }
// NewEWMA1 constructs a new EWMA for a one-minute moving average. // NewEWMA1 constructs a new EWMA for a one-minute moving average.
func NewEWMA1() EWMA { func NewEWMA1() *EWMA {
return NewEWMA(1 - math.Exp(-5.0/60.0/1)) return NewEWMA(1 - math.Exp(-5.0/60.0/1))
} }
// NewEWMA5 constructs a new EWMA for a five-minute moving average. // NewEWMA5 constructs a new EWMA for a five-minute moving average.
func NewEWMA5() EWMA { func NewEWMA5() *EWMA {
return NewEWMA(1 - math.Exp(-5.0/60.0/5)) return NewEWMA(1 - math.Exp(-5.0/60.0/5))
} }
// NewEWMA15 constructs a new EWMA for a fifteen-minute moving average. // NewEWMA15 constructs a new EWMA for a fifteen-minute moving average.
func NewEWMA15() EWMA { func NewEWMA15() *EWMA {
return NewEWMA(1 - math.Exp(-5.0/60.0/15)) return NewEWMA(1 - math.Exp(-5.0/60.0/15))
} }
// ewmaSnapshot is a read-only copy of another EWMA. // EWMA imlements an exponential weighted moving average. It tracks the number
type ewmaSnapshot float64
// Rate returns the rate of events per second at the time the snapshot was
// taken.
func (a ewmaSnapshot) Rate() float64 { return float64(a) }
// NilEWMA is a no-op EWMA.
type NilEWMA struct{}
func (NilEWMA) Snapshot() EWMASnapshot { return (*emptySnapshot)(nil) }
func (NilEWMA) Tick() {}
func (NilEWMA) Update(n int64) {}
// StandardEWMA is the standard implementation of an EWMA and tracks the number
// of uncounted events and processes them on each tick. It uses the // of uncounted events and processes them on each tick. It uses the
// sync/atomic package to manage uncounted events. // sync/atomic package to manage uncounted events.
type StandardEWMA struct { type EWMA struct {
uncounted atomic.Int64 uncounted atomic.Int64
alpha float64 alpha float64
rate atomic.Uint64 rate atomic.Uint64
@ -65,14 +46,14 @@ type StandardEWMA struct {
} }
// Snapshot returns a read-only copy of the EWMA. // Snapshot returns a read-only copy of the EWMA.
func (a *StandardEWMA) Snapshot() EWMASnapshot { func (a *EWMA) Snapshot() EWMASnapshot {
r := math.Float64frombits(a.rate.Load()) * float64(time.Second) r := math.Float64frombits(a.rate.Load()) * float64(time.Second)
return ewmaSnapshot(r) return EWMASnapshot(r)
} }
// Tick ticks the clock to update the moving average. It assumes it is called // Tick ticks the clock to update the moving average. It assumes it is called
// every five seconds. // every five seconds.
func (a *StandardEWMA) Tick() { func (a *EWMA) Tick() {
// Optimization to avoid mutex locking in the hot-path. // Optimization to avoid mutex locking in the hot-path.
if a.init.Load() { if a.init.Load() {
a.updateRate(a.fetchInstantRate()) a.updateRate(a.fetchInstantRate())
@ -94,18 +75,18 @@ func (a *StandardEWMA) Tick() {
a.mutex.Unlock() a.mutex.Unlock()
} }
func (a *StandardEWMA) fetchInstantRate() float64 { func (a *EWMA) fetchInstantRate() float64 {
count := a.uncounted.Swap(0) count := a.uncounted.Swap(0)
return float64(count) / float64(5*time.Second) return float64(count) / float64(5*time.Second)
} }
func (a *StandardEWMA) updateRate(instantRate float64) { func (a *EWMA) updateRate(instantRate float64) {
currentRate := math.Float64frombits(a.rate.Load()) currentRate := math.Float64frombits(a.rate.Load())
currentRate += a.alpha * (instantRate - currentRate) currentRate += a.alpha * (instantRate - currentRate)
a.rate.Store(math.Float64bits(currentRate)) a.rate.Store(math.Float64bits(currentRate))
} }
// Update adds n uncounted events. // Update adds n uncounted events.
func (a *StandardEWMA) Update(n int64) { func (a *EWMA) Update(n int64) {
a.uncounted.Add(n) a.uncounted.Add(n)
} }

View file

@ -82,7 +82,7 @@ func TestEWMA15(t *testing.T) {
} }
} }
func elapseMinute(a EWMA) { func elapseMinute(a *EWMA) {
for i := 0; i < 12; i++ { for i := 0; i < 12; i++ {
a.Tick() a.Tick()
} }

View file

@ -22,7 +22,6 @@ var (
_ HistogramSnapshot = (*emptySnapshot)(nil) _ HistogramSnapshot = (*emptySnapshot)(nil)
_ GaugeSnapshot = (*emptySnapshot)(nil) _ GaugeSnapshot = (*emptySnapshot)(nil)
_ MeterSnapshot = (*emptySnapshot)(nil) _ MeterSnapshot = (*emptySnapshot)(nil)
_ EWMASnapshot = (*emptySnapshot)(nil)
_ TimerSnapshot = (*emptySnapshot)(nil) _ TimerSnapshot = (*emptySnapshot)(nil)
) )

View file

@ -108,7 +108,7 @@ type StandardMeter struct {
uncounted atomic.Int64 // not yet added to the EWMAs uncounted atomic.Int64 // not yet added to the EWMAs
rateMean atomic.Uint64 rateMean atomic.Uint64
a1, a5, a15 EWMA a1, a5, a15 *EWMA
startTime time.Time startTime time.Time
stopped atomic.Bool stopped atomic.Bool
} }