Created
June 7, 2019 13:25
-
-
Save kirugan/9f15120481f0b0f430963cddd67afe6e to your computer and use it in GitHub Desktop.
Simple script that allows you to get experience with failure rate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func init() { | |
rand.Seed(time.Now().UnixNano()) | |
} | |
const failureRate = 25 | |
const numOfExperiments = 1e3 | |
func main() { | |
component1 := component(failureRate) | |
component2 := component(failureRate) | |
var results = make(map[bool]int) | |
for i := 0; i < numOfExperiments; i++ { | |
r := callAnd(component1, component2) | |
results[r]++ | |
} | |
fmt.Println("Ok: ", results[true]) | |
fmt.Println("Failures: ", results[false]) | |
fmt.Printf("Failure rate %v\n", float64(results[false]) / float64(results[true] + results[false])) | |
} | |
type callable func() bool | |
// отказ одного из компонентов приводит к отказу всего метода | |
func callAnd(components ...callable) bool { | |
for _, component := range components { | |
if !component() { | |
return false | |
} | |
} | |
return true | |
} | |
func callOr(components ...callable) bool { | |
for _, component := range components { | |
if component() { | |
return true | |
} | |
} | |
return false | |
} | |
func component(failureRate int) callable { | |
if failureRate < 0 || failureRate > 100 { | |
panic("wrong failure rate") | |
} | |
return func() bool { | |
r := rand.Intn(100) | |
return failureRate <= r | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment