Skip to content

Commit

Permalink
Tweak per-interval chance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
multimac committed Jul 2, 2021
1 parent 133e6d7 commit 09d46aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 11 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"math"
"math/rand"
"strconv"
"strings"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,16 @@ func (suite *Suite) TestParseFrequency() {
}{
{
"1 / hour",
0.166666667,
0.206,
}, {
"1 / minute",
10.0,
1.0,
}, {
"2.5 / hour",
0.416666667,
0.4387,
}, {
"60 / day",
0.416666667,
0.4387,
},
} {
result, err := ParseFrequency(tt.given, interval)
Expand Down

0 comments on commit 09d46aa

Please sign in to comment.