Skip to content

Commit 23cb886

Browse files
committed
runtime: use Run for more benchmarks
Names for Append?Bytes are slightly changed in addition to adding a slash. Change-Id: I0291aa29c693f9040fd01368eaad9766259677df Reviewed-on: https://go-review.googlesource.com/23426 Run-TryBot: Marcel van Lohuizen <[email protected]> Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent d2aa5f9 commit 23cb886

File tree

1 file changed

+48
-101
lines changed

1 file changed

+48
-101
lines changed

src/runtime/append_test.go

Lines changed: 48 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// license that can be found in the LICENSE file.
44
package runtime_test
55

6-
import "testing"
6+
import (
7+
"fmt"
8+
"testing"
9+
)
710

811
const N = 20
912

@@ -84,75 +87,37 @@ func BenchmarkAppendGrowString(b *testing.B) {
8487
}
8588
}
8689

87-
func benchmarkAppendBytes(b *testing.B, length int) {
88-
b.StopTimer()
89-
x := make([]byte, 0, N)
90-
y := make([]byte, length)
91-
b.StartTimer()
92-
for i := 0; i < b.N; i++ {
93-
x = x[0:0]
94-
x = append(x, y...)
90+
func BenchmarkAppendSlice(b *testing.B) {
91+
for _, length := range []int{1, 4, 7, 8, 15, 16, 32} {
92+
b.Run(fmt.Sprint(length, "Bytes"), func(b *testing.B) {
93+
x := make([]byte, 0, N)
94+
y := make([]byte, length)
95+
for i := 0; i < b.N; i++ {
96+
x = x[0:0]
97+
x = append(x, y...)
98+
}
99+
})
95100
}
96101
}
97102

98-
func BenchmarkAppend1Byte(b *testing.B) {
99-
benchmarkAppendBytes(b, 1)
100-
}
101-
102-
func BenchmarkAppend4Bytes(b *testing.B) {
103-
benchmarkAppendBytes(b, 4)
104-
}
105-
106-
func BenchmarkAppend7Bytes(b *testing.B) {
107-
benchmarkAppendBytes(b, 7)
108-
}
109-
110-
func BenchmarkAppend8Bytes(b *testing.B) {
111-
benchmarkAppendBytes(b, 8)
112-
}
113-
114-
func BenchmarkAppend15Bytes(b *testing.B) {
115-
benchmarkAppendBytes(b, 15)
116-
}
117-
118-
func BenchmarkAppend16Bytes(b *testing.B) {
119-
benchmarkAppendBytes(b, 16)
120-
}
121-
122-
func BenchmarkAppend32Bytes(b *testing.B) {
123-
benchmarkAppendBytes(b, 32)
124-
}
125-
126-
func benchmarkAppendStr(b *testing.B, str string) {
127-
b.StopTimer()
128-
x := make([]byte, 0, N)
129-
b.StartTimer()
130-
for i := 0; i < b.N; i++ {
131-
x = x[0:0]
132-
x = append(x, str...)
103+
func BenchmarkAppendStr(b *testing.B) {
104+
for _, str := range []string{
105+
"1",
106+
"1234",
107+
"12345678",
108+
"1234567890123456",
109+
"12345678901234567890123456789012",
110+
} {
111+
b.Run(fmt.Sprint(len(str), "Bytes"), func(b *testing.B) {
112+
x := make([]byte, 0, N)
113+
for i := 0; i < b.N; i++ {
114+
x = x[0:0]
115+
x = append(x, str...)
116+
}
117+
})
133118
}
134119
}
135120

136-
func BenchmarkAppendStr1Byte(b *testing.B) {
137-
benchmarkAppendStr(b, "1")
138-
}
139-
140-
func BenchmarkAppendStr4Bytes(b *testing.B) {
141-
benchmarkAppendStr(b, "1234")
142-
}
143-
144-
func BenchmarkAppendStr8Bytes(b *testing.B) {
145-
benchmarkAppendStr(b, "12345678")
146-
}
147-
148-
func BenchmarkAppendStr16Bytes(b *testing.B) {
149-
benchmarkAppendStr(b, "1234567890123456")
150-
}
151-
152-
func BenchmarkAppendStr32Bytes(b *testing.B) {
153-
benchmarkAppendStr(b, "12345678901234567890123456789012")
154-
}
155-
156121
func BenchmarkAppendSpecialCase(b *testing.B) {
157122
b.StopTimer()
158123
x := make([]int, 0, N)
@@ -195,46 +160,28 @@ func TestAppendOverlap(t *testing.T) {
195160
}
196161
}
197162

198-
func benchmarkCopySlice(b *testing.B, l int) {
199-
s := make([]byte, l)
200-
buf := make([]byte, 4096)
201-
var n int
202-
for i := 0; i < b.N; i++ {
203-
n = copy(buf, s)
204-
}
205-
b.SetBytes(int64(n))
206-
}
207-
208-
func benchmarkCopyStr(b *testing.B, l int) {
209-
s := string(make([]byte, l))
210-
buf := make([]byte, 4096)
211-
var n int
212-
for i := 0; i < b.N; i++ {
213-
n = copy(buf, s)
163+
func BenchmarkCopy(b *testing.B) {
164+
for _, l := range []int{1, 2, 4, 8, 12, 16, 32, 128, 1024} {
165+
buf := make([]byte, 4096)
166+
b.Run(fmt.Sprint(l, "Byte"), func(b *testing.B) {
167+
s := make([]byte, l)
168+
var n int
169+
for i := 0; i < b.N; i++ {
170+
n = copy(buf, s)
171+
}
172+
b.SetBytes(int64(n))
173+
})
174+
b.Run(fmt.Sprint(l, "String"), func(b *testing.B) {
175+
s := string(make([]byte, l))
176+
var n int
177+
for i := 0; i < b.N; i++ {
178+
n = copy(buf, s)
179+
}
180+
b.SetBytes(int64(n))
181+
})
214182
}
215-
b.SetBytes(int64(n))
216183
}
217184

218-
func BenchmarkCopy1Byte(b *testing.B) { benchmarkCopySlice(b, 1) }
219-
func BenchmarkCopy2Byte(b *testing.B) { benchmarkCopySlice(b, 2) }
220-
func BenchmarkCopy4Byte(b *testing.B) { benchmarkCopySlice(b, 4) }
221-
func BenchmarkCopy8Byte(b *testing.B) { benchmarkCopySlice(b, 8) }
222-
func BenchmarkCopy12Byte(b *testing.B) { benchmarkCopySlice(b, 12) }
223-
func BenchmarkCopy16Byte(b *testing.B) { benchmarkCopySlice(b, 16) }
224-
func BenchmarkCopy32Byte(b *testing.B) { benchmarkCopySlice(b, 32) }
225-
func BenchmarkCopy128Byte(b *testing.B) { benchmarkCopySlice(b, 128) }
226-
func BenchmarkCopy1024Byte(b *testing.B) { benchmarkCopySlice(b, 1024) }
227-
228-
func BenchmarkCopy1String(b *testing.B) { benchmarkCopyStr(b, 1) }
229-
func BenchmarkCopy2String(b *testing.B) { benchmarkCopyStr(b, 2) }
230-
func BenchmarkCopy4String(b *testing.B) { benchmarkCopyStr(b, 4) }
231-
func BenchmarkCopy8String(b *testing.B) { benchmarkCopyStr(b, 8) }
232-
func BenchmarkCopy12String(b *testing.B) { benchmarkCopyStr(b, 12) }
233-
func BenchmarkCopy16String(b *testing.B) { benchmarkCopyStr(b, 16) }
234-
func BenchmarkCopy32String(b *testing.B) { benchmarkCopyStr(b, 32) }
235-
func BenchmarkCopy128String(b *testing.B) { benchmarkCopyStr(b, 128) }
236-
func BenchmarkCopy1024String(b *testing.B) { benchmarkCopyStr(b, 1024) }
237-
238185
var (
239186
sByte []byte
240187
s1Ptr []uintptr

0 commit comments

Comments
 (0)