ewma.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. //
  10. // This is an interface so as to encourage other structs to implement
  11. // the EWMA API as appropriate.
  12. type EWMA interface {
  13. Rate() float64
  14. Tick()
  15. Update(int64)
  16. }
  17. // Create a new EWMA with the given alpha.
  18. func NewEWMA(alpha float64) EWMA {
  19. if UseNilMetrics {
  20. return NilEWMA{}
  21. }
  22. return &StandardEWMA{alpha: alpha}
  23. }
  24. // Create a new EWMA with alpha set for a one-minute moving average.
  25. func NewEWMA1() EWMA {
  26. return NewEWMA(1 - math.Exp(-5.0/60.0/1))
  27. }
  28. // Create a new EWMA with alpha set for a five-minute moving average.
  29. func NewEWMA5() EWMA {
  30. return NewEWMA(1 - math.Exp(-5.0/60.0/5))
  31. }
  32. // Create a new EWMA with alpha set for a fifteen-minute moving average.
  33. func NewEWMA15() EWMA {
  34. return NewEWMA(1 - math.Exp(-5.0/60.0/15))
  35. }
  36. // No-op EWMA.
  37. type NilEWMA struct{}
  38. // Force the compiler to check that NilEWMA implements EWMA.
  39. var _ EWMA = NilEWMA{}
  40. // No-op.
  41. func (a NilEWMA) Rate() float64 { return 0.0 }
  42. // No-op.
  43. func (a NilEWMA) Tick() {}
  44. // No-op.
  45. func (a NilEWMA) Update(n int64) {}
  46. // The standard implementation of an EWMA tracks the number of uncounted
  47. // events and processes them on each tick. It uses the sync/atomic package
  48. // to manage uncounted events.
  49. type StandardEWMA struct {
  50. alpha float64
  51. init bool
  52. mutex sync.Mutex
  53. rate float64
  54. uncounted int64
  55. }
  56. // Force the compiler to check that StandardEWMA implements EWMA.
  57. var _ EWMA = &StandardEWMA{}
  58. // Return the moving average rate of events per second.
  59. func (a *StandardEWMA) Rate() float64 {
  60. a.mutex.Lock()
  61. defer a.mutex.Unlock()
  62. return a.rate * float64(1e9)
  63. }
  64. // Tick the clock to update the moving average.
  65. func (a *StandardEWMA) Tick() {
  66. count := atomic.LoadInt64(&a.uncounted)
  67. atomic.AddInt64(&a.uncounted, -count)
  68. instantRate := float64(count) / float64(5e9)
  69. a.mutex.Lock()
  70. defer a.mutex.Unlock()
  71. if a.init {
  72. a.rate += a.alpha * (instantRate - a.rate)
  73. } else {
  74. a.init = true
  75. a.rate = instantRate
  76. }
  77. }
  78. // Add n uncounted events.
  79. func (a *StandardEWMA) Update(n int64) {
  80. atomic.AddInt64(&a.uncounted, n)
  81. }