|
| 1 | +package measurements |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | +) |
| 7 | + |
| 8 | +// WindowlessMovingPercentile implements a moving percentile. |
| 9 | +// This implementation uses a windowless calculation that while not strictly always accurate, |
| 10 | +// provides a very close estimation in O(1) time and space. |
| 11 | +// Much credit goes to Martin Jambon here: https://mjambon.com/2016-07-23-moving-percentile/ |
| 12 | +// a copy can be found in github.com/platinummonkey/go-concurrency-limits/docs/assets/moving_percentile_reference.pdf |
| 13 | +// and this is a port of the OCaml implementation provided in that reference. |
| 14 | +type WindowlessMovingPercentile struct { |
| 15 | + p float64 |
| 16 | + deltaInitial float64 |
| 17 | + q float64 |
| 18 | + |
| 19 | + value float64 |
| 20 | + delta float64 |
| 21 | + deltaState *SimpleMovingVariance |
| 22 | + |
| 23 | + seenCount int |
| 24 | + |
| 25 | + mu sync.RWMutex |
| 26 | +} |
| 27 | + |
| 28 | +// NewWindowlessMovingPercentile creates a new Windowless Moving Percentile |
| 29 | +// p - percentile requested, accepts (0,1) |
| 30 | +// deltaInitial - the initial delta value, here 0 is acceptable if you expect it to be rather stable at start, otherwise |
| 31 | +// choose a larger value. This would be estimated: `delta := stdev * r` where `r` is a user chosen |
| 32 | +// constant. Good values are generally from 0.001 to 0.01 |
| 33 | +// movingAvgAlphaAvg - this is the alpha value for the simple moving average. A good start is 0.05. Accepts [0,1] |
| 34 | +// movingVarianceAlphaAvg - this is the alpha value for the simple moving variance. A good start is 0.05. Accepts [0,1] |
| 35 | +func NewWindowlessMovingPercentile( |
| 36 | + p float64, // percentile requested |
| 37 | + deltaInitial float64, |
| 38 | + movingAvgAlphaAvg float64, |
| 39 | + movingVarianceAlphaVar float64, |
| 40 | +) (*WindowlessMovingPercentile, error) { |
| 41 | + if p <= 0 || p >= 1 { |
| 42 | + return nil, fmt.Errorf("p must be between (0,1)") |
| 43 | + } |
| 44 | + q := 1 - p |
| 45 | + if q <= 0 || q >= 1 { |
| 46 | + return nil, fmt.Errorf("calculated q must be between (0,1)") |
| 47 | + } |
| 48 | + variance, err := NewSimpleMovingVariance(movingAvgAlphaAvg, movingVarianceAlphaVar) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + return &WindowlessMovingPercentile{ |
| 54 | + p: p, |
| 55 | + q: q, |
| 56 | + deltaInitial: deltaInitial, |
| 57 | + delta: deltaInitial, |
| 58 | + deltaState: variance, |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Add a single sample and update the internal state. |
| 63 | +// returns true if the internal state was updated, also return the current value. |
| 64 | +func (m *WindowlessMovingPercentile) Add(value float64) (float64, bool) { |
| 65 | + m.mu.Lock() |
| 66 | + defer m.mu.Unlock() |
| 67 | + return m.add(value) |
| 68 | +} |
| 69 | + |
| 70 | +func (m *WindowlessMovingPercentile) add(value float64) (float64, bool) { |
| 71 | + changed := false |
| 72 | + if m.seenCount < 2 { |
| 73 | + // we only need 2 samples to continue |
| 74 | + m.seenCount++ |
| 75 | + } |
| 76 | + originalDelta := m.delta |
| 77 | + stdev, _ := m.deltaState.Add(value) |
| 78 | + if m.seenCount >= 2 { |
| 79 | + m.delta = m.deltaInitial * stdev |
| 80 | + if m.delta != originalDelta { |
| 81 | + changed = true |
| 82 | + } |
| 83 | + } |
| 84 | + newValue := float64(m.value) |
| 85 | + if m.seenCount == 1 { |
| 86 | + newValue = value |
| 87 | + changed = true |
| 88 | + } else if value < m.value { |
| 89 | + newValue = m.value - m.delta/m.p |
| 90 | + } else if value > m.value { |
| 91 | + newValue = m.value + m.delta/(1-m.p) |
| 92 | + } |
| 93 | + // else the same |
| 94 | + if newValue != m.value { |
| 95 | + changed = true |
| 96 | + } |
| 97 | + m.value = newValue |
| 98 | + return m.value, changed |
| 99 | +} |
| 100 | + |
| 101 | +// Get the current value. |
| 102 | +func (m *WindowlessMovingPercentile) Get() float64 { |
| 103 | + m.mu.RLock() |
| 104 | + defer m.mu.RUnlock() |
| 105 | + return m.value |
| 106 | +} |
| 107 | + |
| 108 | +// Reset the internal state as if no samples were ever added. |
| 109 | +func (m *WindowlessMovingPercentile) Reset() { |
| 110 | + m.mu.Lock() |
| 111 | + m.value = 0 |
| 112 | + m.seenCount = 0 |
| 113 | + m.mu.Unlock() |
| 114 | +} |
| 115 | + |
| 116 | +// Update will update the value given an operation function |
| 117 | +func (m *WindowlessMovingPercentile) Update(operation func(value float64) float64) { |
| 118 | + m.mu.Lock() |
| 119 | + defer m.mu.Unlock() |
| 120 | + newValue, _ := m.add(m.value) |
| 121 | + m.value = operation(newValue) |
| 122 | +} |
0 commit comments