ewma.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package metrics
  2. import (
  3. "math"
  4. "sync/atomic"
  5. )
  6. // EWMAs continuously calculate an exponentially-weighted moving average
  7. // based on an outside source of clock ticks.
  8. //
  9. // This is an interface so as to encourage other structs to implement
  10. // the EWMA API as appropriate.
  11. type EWMA interface {
  12. Rate() float64
  13. Tick()
  14. Update(int64)
  15. }
  16. // The standard implementation of an EWMA tracks the number of uncounted
  17. // events and processes them on each tick. It uses the sync/atomic package
  18. // to manage uncounted events. When the latest weeklies land in a release,
  19. // atomic.LoadInt64 will be available and this code will become safe on
  20. // 32-bit architectures.
  21. type StandardEWMA struct {
  22. alpha float64
  23. uncounted int64
  24. rate float64
  25. initialized bool
  26. tick chan bool
  27. }
  28. // Create a new EWMA with the given alpha. Create the clock channel and
  29. // start the ticker goroutine.
  30. func NewEWMA(alpha float64) *StandardEWMA {
  31. a := &StandardEWMA{alpha, 0, 0.0, false, make(chan bool)}
  32. go a.ticker()
  33. return a
  34. }
  35. // Create a new EWMA with alpha set for a one-minute moving average.
  36. func NewEWMA1() *StandardEWMA {
  37. return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 1))
  38. }
  39. // Create a new EWMA with alpha set for a five-minute moving average.
  40. func NewEWMA5() *StandardEWMA {
  41. return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 5))
  42. }
  43. // Create a new EWMA with alpha set for a fifteen-minute moving average.
  44. func NewEWMA15() *StandardEWMA {
  45. return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 15))
  46. }
  47. // Return the moving average rate of events per second.
  48. func (a *StandardEWMA) Rate() float64 {
  49. return a.rate * float64(1e9)
  50. }
  51. // Tick the clock to update the moving average.
  52. func (a *StandardEWMA) Tick() {
  53. a.tick <- true
  54. }
  55. // Add n uncounted events.
  56. func (a *StandardEWMA) Update(n int64) {
  57. atomic.AddInt64(&a.uncounted, n)
  58. }
  59. // On each clock tick, update the moving average to reflect the number of
  60. // events seen since the last tick.
  61. func (a *StandardEWMA) ticker() {
  62. for <-a.tick {
  63. count := a.uncounted
  64. atomic.AddInt64(&a.uncounted, -count)
  65. instantRate := float64(count) / float64(5e9)
  66. if a.initialized {
  67. a.rate += a.alpha * (instantRate - a.rate)
  68. } else {
  69. a.initialized = true
  70. a.rate = instantRate
  71. }
  72. }
  73. }