首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

net/http/pprof

  • import "net/http/pprof"
  • 概述
  • 索引

概述

软件包pprof通过其HTTP服务器运行时分析数据以pprof可视化工具预期的格式提供服务。

该软件包通常只是为了注册HTTP处理程序的副作用而导入。处理路径全部以/debug/pprof/开头。

要使用pprof,请将此软件包链接到您的程序中:

import _ "net/http/pprof"

如果您的应用程序尚未运行http服务器,则需要启动一个。将“net/http”和“log”添加到您的导入,并将以下代码添加到主函数中:

go func() {
	log.Println(http.ListenAndServe("localhost:6060", nil))
}()

然后使用pprof工具查看堆配置文件:

go tool pprof http://localhost:6060/debug/pprof/heap

或者查看30秒钟的CPU配置文件:

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

或者在程序中调用runtime.SetBlockProfileRate之后查看goroutine阻止配置文件:

go tool pprof http://localhost:6060/debug/pprof/block

或者收集5秒钟的执行轨迹:

wget http://localhost:6060/debug/pprof/trace?seconds=5

或者在程序中调用runtime.SetMutexProfileFraction之后查看争用互斥锁的持有者:

go tool pprof http://localhost:6060/debug/pprof/mutex

要查看所有可用的配置文件,请在浏览器中打开http://localhost:6060/debug/pprof/。

欲了解该设施的实际情况,请访问

https://blog.golang.org/2011/06/profiling-go-programs.html

索引

  • func Cmdline(w http.ResponseWriter, r *http.Request)
  • func Handler(name string) http.Handler
  • func Index(w http.ResponseWriter, r *http.Request)
  • func Profile(w http.ResponseWriter, r *http.Request)
  • func Symbol(w http.ResponseWriter, r *http.Request)
  • func Trace(w http.ResponseWriter, r *http.Request)

文件包

func Cmdline(w http.ResponseWriter, r *http.Request)

Cmdline用正在运行的程序的命令行响应,参数用NUL字节分隔。软件包初始化将其注册为/debug/pprof/cmdline。

func Handler(name string) http.Handler

处理程序返回一个HTTP处理程序来提供指定的配置文件。

func Index(w http.ResponseWriter, r *http.Request)

索引使用请求命名的pprof格式的配置文件进行响应。例如,“/debug/pprof/heap”服务于“堆”配置文件。索引通过列出可用配置文件的HTML页面响应“/debug/pprof/”的请求。

func Profile(w http.ResponseWriter, r *http.Request)

配置文件使用pprof格式的cpu配置文件进行响应。软件包初始化将其注册为/debug/pprof/profile。

func Symbol(w http.ResponseWriter, r *http.Request)

Symbol查找请求中列出的程序计数器,并使用表映射程序计数器响应函数名称。软件包初始化将其注册为/ /debug/pprof/symbol。

func Trace(w http.ResponseWriter, r *http.Request)

Trace以二进制形式执行跟踪。跟踪持续时间指定以秒为单位的GET参数,如果未指定,则持续1秒。软件包初始化将其注册为/debug/pprof/trace。

扫码关注腾讯云开发者

领取腾讯云代金券