单元测试: func Division(a,b float64) (float64 ,error){ ?? ?if ?b==0 { ?? ??? ?return 0, errors.New("除数不能为0") ?? ?} ?? ?return ?a/b ?, nil } *_test.go文件,执行go test -v ?*_test.go: func TestDivision(t *testing.T) { ?? ?t.Parallel() ?? ?if res,err := Division(0,6);res != 0 || err != nil { ?? ??? ?t.Error("失败") ?? ?} else { ?? ??? ?t.Log("成功") ?? ?} } *_b_test.go文件,执行go test -test.bench=".*": func BenchmarkDivision(b *testing.B) { ?? ?for i := 0;i<b.N;i++ { ?? ??? ?Division(4,5) ?? ?} } pprof : 1.生成性能分析文件go test -v -bench . -cpuprofile=输出名.out; 2.go tool pprof 程序名.test 输出名.out; 3.<pprof>top 10 4.安装graphviz 5.go tool pprof --web 程序名.test 输出名.out(.svg输出在/tmp文件夹下)
traces: func main() { ?trace.Start(os.Stderr) ?defer trace.Stop()} 1.go run main.go 2> trace.out 2.go tool trace trace.out curl http://127.0.0.1:6060/debug/pprof/trace\?seconds\=20 > trace.out ?
|