Stage 3 · Build
Go Fundamentals for SREs
Profiling & Optimization
pprof, trace, escape analysis, and allocation profiling for performance-critical tools.
pprof Overview
pprof is Go's built-in profiling tool. It collects CPU usage, memory allocation, goroutine counts, mutex contention, and block profiles. You can use it in production with minimal overhead.
pprof endpoints can leak sensitive information and consume significant CPU. Always bind to localhost and firewall the port. In Kubernetes, use port-forward to access it securely.
CPU Profiling
CPU profiles show where your program spends execution time. The profiler samples at 100 Hz, capturing stack traces of running goroutines. This reveals hot paths and unexpected bottlenecks.
# Capture 30-second CPU profile
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
# Interactive analysis
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
# Common pprof commands
(pprof) top 20 # Show top 20 functions by CPU time
(pprof) top -cum # Sort by cumulative time
(pprof) list parseJSON # Show source-level breakdown
(pprof) web # Visualize as a graph (requires graphviz)pprof captures a snapshot of CPU usage. Use top to find the hottest functions, list to see line-level detail, and web to visualize call graphs. The cumulative sort reveals which functions are responsible for the most total time.
Heap Profiling
Heap profiles show memory allocation. They reveal which functions allocate the most memory and which objects live on the heap versus the stack.
# Live heap profile
go tool pprof http://localhost:6060/debug/pprof/heap
# After garbage collection
go tool pprof http://localhost:6060/debug/pprof/heap?gc=1
# Common heap commands
(pprof) top 20 -inuse_space # Top by in-use memory
(pprof) top 20 -alloc_space # Top by total allocations
(pprof) top 20 -inuse_objects # Top by object count
(pprof) list processRequest # Source-level breakdowninuse_space shows current memory usage. alloc_space shows total allocations over the program lifetime. Use inuse_space to find memory leaks and alloc_space to find allocation hotspots.
Allocation Profiling
Allocation profiles focus on the number and size of heap allocations. Reducing allocations is often the easiest way to improve performance and reduce GC pressure.
runtime/trace
runtime/trace captures detailed execution traces — goroutine scheduling, system calls, GC events, and network I/O. It is the most powerful tool for understanding concurrency behavior.
Optimization Workflow
Optimization without measurement is guesswork. Follow a repeatable workflow: measure, identify the bottleneck, optimize, verify the improvement.
- Write a benchmark that exercises the code path
- Run the benchmark and save the baseline: go test -bench=. -count=5 > old.txt
- Profile the benchmark to find the bottleneck
- Make a targeted change
- Run the benchmark again and save: go test -bench=. -count=5 > new.txt
- Compare with benchstat: benchstat old.txt new.txt
Never optimize based on intuition. The profiler shows you where time is actually spent. A single unnecessary allocation in a hot loop can dominate your profile, while the code you thought was slow barely registers.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.