ewma.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package metrics
  2. import (
  3. "math"
  4. "sync"
  5. "sync/atomic"
  6. )
  7. // EWMAs continuously calculate an exponentially-weighted moving average
  8. // based on an outside source of clock ticks.
  9. type EWMA interface {
  10. Rate() float64
  11. Snapshot() EWMA
  12. Tick()
  13. Update(int64)
  14. }
  15. // NewEWMA constructs a new EWMA with the given alpha.
  16. func NewEWMA(alpha float64) EWMA {
  17. return &StandardEWMA{alpha: alpha}
  18. }
  19. // NewEWMA1 constructs a new EWMA for a one-minute moving average.
  20. func NewEWMA1() EWMA {
  21. return NewEWMA(1 - math.Exp(-5.0/60.0/1))
  22. }
  23. // NewEWMA5 constructs a new EWMA for a five-minute moving average.
  24. func NewEWMA5() EWMA {
  25. return NewEWMA(1 - math.Exp(-5.0/60.0/5))
  26. }
  27. // NewEWMA15 constructs a new EWMA for a fifteen-minute moving average.
  28. func NewEWMA15() EWMA {
  29. return NewEWMA(1 - math.Exp(-5.0/60.0/15))
  30. }
  31. // EWMASnapshot is a read-only copy of another EWMA.
  32. type EWMASnapshot float64
  33. // Rate returns the rate of events per second at the time the snapshot was
  34. // taken.
  35. func (a EWMASnapshot) Rate() float64 { return float64(a) }
  36. // Snapshot returns the snapshot.
  37. func (a EWMASnapshot) Snapshot() EWMA { return a }
  38. // Tick panics.
  39. func (EWMASnapshot) Tick() {
  40. panic("Tick called on an EWMASnapshot")
  41. }
  42. // Update panics.
  43. func (EWMASnapshot) Update(int64) {
  44. panic("Update called on an EWMASnapshot")
  45. }
  46. // NilEWMA is a no-op EWMA.
  47. type NilEWMA struct{}
  48. // Rate is a no-op.
  49. func (NilEWMA) Rate() float64 { return 0.0 }
  50. // Snapshot is a no-op.
  51. func (NilEWMA) Snapshot() EWMA { return NilEWMA{} }
  52. // Tick is a no-op.
  53. func (NilEWMA) Tick() {}
  54. // Update is a no-op.
  55. func (NilEWMA) Update(n int64) {}
  56. // StandardEWMA is the standard implementation of an EWMA and tracks the number
  57. // of uncounted events and processes them on each tick. It uses the
  58. // sync/atomic package to manage uncounted events.
  59. type StandardEWMA struct {
  60. uncounted int64 // /!\ this should be the first member to ensure 64-bit alignment
  61. alpha float64
  62. rate float64
  63. init bool
  64. mutex sync.Mutex
  65. }
  66. // Rate returns the moving average rate of events per second.
  67. func (a *StandardEWMA) Rate() float64 {
  68. a.mutex.Lock()
  69. defer a.mutex.Unlock()
  70. return a.rate * float64(1e9)
  71. }
  72. // Snapshot returns a read-only copy of the EWMA.
  73. func (a *StandardEWMA) Snapshot() EWMA {
  74. return EWMASnapshot(a.Rate())
  75. }
  76. // Tick ticks the clock to update the moving average. It assumes it is called
  77. // every five seconds.
  78. func (a *StandardEWMA) Tick() {
  79. count := atomic.LoadInt64(&a.uncounted)
  80. atomic.AddInt64(&a.uncounted, -count)
  81. instantRate := float64(count) / float64(5e9)
  82. a.mutex.Lock()
  83. defer a.mutex.Unlock()
  84. if a.init {
  85. a.rate += a.alpha * (instantRate - a.rate)
  86. } else {
  87. a.init = true
  88. a.rate = instantRate
  89. }
  90. }
  91. // Update adds n uncounted events.
  92. func (a *StandardEWMA) Update(n int64) {
  93. atomic.AddInt64(&a.uncounted, n)
  94. }