Skip to content

Commit a88f093

Browse files
committed
strings,bytes: make benchmark work deterministic
It's hard to compare two different runs of a benchmark if they are doing different amounts of work. Change-Id: I5d6845f3d11bb10136f745e6207d5f683612276d Reviewed-on: https://go-review.googlesource.com/c/go/+/672895 Reviewed-by: Junyang Shao <shaojunyang@google.com> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 19f0577 commit a88f093

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/bytes/bytes_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,8 +2128,9 @@ func TestContainsFunc(t *testing.T) {
21282128
var makeFieldsInput = func() []byte {
21292129
x := make([]byte, 1<<20)
21302130
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
2131+
r := rand.New(rand.NewSource(99))
21312132
for i := range x {
2132-
switch rand.Intn(10) {
2133+
switch r.Intn(10) {
21332134
case 0:
21342135
x[i] = ' '
21352136
case 1:
@@ -2148,8 +2149,9 @@ var makeFieldsInput = func() []byte {
21482149
var makeFieldsInputASCII = func() []byte {
21492150
x := make([]byte, 1<<20)
21502151
// Input is ~10% space, rest ASCII non-space.
2152+
r := rand.New(rand.NewSource(99))
21512153
for i := range x {
2152-
if rand.Intn(10) == 0 {
2154+
if r.Intn(10) == 0 {
21532155
x[i] = ' '
21542156
} else {
21552157
x[i] = 'x'
@@ -2246,8 +2248,9 @@ func makeBenchInputHard() []byte {
22462248
"hello", "world",
22472249
}
22482250
x := make([]byte, 0, 1<<20)
2251+
r := rand.New(rand.NewSource(99))
22492252
for {
2250-
i := rand.Intn(len(tokens))
2253+
i := r.Intn(len(tokens))
22512254
if len(x)+len(tokens[i]) >= 1<<20 {
22522255
break
22532256
}

src/strings/strings_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,8 +1875,9 @@ func makeBenchInputHard() string {
18751875
"hello", "world",
18761876
}
18771877
x := make([]byte, 0, 1<<20)
1878+
r := rand.New(rand.NewSource(99))
18781879
for {
1879-
i := rand.Intn(len(tokens))
1880+
i := r.Intn(len(tokens))
18801881
if len(x)+len(tokens[i]) >= 1<<20 {
18811882
break
18821883
}
@@ -1964,8 +1965,9 @@ func BenchmarkCountByte(b *testing.B) {
19641965
var makeFieldsInput = func() string {
19651966
x := make([]byte, 1<<20)
19661967
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1968+
r := rand.New(rand.NewSource(99))
19671969
for i := range x {
1968-
switch rand.Intn(10) {
1970+
switch r.Intn(10) {
19691971
case 0:
19701972
x[i] = ' '
19711973
case 1:
@@ -1984,8 +1986,9 @@ var makeFieldsInput = func() string {
19841986
var makeFieldsInputASCII = func() string {
19851987
x := make([]byte, 1<<20)
19861988
// Input is ~10% space, rest ASCII non-space.
1989+
r := rand.New(rand.NewSource(99))
19871990
for i := range x {
1988-
if rand.Intn(10) == 0 {
1991+
if r.Intn(10) == 0 {
19891992
x[i] = ' '
19901993
} else {
19911994
x[i] = 'x'

0 commit comments

Comments
 (0)