import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello world\n")
for i := 0; i < 102400; i++ {
s := make([]byte, 3)
fmt.Sprint(s, "hello world\n")
}
})
log.Fatal(http.ListenAndServe("localhost:7777", nil))
}
-
通过页面访问:http://127.0.0.1:7777/debug/pprof/
/debug/pprof/
Types of profiles available: Count Profile 0 allocs 0 block 0 cmdline 4 goroutine 0 heap 0 mutex 0 profile 8 threadcreate 0 trace full goroutine stack dump
-
通过终端:go tool pprof http://localhost:7777/debug/pprof/profile?seconds=3
Saved profile in /home/blue/pprof/pprof.http_pprof.samples.cpu.001.pb.gz File: http_pprof Type: cpu Time: Feb 21, 2020 at 2:07pm (CST) Duration: 3s, Total samples = 0 No samples were found with the default sample value type. Try "sample_index" command to analyze different sample values. Entering interactive mode (type "help" for commands, "o" for options)
-
进入pprof终端,通过help查看帮助文档,对采集的数据进行分析。例如:top10 会输出cpu占用前10的函数
Showing nodes accounting for 0, 0% of 0 total flat flat% sum% cum cum%
flat:给定函数上运行耗时
flat%:CPU 运行耗时总比例 sum%:给定函数累积使用 CPU 总比例 cum:当前函数加上它之上的调用运行总耗时 cum%:CPU 运行耗时总比例
-
获取heap信息:go tool pprof http://localhost:7777/debug/pprof/heap
-
获取block信息:go tool pprof http://localhost:6060/debug/pprof/block
-
获取mutex信息:go tool pprof http://localhost:6060/debug/pprof/mutex
-
-
通过web命令:
- 进入pprof终端:go tool pprof http://localhost:7777/debug/pprof/profile?seconds=10
- 在pprof终端中输入:web 命令