|
| 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 | +} |
0 commit comments