github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ginx/ginpprof/README.md (about) 1 # ginpprof 2 3 A wrapper for [golang web framework gin](https://github.com/gin-gonic/gin) to use `net/http/pprof` easily. 4 5 Forked from [DeanThompson/ginpprof](github.com/DeanThompson/ginpprof). 6 7 ## Install 8 9 `go get github.com/bingoohuang/ginx/...` 10 11 ## Usage 12 13 ```go 14 package main 15 16 import ( 17 "github.com/gin-gonic/gin" 18 "github.com/bingoohuang/gg/pkg/ginx/ginpprof" 19 ) 20 21 func main() { 22 router := gin.Default() 23 24 router.GET("/ping", func(c *gin.Context) { 25 c.String(200, "pong") 26 }) 27 28 // automatically add routers for net/http/pprof 29 // e.g. /debug/pprof, /debug/pprof/heap, etc. 30 ginpprof.Wrap(router) 31 32 // ginpprof also plays well with *gin.RouterGroup 33 // group := router.Group("/debug/pprof") 34 // ginpprof.WrapGroup(group) 35 36 router.Run(":57047") 37 } 38 ``` 39 40 Start this server, and you will see such outputs: 41 42 ```text 43 [GIN-debug] GET /ping --> main.main.func1 (3 handlers) 44 [GIN-debug] GET /debug/pprof/ --> github.com/DeanThompson/ginpprof.IndexHandler.func1 (3 handlers) 45 [GIN-debug] GET /debug/pprof/heap --> github.com/DeanThompson/ginpprof.HeapHandler.func1 (3 handlers) 46 [GIN-debug] GET /debug/pprof/goroutine --> github.com/DeanThompson/ginpprof.GoroutineHandler.func1 (3 handlers) 47 [GIN-debug] GET /debug/pprof/block --> github.com/DeanThompson/ginpprof.BlockHandler.func1 (3 handlers) 48 [GIN-debug] GET /debug/pprof/threadcreate --> github.com/DeanThompson/ginpprof.ThreadCreateHandler.func1 (3 handlers) 49 [GIN-debug] GET /debug/pprof/cmdline --> github.com/DeanThompson/ginpprof.CmdlineHandler.func1 (3 handlers) 50 [GIN-debug] GET /debug/pprof/profile --> github.com/DeanThompson/ginpprof.ProfileHandler.func1 (3 handlers) 51 [GIN-debug] GET /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers) 52 [GIN-debug] POST /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers) 53 [GIN-debug] GET /debug/pprof/trace --> github.com/DeanThompson/ginpprof.TraceHandler.func1 (3 handlers) 54 [GIN-debug] GET /debug/pprof/mutex --> github.com/DeanThompson/ginpprof.MutexHandler.func1 (3 handlers) 55 [GIN-debug] Listening and serving HTTP on :8080 56 ``` 57 58 Now visit [http://127.0.0.1:57047/debug/pprof/](http://127.0.0.1:57047/debug/pprof/) and you'll see what you want. 59 60 Have Fun. 61 62 63 1. `while true; do date '+%Y-%m-%d %H:%M:%S'; ps aux | grep gologdemo | grep -v grep | grep -v tail; sleep 10; done` 64 1. `go tool pprof -http=:8080 http://127.0.0.1:57047/debug/pprof/profile` 65 1. `go tool pprof -http=:8080 http://127.0.0.1:57047/debug/pprof/heap` 66