From f45361e8ce9679a481bddb557b0d93d44e880f4d Mon Sep 17 00:00:00 2001 From: David Symons Date: Fri, 2 Jul 2021 18:03:20 +1000 Subject: [PATCH] Tweak per-interval chance calculation --- util/util.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/util/util.go b/util/util.go index ceb17fc8..39672d97 100644 --- a/util/util.go +++ b/util/util.go @@ -2,6 +2,7 @@ package util import ( "fmt" + "math" "math/rand" "strconv" "strings" @@ -17,7 +18,7 @@ const ( Kitchen24 = "15:04" // a time format that just cares about the day and month. YearDay = "Jan_2" - + // default annotation prefix for configuration overrides DefaultBaseAnnotation = "chaos.alpha.kubernetes.io" ) @@ -148,7 +149,15 @@ func ParseFrequency(text string, interval time.Duration) (float64, error) { return 0, fmt.Errorf("unknown time period, %v", parts[1]) } - chance := (float64(interval) / float64(period)) * frequency + attempts := float64(period) / float64(interval) / frequency + + // We want a 75% chance of a pod being terminated once in the given number of attempts + // Formula for calculating probability of something occurring once in "n" attempts... + // p = 1 - math.Pow(1 - c, n) + // where p = probability, c = chance per attempt, n = number of attempts + // The equation below inverts that formula to calculate that percent chance we need + chance := 1 - math.Pow(1-0.75, (1/attempts)) + return chance, nil }