前言

GopprofpprofGoEchoGinpprofpprofnet/httpServerMuxEchoGin
pprofgo tool pprof

在Echo中使用pprof

EchoServerMux是pprofpprofHandlerEchoHandlerpprof
func RegisterRoutes(engine *echo.Echo) {
  router := engine.Group("")
  ......
  // 下面的路由根据要采集的数据需求注册,不用全都注册
 router.GET("/debug/pprof", echo.WrapHandler(http.HandlerFunc(pprof.Index)))
 router.GET("/debug/pprof/allocs", echo.WrapHandler(http.HandlerFunc(pprof.Index)))
 router.GET("/debug/pprof/block", echo.WrapHandler(http.HandlerFunc(pprof.Index)))
 router.GET("/debug/pprof/goroutine", echo.WrapHandler(http.HandlerFunc(pprof.Index)))
 router.GET("/debug/pprof/heap", echo.WrapHandler(http.HandlerFunc(pprof.Index)))
 router.GET("/debug/pprof/mutex", echo.WrapHandler(http.HandlerFunc(pprof.Index)))
 router.GET("/debug/pprof/cmdline", echo.WrapHandler(http.HandlerFunc(pprof.Cmdline)))
 router.GET("/debug/pprof/profile", echo.WrapHandler(http.HandlerFunc(pprof.Profile)))
 router.GET("/debug/pprof/symbol", echo.WrapHandler(http.HandlerFunc(pprof.Symbol)))
 router.GET("/debug/pprof/trace", echo.WrapHandler(http.HandlerFunc(pprof.Trace)))
}
EchoWriteTimeoutpprof/debug/pprofsecondsWriteTimeout

如果pprof做profiling的时间超过WriteTimeout会引发一个 "profile duration exceeds server's WriteTimeout"的错误。

RegisterRoutes(engine)

err := engine.StartServer(&http.Server{
   Addr:              addr,
   ReadTimeout:       time.Second * 5,
   ReadHeaderTimeout: time.Second * 2,
   WriteTimeout:      time.Second * 90,
})

上面两步都设置完后就能够按照上面文件里介绍的pprof子命令进行性能分析了

➜ go tool pprof http://{server_ip}:{port}/debug/pprof/profile
Fetching profile over HTTP from http://localhost/debug/pprof/profile
Saved profile in /Users/Kev/pprof/pprof.samples.cpu.005.pb.gz
Type: cpu
Time: Nov 15, 2020 at 3:32pm (CST)
Duration: 30.01s, Total samples = 0
No samples were found with the default sample value type.
Try "sample_index" command to analyze different sample values.
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
pprof

在Gin中使用pprof

GinGingin-contrib/pprofpprof
import "github.com/gin-contrib/pprof"

package main

import (
 "github.com/gin-contrib/pprof"
 "github.com/gin-gonic/gin"
)

func main() {
  router := gin.Default()
  pprof.Register(router)
  router.Run(":8080")
}
pprof
go tool pprof
  • 内存使用信息采集

go tool pprof http://localhost:8080/debug/pprof/heap
  • CPU使用情况信息采集

go tool pprof http://localhost:8080/debug/pprof/profile

总结

go tool pprofGoEchoGinpprof

近期文章推荐

pprofgRPC