preface

Today I will continue to share some information about using Go’s official performance library pprof for performance analysis. Last article: I spent a lot of time in pprof and Go-Torch explaining how to use Pprof to collect performance metrics for Go applications, how to find slow-running functions, and the details of the performance consumption of each part of the function. The focus of this section will be on how to add support for Pprof HTTP requests in both Echo and Gin, since Pprof only provides support for ServerMux routing of NET/HTTP packages, which requires a little extra integration work to be used in Echo and Gin.

After being integrated into the framework and able to access several routes provided by pprof through HTTP, the Go Tool pprof tool still accesses these urls to bring the performance data to the local area for later analysis. The subsequent operation of performance data collection and analysis is exactly the same as described in the previous article, and there is no difference due to different frameworks.

Use pprof in Echo

Because the multiplexer ServerMux used by the Echo framework is customized, routes provided by Pprof need to be manually registered. There are several packages that can be used directly, but none of them are officially provided. Later, I looked at the source code of the routing Handler provided by Pprof. I only need to convert it into the routing Handler of Echo framework, which can normally handle those PPROF related requests. The specific conversion operation is very simple, SO I put the code directly.

func RegisterRoutes(engine *echo.Echo) {
  router := engine.Group("")...// The following routes are registered according to the data needs to be collected, not all
	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)))
}
Copy the code

After registering routes, you need to configure WriteTimeout for Echo framework to ensure that the WriteTimeout interval is longer than the time for pprof to collect data. This configuration corresponds to the seconds parameter of the /debug/pprof route. The default collection time is 30 seconds. For example, if I usually need to collect data for 60 seconds, WriteTimeout should be configured for more than 60 seconds. The specific configuration is as follows:

If pprof profiling lasts longer than WriteTimeout, an error “Profile Duration exceeds Server’s WriteTimeout” can be raised.

RegisterRoutes(engine)

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

After both steps are set up, you can perform performance analysis using the pprof subcommand described in the file above

➜ 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.01 s, 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)Copy the code

For details on how to use pprof subcommands, see pprof and Go-Torch in Golang.

Use pprof in Gin

The Gin framework provides pPROF routing access by installing the gin-contrib/pprof package provided by the Gin project.

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

package main

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

func main(a) {
  router := gin.Default()
  pprof.Register(router)
  router.Run(": 8080")}Copy the code

The package also supports partitioning pprof routes into separate routing groups, as documented in gin-contrib/pprof.

After the above configuration is complete, you can start the service and then use go Tool pprof for data collection and analysis:

  • Collect memory usage information
go tool pprof http://localhost:8080/debug/pprof/heap
Copy the code
  • Collect CPU usage information
go tool pprof http://localhost:8080/debug/pprof/profile
Copy the code

conclusion

Go Tool Pprof can be used to analyze the performance of all types of GO applications. This article mainly discusses how to enable pprof performance collection support in Echo and Gin frameworks. The specific methods and steps of program performance analysis are the same as those emphasized in pprof and Go-Torch, the first Golang program performance Analysis.

Thank you for reading here, there will be an article that uses Pprof to analyze gRPC service performance, but also please support, continue to pay attention to the public number “network management talking BI talking”. Finally the iron needle children beg attention, beg forward, beg to see, do not say next time certain ~!