我尝试分析我的go库,找出比c ++中的同样东西慢得多的原因.
我有简单的基准
func BenchmarkFile(t *testing.B) {
tmpFile, err := ioutil.TempFile("", TMP_FILE_PREFIX)
fw, err := NewFile(tmpFile.Name())
text := []byte("testing")
for i := 0; i < b.N; i++ {
_, err = fw.Write(text)
}
fw.Close()
}
Run Code Online (Sandbox Code Playgroud)
NewFile返回我的自定义Writer,它将数据编码为我们的二进制表示,甚至压缩它们,并写入文件系统.
go test -bench . -memprofile mem.out -cpuprofile cpu.out我跑了
PASS
BenchmarkFile-16 2000000000 0.20 ns/op
ok .../writer/iowriter 9.074s
Run Code Online (Sandbox Code Playgroud)
而不是分析它
# go tool pprof cpu.out
Entering interactive mode (type "help" for commands)
(pprof) top10
930ms of 930ms total ( 100%)
flat flat% sum% cum cum%
930ms 100% 100% 930ms 100%
(pprof)
Run Code Online (Sandbox Code Playgroud)
我甚至尝试编写使用我的编写器的example.go app,并添加pprof.StartCPUProfile(f)如http://blog.golang.org/profiling-go-programs中所示,但结果相同.
我做错了什么,如何确定我的lib的瓶颈是什么?先感谢您
好吧,这很容易,我想念添加二进制文件去工具pprof,它必须是
# go tool pprof write cpu.out
Entering interactive mode (type "help" for commands)
(pprof) top10
7.02s of 7.38s total (95.12%)
Dropped 14 nodes (cum <= 0.04s)
Showing top 10 nodes out of 32 (cum >= 0.19s)
flat flat% sum% cum cum%
6.55s 88.75% 88.75% 6.76s 91.60% syscall.Syscall
...
Run Code Online (Sandbox Code Playgroud)
当使用基准测试时,在那里创建二进制并使用它给出相同的结果.