mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
19f05770b0
commit
a88f093aaa
2 changed files with 12 additions and 6 deletions
|
|
@ -2128,8 +2128,9 @@ func TestContainsFunc(t *testing.T) {
|
||||||
var makeFieldsInput = func() []byte {
|
var makeFieldsInput = func() []byte {
|
||||||
x := make([]byte, 1<<20)
|
x := make([]byte, 1<<20)
|
||||||
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
|
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
for i := range x {
|
for i := range x {
|
||||||
switch rand.Intn(10) {
|
switch r.Intn(10) {
|
||||||
case 0:
|
case 0:
|
||||||
x[i] = ' '
|
x[i] = ' '
|
||||||
case 1:
|
case 1:
|
||||||
|
|
@ -2148,8 +2149,9 @@ var makeFieldsInput = func() []byte {
|
||||||
var makeFieldsInputASCII = func() []byte {
|
var makeFieldsInputASCII = func() []byte {
|
||||||
x := make([]byte, 1<<20)
|
x := make([]byte, 1<<20)
|
||||||
// Input is ~10% space, rest ASCII non-space.
|
// Input is ~10% space, rest ASCII non-space.
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
for i := range x {
|
for i := range x {
|
||||||
if rand.Intn(10) == 0 {
|
if r.Intn(10) == 0 {
|
||||||
x[i] = ' '
|
x[i] = ' '
|
||||||
} else {
|
} else {
|
||||||
x[i] = 'x'
|
x[i] = 'x'
|
||||||
|
|
@ -2246,8 +2248,9 @@ func makeBenchInputHard() []byte {
|
||||||
"hello", "world",
|
"hello", "world",
|
||||||
}
|
}
|
||||||
x := make([]byte, 0, 1<<20)
|
x := make([]byte, 0, 1<<20)
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
for {
|
for {
|
||||||
i := rand.Intn(len(tokens))
|
i := r.Intn(len(tokens))
|
||||||
if len(x)+len(tokens[i]) >= 1<<20 {
|
if len(x)+len(tokens[i]) >= 1<<20 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1875,8 +1875,9 @@ func makeBenchInputHard() string {
|
||||||
"hello", "world",
|
"hello", "world",
|
||||||
}
|
}
|
||||||
x := make([]byte, 0, 1<<20)
|
x := make([]byte, 0, 1<<20)
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
for {
|
for {
|
||||||
i := rand.Intn(len(tokens))
|
i := r.Intn(len(tokens))
|
||||||
if len(x)+len(tokens[i]) >= 1<<20 {
|
if len(x)+len(tokens[i]) >= 1<<20 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -1964,8 +1965,9 @@ func BenchmarkCountByte(b *testing.B) {
|
||||||
var makeFieldsInput = func() string {
|
var makeFieldsInput = func() string {
|
||||||
x := make([]byte, 1<<20)
|
x := make([]byte, 1<<20)
|
||||||
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
|
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
for i := range x {
|
for i := range x {
|
||||||
switch rand.Intn(10) {
|
switch r.Intn(10) {
|
||||||
case 0:
|
case 0:
|
||||||
x[i] = ' '
|
x[i] = ' '
|
||||||
case 1:
|
case 1:
|
||||||
|
|
@ -1984,8 +1986,9 @@ var makeFieldsInput = func() string {
|
||||||
var makeFieldsInputASCII = func() string {
|
var makeFieldsInputASCII = func() string {
|
||||||
x := make([]byte, 1<<20)
|
x := make([]byte, 1<<20)
|
||||||
// Input is ~10% space, rest ASCII non-space.
|
// Input is ~10% space, rest ASCII non-space.
|
||||||
|
r := rand.New(rand.NewSource(99))
|
||||||
for i := range x {
|
for i := range x {
|
||||||
if rand.Intn(10) == 0 {
|
if r.Intn(10) == 0 {
|
||||||
x[i] = ' '
|
x[i] = ' '
|
||||||
} else {
|
} else {
|
||||||
x[i] = 'x'
|
x[i] = 'x'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue