go性能分析工具

参考解析

答案:

pprof(performance profiles) - 性能选项)是Go的性能分析工具,在程序运行过程中,可以记录程序的运行信息,可以是CPU使用情况、内存使用情况、goroutine运行情况等,当需要性能调优或者定位Bug时候,这些记录的信息是相当重要。
使用pprof有多种方式,Go已经现成封装好了1个:net/http/pprof,使用简单的几行命令,就可以开启pprof,记录运行信息。
代码参考:

  1. package main
  2. import (
  3. "net/http"
  4. _ "net/http/pprof"
  5. "time"
  6. )
  7. func main() {
  8. go func() {
  9. http.ListenAndServe(":6060", nil)
  10. }()
  11. time.Sleep(100 * time.Second)
  12. }
  13. CPU profilego tool pprof http://127.0.0.1:6060/debug/pprof/profile
  14. Heap profile:go tool pprof http://127.0.0.1:6060/debug/pprof/heap
  15. Goroutine profile:go tool pprof http://127.0.0.1:6060/debug/pprof/goroutine