summaryrefslogtreecommitdiff
path: root/ma/ma.go
diff options
context:
space:
mode:
authorWill Hawkins <[email protected]>2021-12-16 18:03:00 -0500
committerWill Hawkins <[email protected]>2021-12-16 18:03:00 -0500
commita1847cfe95e4fb73867cf2aeb5c41e332a7fef1e (patch)
tree8019a5855388866340c9339c5c8baadc05fc8699 /ma/ma.go
parentbb48aa9f23d3b74aa3e31f395deaf6fdd1618391 (diff)
Improve robustness
Improve robustness of the client in the case(s) where a network goes away in the middle of the test. Report errors properly in these cases and gracefully end the test.
Diffstat (limited to 'ma/ma.go')
-rw-r--r--ma/ma.go7
1 files changed, 5 insertions, 2 deletions
diff --git a/ma/ma.go b/ma/ma.go
index 8d06a67..f11dbbe 100644
--- a/ma/ma.go
+++ b/ma/ma.go
@@ -21,6 +21,7 @@ func NewMovingAverage(intervals int) *MovingAverage {
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
}
@@ -40,9 +41,11 @@ func (ma *MovingAverage) AllSequentialIncreasesLessThan(limit float64) bool {
return false
}
- previous := ma.instants[ma.index]
+ // 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 := (ma.index + i) % ma.intervals
+ currentIndex := (oldestIndex + i) % ma.intervals
current := ma.instants[currentIndex]
percentChange := utilities.SignedPercentDifference(current, previous)
previous = current