...
1# pprof
2
3[](https://github.com/gin-contrib/pprof/actions/workflows/go.yml)
4[](https://codecov.io/gh/gin-contrib/pprof)
5[](https://goreportcard.com/report/github.com/gin-contrib/pprof)
6[](https://godoc.org/github.com/gin-contrib/pprof)
7[](https://gitter.im/gin-gonic/gin)
8
9gin pprof middleware
10
11> Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.
12
13## Usage
14
15### Start using it
16
17Download and install it:
18
19```bash
20go get github.com/gin-contrib/pprof
21```
22
23Import it in your code:
24
25```go
26import "github.com/gin-contrib/pprof"
27```
28
29### Example
30
31```go
32package main
33
34import (
35 "github.com/gin-contrib/pprof"
36 "github.com/gin-gonic/gin"
37)
38
39func main() {
40 router := gin.Default()
41 pprof.Register(router)
42 router.Run(":8080")
43}
44```
45
46### change default path prefix
47
48```go
49func main() {
50 router := gin.Default()
51 // default is "debug/pprof"
52 pprof.Register(router, "dev/pprof")
53 router.Run(":8080")
54}
55```
56
57### custom router group
58
59```go
60package main
61
62import (
63 "net/http"
64
65 "github.com/gin-contrib/pprof"
66 "github.com/gin-gonic/gin"
67)
68
69func main() {
70 router := gin.Default()
71 pprof.Register(router)
72 adminGroup := router.Group("/admin", func(c *gin.Context) {
73 if c.Request.Header.Get("Authorization") != "foobar" {
74 c.AbortWithStatus(http.StatusForbidden)
75 return
76 }
77 c.Next()
78 })
79 pprof.RouteRegister(adminGroup, "pprof")
80 router.Run(":8080")
81}
82
83```
84
85### Use the pprof tool
86
87Then use the pprof tool to look at the heap profile:
88
89```bash
90go tool pprof http://localhost:8080/debug/pprof/heap
91```
92
93Or to look at a 30-second CPU profile:
94
95```bash
96go tool pprof http://localhost:8080/debug/pprof/profile
97```
98
99Or to look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program:
100
101```bash
102go tool pprof http://localhost:8080/debug/pprof/block
103```
104
105Or to collect a 5-second execution trace:
106
107```bash
108wget http://localhost:8080/debug/pprof/trace?seconds=5
109```
View as plain text