github.com/bloxroute-labs/bor@v0.1.4/metrics/ewma.go (about)

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