Skip to content

Commit

Permalink
bench: separate benchmarks for Pick and PickSource
Browse files Browse the repository at this point in the history
As discussed in #26, the changes in go1.21 will remove rand contention
when the (now deprecated) rand.Seed() is not called.

In order to check if this brings performance parity, start to benchmark
both Pick and PickSource functions in parallel, and no longer rand.Seed
in test init.
  • Loading branch information
mroth committed Aug 7, 2023
1 parent 62d9872 commit 0ee7b80
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions weightedrand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func Example() {
* Tests
*******************************************************************************/

func init() {
rand.Seed(time.Now().UTC().UnixNano()) // only necessary prior to go1.20
}

const (
testChoices = 10
testIterations = 1000000
Expand Down Expand Up @@ -246,6 +242,24 @@ func BenchmarkPick(b *testing.B) {
}

func BenchmarkPickParallel(b *testing.B) {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(n)
chooser, err := NewChooser(choices...)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = chooser.Pick()
}
})
})
}
}

func BenchmarkPickSourceParallel(b *testing.B) {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(n)
Expand Down

0 comments on commit 0ee7b80

Please sign in to comment.