英文:
does anybody have a simple pprof use on a go-executable?
问题
我看了关于分析Go程序的文章,但我简单地无法理解它。有没有人有一个简单的代码示例,可以通过一个"profile-对象"将代码片段的性能记录在文本文件中?
英文:
I have looked at the article about profiling go programs, and I simple do not understand it. Do someone have a simple code example were the performance of code snippet is logged in text file by a profile-"object"?
答案1
得分: 2
以下是我为您翻译的内容:
这里是我用于简单的CPU和内存分析的命令,帮助您入门。
假设您创建了一个类似这样的基准函数:
文件 something_test.go:
func BenchmarkProfileMe(b *testing.B) {
// 执行您想要分析的代码的重要部分,重复执行 b.N 次
}
在一个 shell 脚本中:
# -test XXX 是一个技巧,这样您就不会通过请求一个不存在的特定测试(字面上称为 XXX)来触发其他测试
# 您可以根据要分析的代码类型调整 benchtime。
go test -v -bench ProfileMe -test.run XXX -cpuprofile cpu.pprof -memprofile mem.pprof -benchtime 10s
go tool pprof --text ./something.test cpu.pprof ## 获取每个函数的 CPU 分析结果
go tool pprof --text ./something.test cpu.pprof --lines ## 获取每行的 CPU 分析结果
go tool pprof --text ./something.test mem.pprof ## 获取内存分析结果
它会在控制台上呈现给您每种情况下的热点位置。
英文:
Here are the commands I use for a simple CPU and memory profiling to get you started.
Let's say you made a benchmark function like this :
File something_test.go :
func BenchmarkProfileMe(b *testing.B) {
// execute the significant portion of the code you want to profile b.N times
}
In a shell script:
# -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
It will present you the hottests spots in each cases on the console.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论