aboutsummaryrefslogtreecommitdiff
path: root/src/strings/strings_test.go
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2025-05-14 12:46:28 -0700
committerKeith Randall <khr@golang.org>2025-05-15 14:24:16 -0700
commita88f093aaa35ae18aa02389624822101cbf231c0 (patch)
treed0fb43316339ff49e5d43eee7b92d0f54ce35933 /src/strings/strings_test.go
parent19f05770b05ef2a12692f522056ffb3bc23583ea (diff)
downloadgo-a88f093aaa35ae18aa02389624822101cbf231c0.tar.xz
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>
Diffstat (limited to 'src/strings/strings_test.go')
-rw-r--r--src/strings/strings_test.go9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go
index d058ba7358..b10b5f05cc 100644
--- a/src/strings/strings_test.go
+++ b/src/strings/strings_test.go
@@ -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'