Commit 23cb8864 authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

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 <mpvl@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent d2aa5f95
...@@ -3,7 +3,10 @@ ...@@ -3,7 +3,10 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package runtime_test package runtime_test
import "testing" import (
"fmt"
"testing"
)
const N = 20 const N = 20
...@@ -84,73 +87,35 @@ func BenchmarkAppendGrowString(b *testing.B) { ...@@ -84,73 +87,35 @@ func BenchmarkAppendGrowString(b *testing.B) {
} }
} }
func benchmarkAppendBytes(b *testing.B, length int) { func BenchmarkAppendSlice(b *testing.B) {
b.StopTimer() for _, length := range []int{1, 4, 7, 8, 15, 16, 32} {
b.Run(fmt.Sprint(length, "Bytes"), func(b *testing.B) {
x := make([]byte, 0, N) x := make([]byte, 0, N)
y := make([]byte, length) y := make([]byte, length)
b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
x = x[0:0] x = x[0:0]
x = append(x, y...) x = append(x, y...)
} }
})
}
} }
func BenchmarkAppend1Byte(b *testing.B) { func BenchmarkAppendStr(b *testing.B) {
benchmarkAppendBytes(b, 1) for _, str := range []string{
} "1",
"1234",
func BenchmarkAppend4Bytes(b *testing.B) { "12345678",
benchmarkAppendBytes(b, 4) "1234567890123456",
} "12345678901234567890123456789012",
} {
func BenchmarkAppend7Bytes(b *testing.B) { b.Run(fmt.Sprint(len(str), "Bytes"), func(b *testing.B) {
benchmarkAppendBytes(b, 7)
}
func BenchmarkAppend8Bytes(b *testing.B) {
benchmarkAppendBytes(b, 8)
}
func BenchmarkAppend15Bytes(b *testing.B) {
benchmarkAppendBytes(b, 15)
}
func BenchmarkAppend16Bytes(b *testing.B) {
benchmarkAppendBytes(b, 16)
}
func BenchmarkAppend32Bytes(b *testing.B) {
benchmarkAppendBytes(b, 32)
}
func benchmarkAppendStr(b *testing.B, str string) {
b.StopTimer()
x := make([]byte, 0, N) x := make([]byte, 0, N)
b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
x = x[0:0] x = x[0:0]
x = append(x, str...) x = append(x, str...)
} }
} })
}
func BenchmarkAppendStr1Byte(b *testing.B) {
benchmarkAppendStr(b, "1")
}
func BenchmarkAppendStr4Bytes(b *testing.B) {
benchmarkAppendStr(b, "1234")
}
func BenchmarkAppendStr8Bytes(b *testing.B) {
benchmarkAppendStr(b, "12345678")
}
func BenchmarkAppendStr16Bytes(b *testing.B) {
benchmarkAppendStr(b, "1234567890123456")
}
func BenchmarkAppendStr32Bytes(b *testing.B) {
benchmarkAppendStr(b, "12345678901234567890123456789012")
} }
func BenchmarkAppendSpecialCase(b *testing.B) { func BenchmarkAppendSpecialCase(b *testing.B) {
...@@ -195,46 +160,28 @@ func TestAppendOverlap(t *testing.T) { ...@@ -195,46 +160,28 @@ func TestAppendOverlap(t *testing.T) {
} }
} }
func benchmarkCopySlice(b *testing.B, l int) { func BenchmarkCopy(b *testing.B) {
s := make([]byte, l) for _, l := range []int{1, 2, 4, 8, 12, 16, 32, 128, 1024} {
buf := make([]byte, 4096) buf := make([]byte, 4096)
b.Run(fmt.Sprint(l, "Byte"), func(b *testing.B) {
s := make([]byte, l)
var n int var n int
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n = copy(buf, s) n = copy(buf, s)
} }
b.SetBytes(int64(n)) b.SetBytes(int64(n))
} })
b.Run(fmt.Sprint(l, "String"), func(b *testing.B) {
func benchmarkCopyStr(b *testing.B, l int) {
s := string(make([]byte, l)) s := string(make([]byte, l))
buf := make([]byte, 4096)
var n int var n int
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
n = copy(buf, s) n = copy(buf, s)
} }
b.SetBytes(int64(n)) b.SetBytes(int64(n))
})
}
} }
func BenchmarkCopy1Byte(b *testing.B) { benchmarkCopySlice(b, 1) }
func BenchmarkCopy2Byte(b *testing.B) { benchmarkCopySlice(b, 2) }
func BenchmarkCopy4Byte(b *testing.B) { benchmarkCopySlice(b, 4) }
func BenchmarkCopy8Byte(b *testing.B) { benchmarkCopySlice(b, 8) }
func BenchmarkCopy12Byte(b *testing.B) { benchmarkCopySlice(b, 12) }
func BenchmarkCopy16Byte(b *testing.B) { benchmarkCopySlice(b, 16) }
func BenchmarkCopy32Byte(b *testing.B) { benchmarkCopySlice(b, 32) }
func BenchmarkCopy128Byte(b *testing.B) { benchmarkCopySlice(b, 128) }
func BenchmarkCopy1024Byte(b *testing.B) { benchmarkCopySlice(b, 1024) }
func BenchmarkCopy1String(b *testing.B) { benchmarkCopyStr(b, 1) }
func BenchmarkCopy2String(b *testing.B) { benchmarkCopyStr(b, 2) }
func BenchmarkCopy4String(b *testing.B) { benchmarkCopyStr(b, 4) }
func BenchmarkCopy8String(b *testing.B) { benchmarkCopyStr(b, 8) }
func BenchmarkCopy12String(b *testing.B) { benchmarkCopyStr(b, 12) }
func BenchmarkCopy16String(b *testing.B) { benchmarkCopyStr(b, 16) }
func BenchmarkCopy32String(b *testing.B) { benchmarkCopyStr(b, 32) }
func BenchmarkCopy128String(b *testing.B) { benchmarkCopyStr(b, 128) }
func BenchmarkCopy1024String(b *testing.B) { benchmarkCopyStr(b, 1024) }
var ( var (
sByte []byte sByte []byte
s1Ptr []uintptr s1Ptr []uintptr
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment