Skip to content

Commit 31ddd19

Browse files
committed
bench: restore comparative benches as example pkg
1 parent 12a8992 commit 31ddd19

4 files changed

Lines changed: 132 additions & 30 deletions

File tree

examples/compbench/bench_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Package compbench is used to generate informal comparative benchmarks vs
2+
// randutil.
3+
package compbench
4+
5+
import (
6+
"math/rand"
7+
"strconv"
8+
"testing"
9+
"time"
10+
11+
"github.com/jmcvetta/randutil"
12+
"github.com/mroth/weightedrand"
13+
)
14+
15+
const BMMinChoices = 10
16+
const BMMaxChoices = 1000000
17+
18+
func BenchmarkMultiple(b *testing.B) {
19+
b.Run("jmc_randutil", func(b *testing.B) {
20+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
21+
b.Run(strconv.Itoa(n), func(b *testing.B) {
22+
choices := convertChoices(b, mockChoices(b, n))
23+
b.ResetTimer()
24+
for i := 0; i < b.N; i++ {
25+
randutil.WeightedChoice(choices)
26+
}
27+
})
28+
}
29+
})
30+
31+
b.Run("weightedrand", func(b *testing.B) {
32+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
33+
b.Run(strconv.Itoa(n), func(b *testing.B) {
34+
choices := mockChoices(b, n)
35+
chs := weightedrand.NewChooser(choices...)
36+
b.ResetTimer()
37+
for i := 0; i < b.N; i++ {
38+
chs.Pick()
39+
}
40+
})
41+
}
42+
})
43+
44+
b.Run("wr-parallel", func(b *testing.B) {
45+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
46+
b.Run(strconv.Itoa(n), func(b *testing.B) {
47+
choices := mockChoices(b, n)
48+
chs := weightedrand.NewChooser(choices...)
49+
b.ResetTimer()
50+
b.RunParallel(func(pb *testing.PB) {
51+
rs := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
52+
for pb.Next() {
53+
chs.PickSource(rs)
54+
}
55+
})
56+
})
57+
}
58+
})
59+
}
60+
61+
// The single usage case is an anti-pattern for the intended usage of this
62+
// library. Might as well keep some optional benchmarks for that to illustrate
63+
// the point.
64+
func BenchmarkSingle(b *testing.B) {
65+
if testing.Short() {
66+
b.Skip()
67+
}
68+
69+
b.Run("jmc_randutil", func(b *testing.B) {
70+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
71+
b.Run(strconv.Itoa(n), func(b *testing.B) {
72+
choices := convertChoices(b, mockChoices(b, n))
73+
b.ResetTimer()
74+
for i := 0; i < b.N; i++ {
75+
randutil.WeightedChoice(choices)
76+
}
77+
})
78+
}
79+
})
80+
81+
b.Run("weightedrand", func(b *testing.B) {
82+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
83+
b.Run(strconv.Itoa(n), func(b *testing.B) {
84+
choices := mockChoices(b, n)
85+
b.ResetTimer()
86+
for i := 0; i < b.N; i++ {
87+
chs := weightedrand.NewChooser(choices...)
88+
chs.Pick()
89+
}
90+
})
91+
}
92+
})
93+
}
94+
95+
func mockChoices(tb testing.TB, n int) []weightedrand.Choice {
96+
tb.Helper()
97+
choices := make([]weightedrand.Choice, 0, n)
98+
for i := 0; i < n; i++ {
99+
s := '🥑'
100+
w := rand.Intn(10)
101+
c := weightedrand.NewChoice(s, uint(w))
102+
choices = append(choices, c)
103+
}
104+
return choices
105+
}
106+
107+
func convertChoices(tb testing.TB, cs []weightedrand.Choice) []randutil.Choice {
108+
tb.Helper()
109+
res := make([]randutil.Choice, len(cs))
110+
for i, c := range cs {
111+
res[i] = randutil.Choice{Weight: int(c.Weight), Item: c.Item}
112+
}
113+
return res
114+
}

examples/compbench/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/mroth/weightedrand/examples/compbench
2+
3+
go 1.15
4+
5+
require (
6+
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff
7+
github.com/mroth/weightedrand v0.0.0
8+
)
9+
10+
replace github.com/mroth/weightedrand => ../..

examples/compbench/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff h1:6NvhExg4omUC9NfA+l4Oq3ibNNeJUdiAF3iBVB0PlDk=
2+
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff/go.mod h1:ddfPX8Z28YMjiqoaJhNBzWHapTHXejnB5cDCUWDwriw=

weightedrand_test.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ func verifyFrequencyCounts(t *testing.T, counts map[int]int, choices []Choice) {
127127
* Benchmarks
128128
*******************************************************************************/
129129

130-
const BMminChoices = 10
131-
const BMmaxChoices = 1000000
130+
const BMMinChoices = 10
131+
const BMMaxChoices = 1000000
132132

133133
func BenchmarkNewChooser(b *testing.B) {
134-
for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
134+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
135135
b.Run(strconv.Itoa(n), func(b *testing.B) {
136136
choices := mockChoices(n)
137137
b.ResetTimer()
@@ -144,7 +144,7 @@ func BenchmarkNewChooser(b *testing.B) {
144144
}
145145

146146
func BenchmarkPick(b *testing.B) {
147-
for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
147+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
148148
b.Run(strconv.Itoa(n), func(b *testing.B) {
149149
choices := mockChoices(n)
150150
chooser := NewChooser(choices...)
@@ -158,7 +158,7 @@ func BenchmarkPick(b *testing.B) {
158158
}
159159

160160
func BenchmarkPickParallel(b *testing.B) {
161-
for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
161+
for n := BMMinChoices; n <= BMMaxChoices; n *= 10 {
162162
b.Run(strconv.Itoa(n), func(b *testing.B) {
163163
choices := mockChoices(n)
164164
chooser := NewChooser(choices...)
@@ -176,34 +176,10 @@ func BenchmarkPickParallel(b *testing.B) {
176176
func mockChoices(n int) []Choice {
177177
choices := make([]Choice, 0, n)
178178
for i := 0; i < n; i++ {
179-
s := "⚽️"
179+
s := '🥑'
180180
w := rand.Intn(10)
181181
c := NewChoice(s, uint(w))
182182
choices = append(choices, c)
183183
}
184184
return choices
185185
}
186-
187-
// This following is a historic artifact from comparative benchmarking with
188-
// randutil, however it is not critical to ongoing development.
189-
190-
// func BenchmarkRandutil(b *testing.B) {
191-
// if testing.Short() {
192-
// b.Skip()
193-
// }
194-
// for n := BMminChoices; n <= BMmaxChoices; n *= 10 {
195-
// b.Run(strconv.Itoa(n), func(b *testing.B) {
196-
// b.StopTimer()
197-
// choices := mockChoices(n)
198-
// choicesR := make([]randutil.Choice, len(choices), len(choices))
199-
// for i, c := range choices {
200-
// choicesR[i] = randutil.Choice{Weight: c.Weight, Item: c.Item}
201-
// }
202-
// b.StartTimer()
203-
204-
// for i := 0; i < b.N; i++ {
205-
// randutil.WeightedChoice(choicesR)
206-
// }
207-
// })
208-
// }
209-
// }

0 commit comments

Comments
 (0)