1

我看过关于profiling go programs的文章,我只是不明白。有人有一个简单的代码示例,代码片段的性能由配置文件“对象”记录在文本文件中吗?

4

1 回答 1

2

以下是我用于简单 CPU 和内存分析的命令,以帮助您入门。

假设你做了一个这样的基准函数:

文件 something_test.go :

func BenchmarkProfileMe(b *testing.B) {
 // execute the significant portion of the code you want to profile b.N times
}

在 shell 脚本中:

# -test XXX is a trick so you don't trigger other tests by asking a non existent specific test called literally XXX
# you can adapt the benchtime depending on the type of code you want to profile.

go test -v -bench ProfileMe -test.run XXX -cpuprofile cpu.pprof -memprofile mem.pprof -benchtime 10s

go tool pprof --text ./something.test cpu.pprof           ## To get a CPU profile per function

go tool pprof --text ./something.test cpu.pprof --lines   ## To get a CPU profile per line

go tool pprof --text ./something.test mem.pprof           ## To get the memory profile

它将在控制台上为您呈现每种情况下的最热点。

于 2014-05-02T00:11:08.230 回答