metrics: unexport ewma.Tick method

This commit is contained in:
Martin Holst Swende 2024-11-27 04:58:50 +01:00
parent b1dc19d7ee
commit 4111636841
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 13 additions and 13 deletions

View file

@ -50,22 +50,22 @@ func (a *EWMA) Snapshot() EWMASnapshot {
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 *EWMA) 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())
return return
} }
// Slow-path: this is only needed on the first Tick() and preserves transactional updating // Slow-path: this is only needed on the first tick() and preserves transactional updating
// of init and rate in the else block. The first conditional is needed below because // of init and rate in the else block. The first conditional is needed below because
// a different thread could have set a.init = 1 between the time of the first atomic load and when // a different thread could have set a.init = 1 between the time of the first atomic load and when
// the lock was acquired. // the lock was acquired.
a.mutex.Lock() a.mutex.Lock()
if a.init.Load() { if a.init.Load() {
// The fetchInstantRate() uses atomic loading, which is unnecessary in this critical section // The fetchInstantRate() uses atomic loading, which is unnecessary in this critical section
// but again, this section is only invoked on the first successful Tick() operation. // but again, this section is only invoked on the first successful tick() operation.
a.updateRate(a.fetchInstantRate()) a.updateRate(a.fetchInstantRate())
} else { } else {
a.init.Store(true) a.init.Store(true)

View file

@ -12,7 +12,7 @@ func BenchmarkEWMA(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
a.Update(1) a.Update(1)
a.Tick() a.tick()
} }
} }
@ -23,7 +23,7 @@ func BenchmarkEWMAParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
a.Update(1) a.Update(1)
a.Tick() a.tick()
} }
}) })
} }
@ -31,7 +31,7 @@ func BenchmarkEWMAParallel(b *testing.B) {
func TestEWMA1(t *testing.T) { func TestEWMA1(t *testing.T) {
a := NewEWMA1() a := NewEWMA1()
a.Update(3) a.Update(3)
a.Tick() a.tick()
for i, want := range []float64{0.6, for i, want := range []float64{0.6,
0.22072766470286553, 0.08120116994196772, 0.029872241020718428, 0.22072766470286553, 0.08120116994196772, 0.029872241020718428,
0.01098938333324054, 0.004042768199451294, 0.0014872513059998212, 0.01098938333324054, 0.004042768199451294, 0.0014872513059998212,
@ -49,7 +49,7 @@ func TestEWMA1(t *testing.T) {
func TestEWMA5(t *testing.T) { func TestEWMA5(t *testing.T) {
a := NewEWMA5() a := NewEWMA5()
a.Update(3) a.Update(3)
a.Tick() a.tick()
for i, want := range []float64{ for i, want := range []float64{
0.6, 0.49123845184678905, 0.4021920276213837, 0.32928698165641596, 0.6, 0.49123845184678905, 0.4021920276213837, 0.32928698165641596,
0.269597378470333, 0.2207276647028654, 0.18071652714732128, 0.269597378470333, 0.2207276647028654, 0.18071652714732128,
@ -67,7 +67,7 @@ func TestEWMA5(t *testing.T) {
func TestEWMA15(t *testing.T) { func TestEWMA15(t *testing.T) {
a := NewEWMA15() a := NewEWMA15()
a.Update(3) a.Update(3)
a.Tick() a.tick()
for i, want := range []float64{ for i, want := range []float64{
0.6, 0.5613041910189706, 0.5251039914257684, 0.4912384518467888184678905, 0.6, 0.5613041910189706, 0.5251039914257684, 0.4912384518467888184678905,
0.459557003018789, 0.4299187863442732, 0.4021920276213831, 0.459557003018789, 0.4299187863442732, 0.4021920276213831,
@ -84,6 +84,6 @@ 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

@ -119,9 +119,9 @@ func (m *Meter) tick() {
m.a5.Update(n) m.a5.Update(n)
m.a15.Update(n) m.a15.Update(n)
// And trigger them to calculate the rates // And trigger them to calculate the rates
m.a1.Tick() m.a1.tick()
m.a5.Tick() m.a5.tick()
m.a15.Tick() m.a15.tick()
} }
var arbiter = meterTicker{meters: make(map[*Meter]struct{})} var arbiter = meterTicker{meters: make(map[*Meter]struct{})}