Skip to content

Commit

Permalink
bench: restore comparative benches as example pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
mroth committed Oct 9, 2020
1 parent 12a8992 commit 31ddd19
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 30 deletions.
114 changes: 114 additions & 0 deletions examples/compbench/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Package compbench is used to generate informal comparative benchmarks vs
// randutil.
package compbench

import (
"math/rand"
"strconv"
"testing"
"time"

"github.com/jmcvetta/randutil"
"github.com/mroth/weightedrand"
)

const BMMinChoices = 10
const BMMaxChoices = 1000000

func BenchmarkMultiple(b *testing.B) {
b.Run("jmc_randutil", func(b *testing.B) {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := convertChoices(b, mockChoices(b, n))
b.ResetTimer()
for i := 0; i < b.N; i++ {
randutil.WeightedChoice(choices)
}
})
}
})

b.Run("weightedrand", func(b *testing.B) {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(b, n)
chs := weightedrand.NewChooser(choices...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
chs.Pick()
}
})
}
})

b.Run("wr-parallel", func(b *testing.B) {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(b, n)
chs := weightedrand.NewChooser(choices...)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
rs := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
for pb.Next() {
chs.PickSource(rs)
}
})
})
}
})
}

// The single usage case is an anti-pattern for the intended usage of this
// library. Might as well keep some optional benchmarks for that to illustrate
// the point.
func BenchmarkSingle(b *testing.B) {
if testing.Short() {
b.Skip()
}

b.Run("jmc_randutil", func(b *testing.B) {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := convertChoices(b, mockChoices(b, n))
b.ResetTimer()
for i := 0; i < b.N; i++ {
randutil.WeightedChoice(choices)
}
})
}
})

b.Run("weightedrand", func(b *testing.B) {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(b, n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
chs := weightedrand.NewChooser(choices...)
chs.Pick()
}
})
}
})
}

func mockChoices(tb testing.TB, n int) []weightedrand.Choice {
tb.Helper()
choices := make([]weightedrand.Choice, 0, n)
for i := 0; i < n; i++ {
s := '🥑'
w := rand.Intn(10)
c := weightedrand.NewChoice(s, uint(w))
choices = append(choices, c)
}
return choices
}

func convertChoices(tb testing.TB, cs []weightedrand.Choice) []randutil.Choice {
tb.Helper()
res := make([]randutil.Choice, len(cs))
for i, c := range cs {
res[i] = randutil.Choice{Weight: int(c.Weight), Item: c.Item}
}
return res
}
10 changes: 10 additions & 0 deletions examples/compbench/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/mroth/weightedrand/examples/compbench

go 1.15

require (
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff
github.com/mroth/weightedrand v0.0.0
)

replace github.com/mroth/weightedrand => ../..
2 changes: 2 additions & 0 deletions examples/compbench/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff h1:6NvhExg4omUC9NfA+l4Oq3ibNNeJUdiAF3iBVB0PlDk=
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff/go.mod h1:ddfPX8Z28YMjiqoaJhNBzWHapTHXejnB5cDCUWDwriw=
36 changes: 6 additions & 30 deletions weightedrand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func verifyFrequencyCounts(t *testing.T, counts map[int]int, choices []Choice) {
* Benchmarks
*******************************************************************************/

const BMminChoices = 10
const BMmaxChoices = 1000000
const BMMinChoices = 10
const BMMaxChoices = 1000000

func BenchmarkNewChooser(b *testing.B) {
for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(n)
b.ResetTimer()
Expand All @@ -144,7 +144,7 @@ func BenchmarkNewChooser(b *testing.B) {
}

func BenchmarkPick(b *testing.B) {
for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(n)
chooser := NewChooser(choices...)
Expand All @@ -158,7 +158,7 @@ func BenchmarkPick(b *testing.B) {
}

func BenchmarkPickParallel(b *testing.B) {
for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
b.Run(strconv.Itoa(n), func(b *testing.B) {
choices := mockChoices(n)
chooser := NewChooser(choices...)
Expand All @@ -176,34 +176,10 @@ func BenchmarkPickParallel(b *testing.B) {
func mockChoices(n int) []Choice {
choices := make([]Choice, 0, n)
for i := 0; i < n; i++ {
s := "⚽️"
s := '🥑'
w := rand.Intn(10)
c := NewChoice(s, uint(w))
choices = append(choices, c)
}
return choices
}

// This following is a historic artifact from comparative benchmarking with
// randutil, however it is not critical to ongoing development.

// func BenchmarkRandutil(b *testing.B) {
// if testing.Short() {
// b.Skip()
// }
// for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
// b.Run(strconv.Itoa(n), func(b *testing.B) {
// b.StopTimer()
// choices := mockChoices(n)
// choicesR := make([]randutil.Choice, len(choices), len(choices))
// for i, c := range choices {
// choicesR[i] = randutil.Choice{Weight: c.Weight, Item: c.Item}
// }
// b.StartTimer()

// for i := 0; i < b.N; i++ {
// randutil.WeightedChoice(choicesR)
// }
// })
// }
// }

0 comments on commit 31ddd19

Please sign in to comment.