golang pprof

pprof

使用net/http/pprof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package server

import (
"log"
"net/http/pprof"

"github.com/gin-gonic/gin"
)

func ProfileHandlers() *gin.Engine {
e := gin.Default()
e.GET("/", func(context *gin.Context) {
context.Redirect(301, context.Request.RequestURI+"/pprof")
})

g := e.Group("/internal/debug/pprof")
{
g.GET("/", gin.WrapF(pprof.Index))
g.GET("/cmdline", gin.WrapF(pprof.Cmdline))
g.GET("/profile", gin.WrapF(pprof.Profile)) // cpu profile
g.GET("/symbol", gin.WrapF(pprof.Symbol))
g.GET("/trace", gin.WrapF(pprof.Trace))
g.GET("/block", gin.WrapH(pprof.Handler("block"))) //
g.GET("/heap", gin.WrapH(pprof.Handler("heap"))) // heap allocation
g.GET("/goroutine", gin.WrapH(pprof.Handler("goroutine"))) // current goroutine
g.GET("/threadcreate", gin.WrapH(pprof.Handler("threadcreate"))) // os thread create
}

return e
}

func LoadProfileServer(port string) {
go func() {
log.Fatalln(ProfileHandlers().Run(port))
}()
}

在main函数中启动profile server

1
2
3
4
5
6
func main() {
...

LoadProfileServer(":8080")
...
}

参考