ewma.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.
  19. type StandardEWMA struct {
  20. alpha float64
  21. uncounted int64
  22. rate float64
  23. initialized bool
  24. tick chan bool
  25. out chan float64
  26. }
  27. // Create a new EWMA with the given alpha. Create the clock channel and
  28. // start the ticker goroutine.
  29. func NewEWMA(alpha float64) *StandardEWMA {
  30. a := &StandardEWMA{alpha, 0, 0.0, false, make(chan bool), make(chan float64)}
  31. go a.ticker()
  32. return a
  33. }
  34. // Create a new EWMA with alpha set for a one-minute moving average.
  35. func NewEWMA1() *StandardEWMA {
  36. return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 1))
  37. }
  38. // Create a new EWMA with alpha set for a five-minute moving average.
  39. func NewEWMA5() *StandardEWMA {
  40. return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 5))
  41. }
  42. // Create a new EWMA with alpha set for a fifteen-minute moving average.
  43. func NewEWMA15() *StandardEWMA {
  44. return NewEWMA(1 - math.Exp(-5.0 / 60.0 / 15))
  45. }
  46. // Return the moving average rate of events per second.
  47. func (a *StandardEWMA) Rate() float64 {
  48. return a.rate * float64(1e9)
  49. }
  50. // Tick the clock to update the moving average.
  51. func (a *StandardEWMA) Tick() {
  52. a.tick <- true
  53. }
  54. // Add n uncounted events.
  55. func (a *StandardEWMA) Update(n int64) {
  56. atomic.AddInt64(&a.uncounted, n)
  57. }
  58. // On each clock tick, update the moving average to reflect the number of
  59. // events seen since the last tick.
  60. func (a *StandardEWMA) ticker() {
  61. for {
  62. select {
  63. case <-a.tick:
  64. count := atomic.LoadInt64(&a.uncounted)
  65. atomic.AddInt64(&a.uncounted, -count)
  66. instantRate := float64(count) / float64(5e9)
  67. if a.initialized {
  68. a.rate += a.alpha * (instantRate - a.rate)
  69. } else {
  70. a.initialized = true
  71. a.rate = instantRate
  72. }
  73. case a.out <- a.rate:
  74. }
  75. }
  76. }