gpt4 book ai didi

Golang 分析 - top10 仅显示一行 100%

转载 作者:IT王子 更新时间:2023-10-29 01:25:56 24 4
gpt4 key购买 nike

我尝试分析我的 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()
}

NewFile 返回我的自定义 Writer,它将数据编码为我们的二进制表示,甚至压缩它们,然后写入文件系统。

运行 go test -bench 。 -memprofile mem.out -cpuprofile cpu.out 我明白了

PASS
BenchmarkFile-16 2000000000 0.20 ns/op
ok .../writer/iowriter 9.074s

比分析它

# 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)

我什至尝试编写使用我的编写器的 example.go 应用程序,并添加 pprof.StartCPUProfile(f),如 http://blog.golang.org/profiling-go-programs 所示。但结果相同。

我做错了什么,我如何确定我的库的瓶颈是什么?提前谢谢你

最佳答案

好吧,这很简单,我想念将二进制文件添加到 go 工具 pprof,si 它必须是

# 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
...

并且在使用基准测试时,会在那里创建二进制文件并使用它给出相同的结果。

关于Golang 分析 - top10 仅显示一行 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35872019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com