以下所有观点都是个人愚见,有不同建议或补充的的欢迎emial,
aboutme
原文章地址
pprof的简介
pprof是golang标准库里面的其中一个库,它通过其HTTP服务器得到运行时的分析数据,从而给pprof可视化工具提供数据分析来源。它可以用来分析性能消耗,分析内存泄漏,死锁等。
具体使用可以了解官方包
pprof
,那我如何在http中使用pprof?如何在已有http或者https服务上使用pprof呢? 这些答案在标准库找不到,随在此记录一下。
如何启动pprof
在官方包中已经给出了例子:
package main
import "net/http"
import _ "net/http/pprof" // 初始化pprof
func main() {
// do something
...
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil)) //启动http服务器
}()
}
启动完后,就可以使用go自动的工具
go tool pprof
如:
go tool pprof http://localhost:6060/debug/pprof/heap // 获取堆的相关数据
go tool pprof http://localhost:6060/debug/pprof/profile // 获取30s内cpu的相关数据
go tool pprof http://localhost:6060/debug/pprof/block // 在你程序调用 runtime.SetBlockProfileRate ,查看goroutine阻塞的相关数据
go tool pprof http://localhost:6060/debug/pprof/mutex // 在你程序调用 runtime.SetMutexProfileFraction,查看谁占用mutex
为什么我自定义mux的http服务不能用?
启动自定义mux的http服务器
package main
import (
"net/http"
_ "net/http/pprof"
)
func main() {
// 启动一个自定义mux的http服务器
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})
http.ListenAndServe(":6060", mux)
}
得到结果:
404 Not Found