Skip to content

Commit 09d46aa

Browse files
Tweak per-interval chance calculation
1 parent 133e6d7 commit 09d46aa

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

util/util.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package util
22

33
import (
44
"fmt"
5+
"math"
56
"math/rand"
67
"strconv"
78
"strings"
@@ -17,7 +18,7 @@ const (
1718
Kitchen24 = "15:04"
1819
// a time format that just cares about the day and month.
1920
YearDay = "Jan_2"
20-
21+
// default annotation prefix for configuration overrides
2122
DefaultBaseAnnotation = "chaos.alpha.kubernetes.io"
2223
)
2324

@@ -148,7 +149,15 @@ func ParseFrequency(text string, interval time.Duration) (float64, error) {
148149
return 0, fmt.Errorf("unknown time period, %v", parts[1])
149150
}
150151

151-
chance := (float64(interval) / float64(period)) * frequency
152+
attempts := float64(period) / float64(interval) / frequency
153+
154+
// We want a 75% chance of a pod being terminated once in the given number of attempts
155+
// Formula for calculating probability of something occurring once in "n" attempts...
156+
// p = 1 - math.Pow(1 - c, n)
157+
// where p = probability, c = chance per attempt, n = number of attempts
158+
// The equation below inverts that formula to calculate that percent chance we need
159+
chance := 1 - math.Pow(1-0.75, (1/attempts))
160+
152161
return chance, nil
153162
}
154163

util/util_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,16 @@ func (suite *Suite) TestParseFrequency() {
375375
}{
376376
{
377377
"1 / hour",
378-
0.166666667,
378+
0.206,
379379
}, {
380380
"1 / minute",
381-
10.0,
381+
1.0,
382382
}, {
383383
"2.5 / hour",
384-
0.416666667,
384+
0.4387,
385385
}, {
386386
"60 / day",
387-
0.416666667,
387+
0.4387,
388388
},
389389
} {
390390
result, err := ParseFrequency(tt.given, interval)

0 commit comments

Comments
 (0)