]> Cypherpunks repositories - gostls13.git/commitdiff
strings,bytes: make benchmark work deterministic
authorKeith Randall <khr@golang.org>
Wed, 14 May 2025 19:46:28 +0000 (12:46 -0700)
committerKeith Randall <khr@golang.org>
Thu, 15 May 2025 21:24:16 +0000 (14:24 -0700)
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>

src/bytes/bytes_test.go
src/strings/strings_test.go

index 14b52a80358bb7efa51402860dd85b18511c3460..0f6cf4993af642d6c6ca24981f9f39decac44f56 100644 (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
                }
index d058ba7358cd918132de7640b43a2f142c71ee82..b10b5f05ccae53a65f62cfeb53985576d8f5dc9c 100644 (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'