项目作者: gin-contrib

项目描述 :
gin pprof middleware
高级语言: Go
项目地址: git://github.com/gin-contrib/pprof.git
创建时间: 2016-11-30T22:29:09Z
项目社区:https://github.com/gin-contrib/pprof

开源协议:MIT License

下载


pprof

Run Tests
codecov
Go Report Card
GoDoc

gin pprof middleware

Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.

Usage

Start using it

Download and install it:

  1. go get github.com/gin-contrib/pprof

Import it in your code:

  1. import "github.com/gin-contrib/pprof"

Example

  1. package main
  2. import (
  3. "github.com/gin-contrib/pprof"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. router := gin.Default()
  8. pprof.Register(router)
  9. router.Run(":8080")
  10. }

change default path prefix

  1. func main() {
  2. router := gin.Default()
  3. // default is "debug/pprof"
  4. pprof.Register(router, "dev/pprof")
  5. router.Run(":8080")
  6. }

custom router group

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gin-contrib/pprof"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func main() {
  8. router := gin.Default()
  9. debugGroup := router.Group("/debug", func(c *gin.Context) {
  10. if c.Request.Header.Get("Authorization") != "foobar" {
  11. c.AbortWithStatus(http.StatusForbidden)
  12. return
  13. }
  14. c.Next()
  15. })
  16. pprof.RouteRegister(debugGroup, "pprof")
  17. router.Run(":8080")
  18. }

Use the pprof tool

Then use the pprof tool to look at the heap profile:

  1. go tool pprof http://localhost:8080/debug/pprof/heap

Or to look at a 30-second CPU profile:

  1. go tool pprof http://localhost:8080/debug/pprof/profile?seconds=30

Or to look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program:

  1. go tool pprof http://localhost:8080/debug/pprof/block

Or to collect a 5-second execution trace:

  1. wget http://localhost:8080/debug/pprof/trace?seconds=5

Download the pprof profile data:

  1. curl -v -H "Authorization: foobar" -o profile.pb.gz \
  2. http://localhost:8080/debug/pprof/profile?seconds=60
  3. go tool pprof -http=:8099 profile.pb.gz