The post is to illustrate issues encountered during a web service debug/inspection process using pprof. Some insightful golang-program-performance-debugging posts also been mentioned.
Note that even though your http api server may not experience obvious latency, it is worth digging deeper of it so as to understand more about it. It may helps avoiding memory leak and make the web service pure without unnecessary overhead.
Also note that profiling using pprof
is not only the approach to diagnose a program. Check the official post to see other diagnostics solutions like debugging
, tracing
, runtime statistics and events
Table of Content
- Getting Started
- Deploy pprof
- Ensure GC Runs
- Inspect in Initialization
- Inspect While Running
- References
Getting Started
Deploy pprof
- For most of the web services developed by
golang
:
import _ "net/http/pprof"
...
http.ListenAndServe("localhost:6060", nil)
- For web framework: go-chi
import "github.com/go-chi/chi/middleware"
....
r = chi.NewRouter()
...
r.Mount("/debug", middleware.Profiler())
Then you can access http://ip:port/debug/pprof/
directly using browser as your app runs. For heap inpsectation:
pprof
behind the scenes is using theruntime.MemProfile
function, which by default collects allocation information on each512KB
of allocated bytes. It can be adjust usingruntime.MemProfileRate
Addtionally, golang provides a cmd tool pprof