Skip to content

Commit

Permalink
Add test for probability convergance
Browse files Browse the repository at this point in the history
  • Loading branch information
leszko committed Sep 29, 2023
1 parent 73c1076 commit 3355766
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion server/selection_algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"github.com/golang/glog"
"math"
"math/rand"
"time"
)

var random = rand.New(rand.NewSource(time.Now().UnixNano()))

type ProbabilitySelectionAlgorithm struct {
MinPerfScore float64

Expand Down Expand Up @@ -90,7 +93,7 @@ func selectBy(probabilities map[ethcommon.Address]float64) ethcommon.Address {
cumProbs = append(cumProbs, cumProb)
}

r := rand.Float64()
r := random.Float64()
for i, cumProb := range cumProbs {
if r <= cumProb {
return addrs[i]
Expand Down
19 changes: 14 additions & 5 deletions server/selection_algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,23 @@ func TestCalculateProbabilities(t *testing.T) {
}

func TestSelectByProbability(t *testing.T) {
// use constant seed to avoid test flakiness
random.Seed(0)
iters := 100000

probs := map[ethcommon.Address]float64{
ethcommon.HexToAddress("0x0000000000000000000000000000000000000001"): 0.1,
ethcommon.HexToAddress("0x0000000000000000000000000000000000000002"): 0.3,
ethcommon.HexToAddress("0x0000000000000000000000000000000000000001"): 0.11,
ethcommon.HexToAddress("0x0000000000000000000000000000000000000002"): 0.29,
ethcommon.HexToAddress("0x0000000000000000000000000000000000000003"): 0.6,
}

selected := selectBy(probs)
selected := map[ethcommon.Address]int64{}
for i := 0; i < iters; i++ {
selected[selectBy(probs)]++
}

// all we can check is that one of input addresses was selected
require.Contains(t, probs, selected)
for addr, prob := range probs {
selectedRatio := float64(selected[addr]) / float64(iters)
require.InDelta(t, prob, selectedRatio, 0.01)
}
}

0 comments on commit 3355766

Please sign in to comment.