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条回答 默认 最新
- doucan9079 2014-05-02 00:11关注
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.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报