Skip to content

Commit f45361e

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

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
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

0 commit comments

Comments
 (0)