ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# package pprof `import "net/http/pprof"` pprof包通过它的HTTP服务端提供pprof可视化工具期望格式的运行时剖面文件数据服务。关于pprof的更多信息,参见[http://code.google.com/p/google-perftools/](http://code.google.com/p/google-perftools/)。 本包一般只需导入获取其注册HTTP处理器的副作用。处理器的路径以/debug/pprof/开始。 要使用pprof,在你的程序里导入本包: ``` import _ "net/http/pprof" ``` 如果你的应用还没有运行http服务器,你需要开始一个http服务器。添加"net/http"包和"log"包到你的导入列表,然后在main函数开始处添加如下代码: ``` 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 ``` 或查看go程阻塞剖面: ``` go tool pprof http://localhost:6060/debug/pprof/block ``` 要查看所有可用的剖面,在你的浏览器阅读[http://localhost:6060/debug/pprof/](http://localhost:6060/debug/pprof/)。要学习这些运转的设施,访问: ``` http://blog.golang.org/2011/06/profiling-go-programs.html ``` ## Index * [func Handler(name string) http.Handler](#Handler) * [func Cmdline(w http.ResponseWriter, r \*http.Request)](#Cmdline) * [func Index(w http.ResponseWriter, r \*http.Request)](#Index) * [func Profile(w http.ResponseWriter, r \*http.Request)](#Profile) * [func Symbol(w http.ResponseWriter, r \*http.Request)](#Symbol) ## func [Handler](http://code.google.com/p/go/source/browse/src/pkg/net/http/pprof/pprof.go?name=release#150 "View Source") ``` func Handler(name string) http.Handler ``` Handler返回一个提供name指定的剖面文件的服务的HTTP处理器。 ## func [Cmdline](http://code.google.com/p/go/source/browse/src/pkg/net/http/pprof/pprof.go?name=release#72 "View Source") ``` func Cmdline(w http.ResponseWriter, r *http.Request) ``` Cmdline回应执行中程序的命令行,采用NUL字节分隔的参数。本包将它注册在/debug/pprof/cmdline。 ## func [Index](http://code.google.com/p/go/source/browse/src/pkg/net/http/pprof/pprof.go?name=release#173 "View Source") ``` func Index(w http.ResponseWriter, r *http.Request) ``` Index回复请求要求的pprof格式的剖面。例如,"/debug/pprof/heap"会回复"heap"剖面。Index会回复"/debug/pprof/" 请求一个列出所有可用的剖面的HTML页面。 ## func [Profile](http://code.google.com/p/go/source/browse/src/pkg/net/http/pprof/pprof.go?name=release#79 "View Source") ``` func Profile(w http.ResponseWriter, r *http.Request) ``` Profile回复pprof格式的CPU剖面。本包将它注册在/debug/pprof/profile。 ## func [Symbol](http://code.google.com/p/go/source/browse/src/pkg/net/http/pprof/pprof.go?name=release#104 "View Source") ``` func Symbol(w http.ResponseWriter, r *http.Request) ``` Symbol查看请求中列出的程序计数器,回复一个映射程序计数器到函数名的表格。本包将它注册在/debug/pprof/symbol。