1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func BenchmarkSwitch8Predictable(b *testing.B) {
13 benchmarkSwitch8(b, true)
15 func BenchmarkSwitch8Unpredictable(b *testing.B) {
16 benchmarkSwitch8(b, false)
18 func benchmarkSwitch8(b *testing.B, predictable bool) {
21 for i := 0; i < b.N; i++ {
22 rng = rng.next(predictable)
23 switch rng.value() & 7 {
45 func BenchmarkSwitch32Predictable(b *testing.B) {
46 benchmarkSwitch32(b, true)
48 func BenchmarkSwitch32Unpredictable(b *testing.B) {
49 benchmarkSwitch32(b, false)
51 func benchmarkSwitch32(b *testing.B, predictable bool) {
54 for i := 0; i < b.N; i++ {
55 rng = rng.next(predictable)
56 switch rng.value() & 31 {
80 func BenchmarkSwitchStringPredictable(b *testing.B) {
81 benchmarkSwitchString(b, true)
83 func BenchmarkSwitchStringUnpredictable(b *testing.B) {
84 benchmarkSwitchString(b, false)
86 func benchmarkSwitchString(b *testing.B, predictable bool) {
99 for i := 0; i < b.N; i++ {
100 rng = rng.next(predictable)
101 switch a[rng.value()&7] {
123 func BenchmarkSwitchTypePredictable(b *testing.B) {
124 benchmarkSwitchType(b, true)
126 func BenchmarkSwitchTypeUnpredictable(b *testing.B) {
127 benchmarkSwitchType(b, false)
129 func benchmarkSwitchType(b *testing.B, predictable bool) {
142 for i := 0; i < b.N; i++ {
143 rng = rng.next(predictable)
144 switch a[rng.value()&7].(type) {
166 func BenchmarkSwitchInterfaceTypePredictable(b *testing.B) {
167 benchmarkSwitchInterfaceType(b, true)
169 func BenchmarkSwitchInterfaceTypeUnpredictable(b *testing.B) {
170 benchmarkSwitchInterfaceType(b, false)
245 func benchmarkSwitchInterfaceType(b *testing.B, predictable bool) {
258 for i := 0; i < b.N; i++ {
259 rng = rng.next(predictable)
260 switch a[rng.value()&7].(type) {
282 // A simple random number generator used to make switches conditionally predictable.
288 func (r rng) next(predictable bool) rng {
292 return rng(bits.RotateLeft64(uint64(r), 13) * 0x3c374d)
294 func (r rng) value() uint64 {