ewma.go 1.9 KB

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