ewma.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. in chan bool
  23. out chan float64
  24. }
  25. // Force the compiler to check that StandardEWMA implements EWMA.
  26. var _ EWMA = &StandardEWMA{}
  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, make(chan bool), make(chan float64)}
  31. go a.arbiter()
  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.out * float64(1e9)
  49. }
  50. // Tick the clock to update the moving average.
  51. func (a *StandardEWMA) Tick() {
  52. a.in <- 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) arbiter() {
  61. var initialized bool
  62. var rate float64
  63. for {
  64. select {
  65. case <-a.in:
  66. count := atomic.LoadInt64(&a.uncounted)
  67. atomic.AddInt64(&a.uncounted, -count)
  68. instantRate := float64(count) / float64(5e9)
  69. if initialized {
  70. rate += a.alpha * (instantRate - rate)
  71. } else {
  72. initialized = true
  73. rate = instantRate
  74. }
  75. case a.out <- rate:
  76. }
  77. }
  78. }