Skip to content

Commit fcfd837

Browse files
authored
Merge pull request #2 from mroth/rand-contention
Alternative method to avoid rand contention in highly parallel usage
2 parents 6125a88 + 7a6357d commit fcfd837

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

weightedrand.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,26 @@ func NewChooser(cs ...Choice) Chooser {
4949
}
5050

5151
// Pick returns a single weighted random Choice.Item from the Chooser.
52+
//
53+
// Utilizes global rand as the source of randomness -- you will likely want to
54+
// seed it.
5255
func (chs Chooser) Pick() interface{} {
5356
r := rand.Intn(chs.max) + 1
5457
i := sort.SearchInts(chs.totals, r)
5558
return chs.data[i].Item
5659
}
60+
61+
// PickSource returns a single weighted random Choice.Item from the Chooser,
62+
// utilizing the provided *rand.Rand source rs for randomness.
63+
//
64+
// The primary use-case for this is avoid lock contention from the global random
65+
// source if utilizing Chooser(s) from multiple goroutines in extremely
66+
// high-throughput situations.
67+
//
68+
// It is the responsibility of the caller to ensure the provided rand.Source is
69+
// safe from thread safety issues.
70+
func (chs Chooser) PickSource(rs *rand.Rand) interface{} {
71+
r := rs.Intn(chs.max) + 1
72+
i := sort.SearchInts(chs.totals, r)
73+
return chs.data[i].Item
74+
}

weightedrand_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"math/rand"
66
"strconv"
7+
"sync"
78
"testing"
89
"time"
910
)
@@ -60,6 +61,35 @@ func TestChooser_Pick(t *testing.T) {
6061
verifyFrequencyCounts(t, counts, choices)
6162
}
6263

64+
// TestChooser_PickSource is the same test methodology as TestChooser_Pick, but
65+
// here we use the PickSource method and access the same chooser concurrently
66+
// from multiple different goroutines, each providing its own source of
67+
// randomness.
68+
func TestChooser_PickSource(t *testing.T) {
69+
choices := mockFrequencyChoices(t, testChoices)
70+
chooser := NewChooser(choices...)
71+
t.Log("totals in chooser", chooser.totals)
72+
73+
counts1 := make(map[int]int)
74+
counts2 := make(map[int]int)
75+
var wg sync.WaitGroup
76+
wg.Add(2)
77+
checker := func(counts map[int]int) {
78+
defer wg.Done()
79+
rs := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
80+
for i := 0; i < testIterations/2; i++ {
81+
c := chooser.PickSource(rs)
82+
counts[c.(int)]++
83+
}
84+
}
85+
go checker(counts1)
86+
go checker(counts2)
87+
wg.Wait()
88+
89+
verifyFrequencyCounts(t, counts1, choices)
90+
verifyFrequencyCounts(t, counts2, choices)
91+
}
92+
6393
// Similar to what is used in randutil test, but in randomized order to avoid
6494
// any issues with algorithms that are accidentally dependant on presorted data.
6595
func mockFrequencyChoices(t *testing.T, n int) []Choice {
@@ -127,6 +157,22 @@ func BenchmarkPick(b *testing.B) {
127157
}
128158
}
129159

160+
func BenchmarkPickParallel(b *testing.B) {
161+
for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
162+
b.Run(strconv.Itoa(n), func(b *testing.B) {
163+
choices := mockChoices(n)
164+
chooser := NewChooser(choices...)
165+
b.ResetTimer()
166+
b.RunParallel(func(pb *testing.PB) {
167+
rs := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
168+
for pb.Next() {
169+
chooser.PickSource(rs)
170+
}
171+
})
172+
})
173+
}
174+
}
175+
130176
func mockChoices(n int) []Choice {
131177
choices := make([]Choice, 0, n)
132178
for i := 0; i < n; i++ {

0 commit comments

Comments
 (0)