pprof
package for memory and CPU profiling. This section provides guidance on setting up and retrieving profiles using pprof
. These profiles are valuable for troubleshooting issues and can sometimes be the only way to gain meaningful context.
Enable Profiling
To enable thepprof
endpoints, start the router with the following environment variable:
The
pprof
HTTP server will be accessible at http://localhost:6060
. Exposing this endpoint to production environments is highly discouraged due to security risks.-
/debug/pprof/heap
β Memory profile. -
/debug/pprof/profile
β CPU profile. -
/debug/pprof/goroutine
β Goroutine profile. -
/debug/pprof/threadcreate
β Thread creation profile. -
/debug/pprof/block
β Block profile.
Downloading the Appropriate Profiles
To troubleshoot issues effectively, categorize them into the following three types: Depending on the issue, you can download individual profiles or generate a ZIP archive containing a set of basic profiles. This is useful when you canβt categorize the issue yourself.1. CPU Utilization
To investigate CPU-related issues, you can fetch the CPU profile by running:2. Memory Utilization
To diagnose memory-related issues, you can download the heap profile:3. Blocking and Synchronization
To identify deadlocks or goroutine-related issues, you can fetch the goroutine profile:-
Block Profile: Captures blocking events caused by synchronization primitives.
-
Thread Creation Profile: Identifies issues related to excessive thread creation.
go tool pprof
interactive commands such as top
, list
, peek
, and web
.
Best Practices for Capturing and Sharing Performance Profiles
By attaching these profiles, you provide invaluable information for diagnosing performance bottlenecks and crashes efficiently!Considerations Before Exporting:
- Run with a real workload: Capture profiles during actual usage scenarios to ensure meaningful data.
- Use an adequate duration: For CPU profiles, longer capture times (10β30 seconds) yield more useful information.
- Avoid noise: Terminate other background processes to minimize interference in the profiles.