cap*_*aig 6 testing benchmarking go
我有一些不同的接口实现,以及我想要测试它们的各种因素.最终目标是在不同情况下为不同的实现制作结果网格.
我可以为每种可能的组合编写测试,但这会让人筋疲力尽:
func Benchmark_ImplA_N100_X300(b *testing.B){
impl := newImplA(100,300)
runBenchmark(b,impl)
}
Run Code Online (Sandbox Code Playgroud)
我添加的组合越多,我就越需要复制/粘贴.这很快就会变得很麻烦.
我很乐意做以下事情:
tests := []testing.InternalBenchmark{}
for _, n := range []int{50,100,500,10000}{
for _,x := range []int{300,200}{
for name, gen := range implementations{
impl = gen(n,x)
bm := testing.InternalBenchmark{Name: fmt.Sprint(name,x,n)}
bm.F = func(b *testing.B){
runBench(b,impl)
}
tests = append(tests,bm)
}
}
}
testing.RunBenchmarks(anything, tests)
Run Code Online (Sandbox Code Playgroud)
这样我就可以更新组合列表,并且基准测试将在所有组合中神奇地运行.我在main和TestMain中尝试了类似的东西,没有任何输出.我不确定我是否使用它错误,或者测试包是否正在做一些有趣的事情.
我真的不在乎go test工具是否可以处理它或是否有其他方式.
是的,这是可能的.在你的测试文件中(xxx_test.go)创建你自己的TestMain()函数,在组装动态基准测试用例(struct的值testing.InternalBenchmark)之后,调用testing.Main()哪个正确解析命令行标志,创建和设置testing.M,准备和调用testing.RunBenchmarks().这样,您的动态基准测试仍然可以运行go test.
注意:testing.Main()永远不会回复os.Exit().如果你想在基准测试结果进行进一步的记录和计算,您也可以拨打(这是做),你可以通过它返回的退出代码来.testing.MainStart().Run()testing.Main()M.Run()os.Exit()
下面是一个完整的测试文件,您只需运行它即可go test -bench ..
输出为:动态生成测试的基准测试结果(具有不同参数的不同实现):
testing: warning: no tests to run
PASS
main.EngineA[impl=0, n=50, x=300]-4 100000 16716 ns/op
main.EngineB[impl=1, n=50, x=300]-4 100000 24788 ns/op
main.EngineA[impl=0, n=50, x=200]-4 100000 10764 ns/op
main.EngineB[impl=1, n=50, x=200]-4 100000 16415 ns/op
main.EngineA[impl=0, n=100, x=300]-4 50000 33426 ns/op
main.EngineB[impl=1, n=100, x=300]-4 30000 48466 ns/op
main.EngineA[impl=0, n=100, x=200]-4 50000 20452 ns/op
main.EngineB[impl=1, n=100, x=200]-4 50000 33134 ns/op
main.EngineA[impl=0, n=500, x=300]-4 10000 163087 ns/op
main.EngineB[impl=1, n=500, x=300]-4 5000 238043 ns/op
main.EngineA[impl=0, n=500, x=200]-4 10000 102662 ns/op
main.EngineB[impl=1, n=500, x=200]-4 10000 163113 ns/op
main.EngineA[impl=0, n=1000, x=300]-4 5000 319744 ns/op
main.EngineB[impl=1, n=1000, x=300]-4 3000 512077 ns/op
main.EngineA[impl=0, n=1000, x=200]-4 10000 201036 ns/op
main.EngineB[impl=1, n=1000, x=200]-4 5000 325714 ns/op
ok _/xxx/src/play 27.307s
Run Code Online (Sandbox Code Playgroud)
和源(测试文件,例如dynbench_test.go):
package main
import (
"fmt"
"testing"
)
type Engine interface {
Calc()
}
type EngineA struct{ n, x int }
func (e EngineA) Calc() {
for i := 0; i < e.n; i++ {
a, b := make([]byte, e.x), make([]byte, e.x)
copy(b, a)
}
}
type EngineB struct{ n, x int }
func (e EngineB) Calc() {
for i := 0; i < e.n*2; i++ {
a, b := make([]byte, e.x/2), make([]byte, e.x/2)
copy(b, a)
}
}
func TestMain(m *testing.M) {
implementations := [](func(n, x int) Engine){
func(n, x int) Engine { return EngineA{n, x} },
func(n, x int) Engine { return EngineB{n, x} },
}
benchmarks := []testing.InternalBenchmark{}
for _, n := range []int{50, 100, 500, 1000} {
for _, x := range []int{300, 200} {
for name, gen := range implementations {
impl := gen(n, x)
bm := testing.InternalBenchmark{
Name: fmt.Sprintf("%T[impl=%d, n=%d, x=%d]", impl, name, n, x)}
bm.F = func(b *testing.B) {
for i := 0; i < b.N; i++ {
impl.Calc()
}
}
benchmarks = append(benchmarks, bm)
}
}
}
anything := func(pat, str string) (bool, error) { return true, nil }
testing.Main(anything, nil, benchmarks, nil)
}
Run Code Online (Sandbox Code Playgroud)
注释#2:
testing.Main(),testing.MainStart()并且testing.InternalBenchmark可能会在Go的未来版本中更改(或删除):
内部函数/内部类型但由于它是交叉包装而导出; 执行"go test"命令的一部分或调用.