blob: 7f5f39f082eeb2c4669f314a2a8bd2dc1b31ee01 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
/*
* This file is part of Go Responsiveness.
*
* Go Responsiveness is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
* Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>.
*/
package ma
import (
"github.com/network-quality/goresponsiveness/saturating"
"github.com/network-quality/goresponsiveness/utilities"
)
// Convert this to a Type Parameterized interface when they are available
// in Go (1.18).
type MovingAverage struct {
intervals int
instants []float64
index int
divisor *saturating.SaturatingInt
}
func NewMovingAverage(intervals int) *MovingAverage {
return &MovingAverage{
instants: make([]float64, intervals),
intervals: intervals,
divisor: saturating.NewSaturatingInt(intervals),
}
}
func (ma *MovingAverage) AddMeasurement(measurement float64) {
ma.instants[ma.index] = measurement
ma.divisor.Add(1)
// Invariant: ma.index always points to the oldest measurement
ma.index = (ma.index + 1) % ma.intervals
}
func (ma *MovingAverage) CalculateAverage() float64 {
total := float64(0)
for i := 0; i < ma.intervals; i++ {
total += ma.instants[i]
}
return float64(total) / float64(ma.divisor.Value())
}
func (ma *MovingAverage) AllSequentialIncreasesLessThan(limit float64) bool {
// If we have not yet accumulated a complete set of intervals,
// this is false.
if ma.divisor.Value() != ma.intervals {
return false
}
// Invariant: ma.index always points to the oldest (see AddMeasurement
// above)
oldestIndex := ma.index
previous := ma.instants[oldestIndex]
for i := 1; i < ma.intervals; i++ {
currentIndex := (oldestIndex + i) % ma.intervals
current := ma.instants[currentIndex]
percentChange := utilities.SignedPercentDifference(current, previous)
previous = current
if percentChange > limit {
return false
}
}
return true
}
|