EMWA.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // includes code from
  2. // https://raw.githubusercontent.com/rcrowley/go-metrics/master/sample.go
  3. // Copyright 2012 Richard Crowley. All rights reserved.
  4. package metrics
  5. import (
  6. "math"
  7. "sync"
  8. "sync/atomic"
  9. )
  10. // EWMAs continuously calculate an exponentially-weighted moving average
  11. // based on an outside source of clock ticks.
  12. type EWMA interface {
  13. Rate() float64
  14. Snapshot() EWMA
  15. Tick()
  16. Update(int64)
  17. }
  18. // NewEWMA constructs a new EWMA with the given alpha.
  19. func NewEWMA(alpha float64) EWMA {
  20. if UseNilMetrics {
  21. return NilEWMA{}
  22. }
  23. return &StandardEWMA{alpha: alpha}
  24. }
  25. // NewEWMA1 constructs a new EWMA for a one-minute moving average.
  26. func NewEWMA1() EWMA {
  27. return NewEWMA(1 - math.Exp(-5.0/60.0/1))
  28. }
  29. // NewEWMA5 constructs a new EWMA for a five-minute moving average.
  30. func NewEWMA5() EWMA {
  31. return NewEWMA(1 - math.Exp(-5.0/60.0/5))
  32. }
  33. // NewEWMA15 constructs a new EWMA for a fifteen-minute moving average.
  34. func NewEWMA15() EWMA {
  35. return NewEWMA(1 - math.Exp(-5.0/60.0/15))
  36. }
  37. // EWMASnapshot is a read-only copy of another EWMA.
  38. type EWMASnapshot float64
  39. // Rate returns the rate of events per second at the time the snapshot was
  40. // taken.
  41. func (a EWMASnapshot) Rate() float64 { return float64(a) }
  42. // Snapshot returns the snapshot.
  43. func (a EWMASnapshot) Snapshot() EWMA { return a }
  44. // Tick panics.
  45. func (EWMASnapshot) Tick() {
  46. panic("Tick called on an EWMASnapshot")
  47. }
  48. // Update panics.
  49. func (EWMASnapshot) Update(int64) {
  50. panic("Update called on an EWMASnapshot")
  51. }
  52. // NilEWMA is a no-op EWMA.
  53. type NilEWMA struct{}
  54. // Rate is a no-op.
  55. func (NilEWMA) Rate() float64 { return 0.0 }
  56. // Snapshot is a no-op.
  57. func (NilEWMA) Snapshot() EWMA { return NilEWMA{} }
  58. // Tick is a no-op.
  59. func (NilEWMA) Tick() {}
  60. // Update is a no-op.
  61. func (NilEWMA) Update(n int64) {}
  62. // StandardEWMA is the standard implementation of an EWMA and tracks the number
  63. // of uncounted events and processes them on each tick. It uses the
  64. // sync/atomic package to manage uncounted events.
  65. type StandardEWMA struct {
  66. uncounted int64 // /!\ this should be the first member to ensure 64-bit alignment
  67. alpha float64
  68. rate float64
  69. init bool
  70. mutex sync.Mutex
  71. }
  72. // Rate returns the moving average rate of events per second.
  73. func (a *StandardEWMA) Rate() float64 {
  74. a.mutex.Lock()
  75. defer a.mutex.Unlock()
  76. return a.rate * float64(1e9)
  77. }
  78. // Snapshot returns a read-only copy of the EWMA.
  79. func (a *StandardEWMA) Snapshot() EWMA {
  80. return EWMASnapshot(a.Rate())
  81. }
  82. // Tick ticks the clock to update the moving average. It assumes it is called
  83. // every five seconds.
  84. func (a *StandardEWMA) Tick() {
  85. count := atomic.LoadInt64(&a.uncounted)
  86. atomic.AddInt64(&a.uncounted, -count)
  87. instantRate := float64(count) / float64(5e9)
  88. a.mutex.Lock()
  89. defer a.mutex.Unlock()
  90. if a.init {
  91. a.rate += a.alpha * (instantRate - a.rate)
  92. } else {
  93. a.init = true
  94. a.rate = instantRate
  95. }
  96. }
  97. // Update adds n uncounted events.
  98. func (a *StandardEWMA) Update(n int64) {
  99. atomic.AddInt64(&a.uncounted, n)
  100. }