Skip to content

Commit bedab74

Browse files
committed
pprof plugin
1 parent ed1d5fd commit bedab74

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

pprof/README.MD

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## GVA Pprof&Trace 功能插件
2+
By using Go’s profiling tools to identify and correct specific bottlenecks, we can make programm faster and shrink resource occupation.
3+
#### 开发者:[email protected]
4+
5+
### 使用步骤
6+
7+
#### 1. 前往GVA主程序下的initialize/router.go 在Routers 方法最末尾按照你需要的及安全模式添加本插件
8+
例:
9+
本插件可以采用gva的配置文件 也可以直接写死内容作为配置 建议为gva添加配置文件结构 然后将配置传入:
10+
PluginInit(PrivateGroup, pprof.CreatePprofPlug(global.GVA_CONFIG.Pprof.Prefix))
11+
12+
同样也可以在传入时写死:
13+
PluginInit(PrivateGroup, pprof.CreatePprofPlug("debug/pprof"))
14+
15+
### 2. 配置说明
16+
17+
#### 2-1 全局配置结构体说明
18+
19+
type Pprof struct {
20+
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // path prefix
21+
}
22+
23+
24+
### 3. 方法API
25+
All routers are listed in `plugin\pprof\router\pprof.go`
26+
* pprofRouter.GET("/", pprofHandler(pprof.Index))
27+
Index responds with the pprof-formatted profile named by the request. For example, "/debug/pprof/heap" serves the "heap" profile.
28+
29+
* pprofRouter.GET("/cmdline", pprofHandler(pprof.Cmdline))
30+
Cmdline responds with the running program's command line, with arguments separated by NUL bytes. The package initialization registers it as /debug/pprof/cmdline.
31+
32+
* pprofRouter.GET("/profile", pprofHandler(pprof.Profile))
33+
Profile responds with the pprof-formatted cpu profile. Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified. The package initialization registers it as /debug/pprof/profile.
34+
35+
* pprofRouter.POST("/symbol", pprofHandler(pprof.Symbol))
36+
Symbol looks up the program counters listed in the request, responding with a table mapping program counters to function names. The package initialization registers it as /debug/pprof/symbol.
37+
38+
* pprofRouter.GET("/symbol", pprofHandler(pprof.Symbol))
39+
Symbol looks up the program counters listed in the request, responding with a table mapping program counters to function names. The package initialization registers it as /debug/pprof/symbol.
40+
41+
* pprofRouter.GET("/trace", pprofHandler(pprof.Trace))
42+
Trace responds with the execution trace in binary form. Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified. The package initialization registers it as /debug/pprof/trace.
43+
44+
* pprofRouter.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP))
45+
The endpoint "/allocs" responds with the memory allocations of your running programm.
46+
47+
* pprofRouter.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP))
48+
The endpoint "/block" responds with the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program.
49+
50+
* pprofRouter.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP))
51+
The endpoint "/gorotine" responds with the gorotine profile of your running programm.
52+
53+
* pprofRouter.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP))
54+
The endpoint "/heap" responds with the heap profile of your running programm.
55+
56+
* pprofRouter.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP))
57+
The endpoint "/mutex" responds with the holders of contended mutexes, after calling runtime.SetMutexProfileFraction in your program.
58+
59+
* pprofRouter.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP))
60+
The endpoint "/threadcreate" responds with the created threads profile of your programm.
61+
62+
### Reference:
63+
* Package Doc, https://pkg.go.dev/net/http/pprof
64+
* Profiling Go Programs-The go blog, https://go.dev/blog/pprof

pprof/config/pprof.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package config
2+
3+
type Pprof struct {
4+
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`
5+
}

pprof/global/global.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package global
2+
3+
import "github.com/flipped-aurora/gin-vue-admin/server/plugin/pprof/config"
4+
5+
var PprofConfig = new(config.Pprof)

pprof/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pprof
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/pprof/global"
5+
"github.com/flipped-aurora/gin-vue-admin/server/plugin/pprof/router"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
const (
10+
// default url prefix of pprof
11+
DefaultPrefix = "debug/pprof"
12+
)
13+
14+
type pprofPlugin struct{}
15+
16+
func CreatePprofPlug(Prefix string) *pprofPlugin {
17+
global.PprofConfig.Prefix = Prefix
18+
return &pprofPlugin{}
19+
}
20+
21+
func (*pprofPlugin) Register(group *gin.RouterGroup) {
22+
router.RouterGroupApp.InitPprofRouter(group)
23+
}
24+
25+
func (*pprofPlugin) RouterPath() string {
26+
prefix := DefaultPrefix
27+
if len(global.PprofConfig.Prefix) > 0 {
28+
prefix = global.PprofConfig.Prefix
29+
}
30+
return prefix
31+
}

pprof/router/enter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package router
2+
3+
type RouterGroup struct {
4+
PprofRouter
5+
}
6+
7+
var RouterGroupApp = new(RouterGroup)

pprof/router/pprof.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package router
2+
3+
import (
4+
"net/http"
5+
"net/http/pprof"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
type PprofRouter struct{}
11+
12+
func (s *PprofRouter) InitPprofRouter(Router *gin.RouterGroup) {
13+
pprofRouter := Router // .Use(middleware.OperationRecord())
14+
{
15+
pprofRouter.GET("/", pprofHandler(pprof.Index))
16+
pprofRouter.GET("/cmdline", pprofHandler(pprof.Cmdline))
17+
pprofRouter.GET("/profile", pprofHandler(pprof.Profile))
18+
pprofRouter.POST("/symbol", pprofHandler(pprof.Symbol))
19+
pprofRouter.GET("/symbol", pprofHandler(pprof.Symbol))
20+
pprofRouter.GET("/trace", pprofHandler(pprof.Trace))
21+
pprofRouter.GET("/allocs", pprofHandler(pprof.Handler("allocs").ServeHTTP))
22+
pprofRouter.GET("/block", pprofHandler(pprof.Handler("block").ServeHTTP))
23+
pprofRouter.GET("/goroutine", pprofHandler(pprof.Handler("goroutine").ServeHTTP))
24+
pprofRouter.GET("/heap", pprofHandler(pprof.Handler("heap").ServeHTTP))
25+
pprofRouter.GET("/mutex", pprofHandler(pprof.Handler("mutex").ServeHTTP))
26+
pprofRouter.GET("/threadcreate", pprofHandler(pprof.Handler("threadcreate").ServeHTTP))
27+
}
28+
}
29+
30+
// pprofHandler, transfer `http.HandlerFunc` to `gin.HandlerFunc`
31+
32+
func pprofHandler(h http.HandlerFunc) gin.HandlerFunc {
33+
handler := http.HandlerFunc(h)
34+
return func(c *gin.Context) {
35+
handler.ServeHTTP(c.Writer, c.Request)
36+
}
37+
}

0 commit comments

Comments
 (0)