gogotesting.B
需要测试的代码
我们依然用上一篇的代码结构
│ main.go
│ main_test.go
main.go
func JoinStrUseSprint(a,b string) string {
return fmt.Sprintf("%s%s",a,b)
}
func JoinStrUseNor(a,b string) string {
return a+b
}
创建一个基准测试
TestFuncBenchmarkFunc
func TestJoinStrUseNor(t *testing.T) {
s := JoinStrUseNor("aaa","bbb")
t.Log(s)
}
func TestJoinStrUseSprint(t *testing.T) {
s := JoinStrUseSprint("aaa","bbb")
t.Log(s)
}
func BenchmarkJoinStrUseNor(b *testing.B) {
b.ResetTimer()
for i:=0; i<b.N; i++{
JoinStrUseNor("aaa","bbb")
}
}
func BenchmarkJoinStrUseSprint(b *testing.B) {
b.ResetTimer()
for i:=0; i<b.N; i++{
JoinStrUseSprint("aaa","bbb")
}
}
执行基础测试
go test -bench=. -benchtime=1s -benchmem -count=1
goos: linux
goarch: amd64
pkg: test
BenchmarkJoinStrUseNor-8 79888155 15.5 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrUseSprint-8 8956500 135 ns/op 40 B/op 3 allocs/op
PASS
ok test 2.930s
命令参数
-bench=..go test -bench=BenchmarkJoinStrUseSprintBenchmarkJoinStrUseSprint-benchtime=1s1s-benchmem-count=1
响应参数
goos: linuxgoarch: amd64BenchmarkJoinStrUseNor-8GOMAXPROCS79888155b.N15.5 ns/op0 B/op0 allocs/op
结果分析
+fmt.Sprintf()
样本分析
go test -bench=BenchmarkJoinStrUseSprint -benchtime=1s -benchmem -count=10benchstat
benchstatGolang
安装
go get golang.org/x/perf/cmd/benchstat
分析
- 为了对比我们增加一个函数
func JoinStrOpt(a,b string) string {
return fmt.Sprintf("%s%s",a,b)
}
- 增加测试
func BenchmarkJoinStrOpt(b *testing.B) {
b.ResetTimer()
for i:=0; i<b.N; i++{
JoinStrOpt("aaa","bbb")
}
}
before.txt
go test -bench=BenchmarkJoinStrOpt -benchmem -count=10 | tee before.txt
goos: linux
goarch: amd64
pkg: test
BenchmarkJoinStrOpt-8 9143092 131 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9222475 131 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9344643 130 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9127231 131 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9223482 130 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9126334 131 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9364201 129 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9248034 130 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9034518 130 ns/op 40 B/op 3 allocs/op
BenchmarkJoinStrOpt-8 9102846 130 ns/op 40 B/op 3 allocs/op
PASS
ok test 13.323s
- 简单分析结果
benchstat before.txt
name time/op
JoinStrOpt-8 130ns ± 1%
name alloc/op
JoinStrOpt-8 40.0B ± 0%
name allocs/op
JoinStrOpt-8 3.00 ± 0%
benchstrat
对比分析
JoinStrUseNor
func JoinStrOpt(a,b string) string {
return a+b
}
after.txt
>go test -bench=BenchmarkJoinStrOpt -benchmem -count=10 | tee after.txt
goos: linux
goarch: amd64
pkg: test
BenchmarkJoinStrOpt-8 78033046 15.3 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 77211630 15.3 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 78088903 15.4 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 77907912 15.3 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 73805730 15.3 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 78508854 15.3 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 73493384 15.2 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 78618926 15.2 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 74973290 15.2 ns/op 0 B/op 0 allocs/op
BenchmarkJoinStrOpt-8 79287993 15.4 ns/op 0 B/op 0 allocs/op
PASS
ok test 11.959s
- 对比分析
>benchstat before.txt after.txt
name old time/op new time/op delta
JoinStrOpt-8 130ns ± 1% 15ns ± 1% -88.27% (p=0.000 n=10+10)
name old alloc/op new alloc/op delta
JoinStrOpt-8 40.0B ± 0% 0.0B -100.00% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
JoinStrOpt-8 3.00 ± 0% 0.00 -100.00% (p=0.000 n=10+10)
benchstat
对比值括号中的数据含义
p
n=10+10benchstat10+10
参考资料
期待与您一起交流