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>
This commit is contained in:
Keith Randall 2025-05-14 12:46:28 -07:00
parent 19f05770b0
commit a88f093aaa
2 changed files with 12 additions and 6 deletions

View File

@ -2128,8 +2128,9 @@ func TestContainsFunc(t *testing.T) {
var makeFieldsInput = func() []byte {
x := make([]byte, 1<<20)
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
r := rand.New(rand.NewSource(99))
for i := range x {
switch rand.Intn(10) {
switch r.Intn(10) {
case 0:
x[i] = ' '
case 1:
@ -2148,8 +2149,9 @@ var makeFieldsInput = func() []byte {
var makeFieldsInputASCII = func() []byte {
x := make([]byte, 1<<20)
// Input is ~10% space, rest ASCII non-space.
r := rand.New(rand.NewSource(99))
for i := range x {
if rand.Intn(10) == 0 {
if r.Intn(10) == 0 {
x[i] = ' '
} else {
x[i] = 'x'
@ -2246,8 +2248,9 @@ func makeBenchInputHard() []byte {
"hello", "world",
}
x := make([]byte, 0, 1<<20)
r := rand.New(rand.NewSource(99))
for {
i := rand.Intn(len(tokens))
i := r.Intn(len(tokens))
if len(x)+len(tokens[i]) >= 1<<20 {
break
}

View File

@ -1875,8 +1875,9 @@ func makeBenchInputHard() string {
"hello", "world",
}
x := make([]byte, 0, 1<<20)
r := rand.New(rand.NewSource(99))
for {
i := rand.Intn(len(tokens))
i := r.Intn(len(tokens))
if len(x)+len(tokens[i]) >= 1<<20 {
break
}
@ -1964,8 +1965,9 @@ func BenchmarkCountByte(b *testing.B) {
var makeFieldsInput = func() string {
x := make([]byte, 1<<20)
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
r := rand.New(rand.NewSource(99))
for i := range x {
switch rand.Intn(10) {
switch r.Intn(10) {
case 0:
x[i] = ' '
case 1:
@ -1984,8 +1986,9 @@ var makeFieldsInput = func() string {
var makeFieldsInputASCII = func() string {
x := make([]byte, 1<<20)
// Input is ~10% space, rest ASCII non-space.
r := rand.New(rand.NewSource(99))
for i := range x {
if rand.Intn(10) == 0 {
if r.Intn(10) == 0 {
x[i] = ' '
} else {
x[i] = 'x'