美文网首页
GO学习笔记(9)-性能监控 go-pprof

GO学习笔记(9)-性能监控 go-pprof

作者: 卡门001 | 来源:发表于2021-07-04 00:36 被阅读0次

介绍

代码的性能监控,包含: net/http/pprofruntime/pprof,其中net/http/pprof可用于 web服务器监控服务进程的监听。如果要监听应用程序,则需要用到runtime/pprof

服务监听

引入net/http/pprof,一般通过启动goroutine启端口进行监听

  • 开启监听
go func() {
      log.Println(http.ListenAndServe("localhost:6060", nil)) 
}()
  • 监听方式
http://localhost:port/debug/pprof/

也可以通过命令行方式

go tool pprof http://localhost/debug/pprof/profile

应用程序监听

应用程序,比如计算fabonacci数列 监听,需要用runtime/pprof

  • 示例代码
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    }
  • 启动监听方式:在应用运行命令后增加--cpuprofile,如下
//cpu的信旋记录到XXX.prof
myproject --cpuprofile=fabonacci.prof

相关文章

网友评论

      本文标题:GO学习笔记(9)-性能监控 go-pprof

      本文链接:https://www.haomeiwen.com/subject/oturultx.html