深入理解Golang的单元测试和性能测试

2020-01-28 12:19:23王旭

go tool pprof popcnt.test cpu.profile 进入交互模式

➜ testBenchMark go tool pprof popcnt.test cpu.profile
Entering interactive mode (type "help" for commands)
(pprof) top
1880ms of 1880ms total ( 100%)
  flat flat% sum%  cum cum%
 1790ms 95.21% 95.21%  1790ms 95.21% app/testBenchMark/popcnt.BenchmarkPopcnt
  90ms 4.79% 100%  90ms 4.79% runtime.usleep
   0  0% 100%  1790ms 95.21% runtime.goexit
   0  0% 100%  90ms 4.79% runtime.mstart
   0  0% 100%  90ms 4.79% runtime.mstart1
   0  0% 100%  90ms 4.79% runtime.sysmon
   0  0% 100%  1790ms 95.21% testing.(*B).launch
   0  0% 100%  1790ms 95.21% testing.(*B).runN
(pprof)

go tool pprof --web popcnt.test cpu.profile 进入web模式


$ go tool pprof --text mybin http://myserver:6060:/debug/pprof/profile

这有几个可用的输出类型,最有用的几个为: --text,--web 和 --list 。运行 go tool pprof 来得到最完整的列表。

下面分享一点go test的参数解读。来源

格式形如:

go test [-c] [-i] [build flags] [packages] [flags for test binary]

参数解读:

-c : 编译go test成为可执行的二进制文件,但是不运行测试。

-i : 安装测试包依赖的package,但是不运行测试。

关于build flags,调用go help build,这些是编译运行过程中需要使用到的参数,一般设置为空

关于packages,调用go help packages,这些是关于包的管理,一般设置为空

关于flags for test binary,调用go help testflag,这些是go test过程中经常使用到的参数

-test.v : 是否输出全部的单元测试用例(不管成功或者失败),默认没有加上,所以只输出失败的单元测试用例。

-test.run pattern: 只跑哪些单元测试用例

-test.bench patten: 只跑那些性能测试用例

-test.benchmem : 是否在性能测试的时候输出内存情况

-test.benchtime t : 性能测试运行的时间,默认是1s

-test.cpuprofile cpu.out : 是否输出cpu性能分析文件

-test.memprofile mem.out : 是否输出内存性能分析文件

-test.blockprofile block.out : 是否输出内部goroutine阻塞的性能分析文件

-test.memprofilerate n : 内存性能分析的时候有一个分配了多少的时候才打点记录的问题。这个参数就是设置打点的内存分配间隔,也就是profile中一个sample代表的内存大小。默认是设置为512 * 1024的。如果你将它设置为1,则每分配一个内存块就会在profile中有个打点,那么生成的profile的sample就会非常多。如果你设置为0,那就是不做打点了。

你可以通过设置memprofilerate=1和GOGC=off来关闭内存回收,并且对每个内存块的分配进行观察。

-test.blockprofilerate n: 基本同上,控制的是goroutine阻塞时候打点的纳秒数。默认不设置就相当于-test.blockprofilerate=1,每一纳秒都打点记录一下