05 เมษายน 2567

Profiling golang gin with pprof



Profiling golang gin with pprof

1. Install pprof lib for gin

go get github.com/gin-contrib/pprof


2. Register debug pprof routes

import "github.com/gin-contrib/pprof"
// ... 
r := gin.New() 
pprof.Register(r, &pprof.Options{RoutePrefix: "debug/pprof"}) 
// ...


3. Under debug mode, gin will print debug pprof routes like this



4. Load test to your server, you can use any tool like Apache Bench (ab)

5. Generate profiling report using go tool
go tool pprof -http=localhost:8081 'http://localhost:8080/debug/pprof/profile?seconds=60' 

Note
  • seconds is the time you need to collect samples (default is 30 seconds)
  • http://localhost:8080 is your web server url
  • localhost:8081 is the web report to view virtualization report

6. After 60 seconds, you will be re-direct to localhost:8081 and get virtualization report, and the profile result file will be saved to path /home/{user}/pprof/pprof.xxx.pb.gz


7. You can view the virtualization report from your gz file also by using command
go tool pprof -http=localhost:8081 /home/{user}/pprof/pprof.xxx.pb.gz



References 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

บทความยอดนิยม (ล่าสุด)

บทความยอดนิยม (All Time)