CPU Profiling with pprof
Collecting and visualizing CPU profiles
CPU Profiling with pprof is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is pprof?
net/http/pprof and the runtime/pprof package expose profiling data that shows where your program spends CPU time. The go tool pprof CLI analyses profiles.
HTTP pprof endpoint
Import _ "net/http/pprof" to register profiling endpoints under /debug/pprof/ on an existing HTTP server.
import _ "net/http/pprof"
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()Capturing a CPU profile
Capture 30 seconds of CPU profile via the HTTP endpoint:
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30File-based CPU profiling
For programs without an HTTP server, use runtime/pprof to write a profile file:
f, _ := os.Create("cpu.prof")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
// run workloadProfiling in tests
Run tests with -cpuprofile and -memprofile flags to profile under realistic test load:
go test -cpuprofile cpu.prof -memprofile mem.prof -bench=. ./...go tool pprof interactive mode
Open the profile and explore with interactive commands:
go tool pprof cpu.prof
(pprof) top # top functions by CPU time
(pprof) list main # annotated source
(pprof) web # call graph in browserFlame graph
Generate a flame graph (requires graphviz) with -http=:8080 for an interactive web UI:
go tool pprof -http=:8080 cpu.profInterpreting top output
top shows functions sorted by self time (time in that function) and cumulative time (including callees). Focus on high self-time functions for direct optimisation opportunities.
Goroutine profile
The /debug/pprof/goroutine endpoint shows all goroutine stack traces. Use it to detect goroutine leaks or blocked goroutines.
go tool pprof http://localhost:6060/debug/pprof/goroutineContinuous profiling
For production systems, use continuous profiling services (Pyroscope, Datadog, Google Cloud Profiler) to capture profiles without stopping the service.
pprof in CI
Compare profiles from two versions of your code to detect performance regressions before they reach production.
Quick Check
What does the "self" column in pprof top output represent?
Recap: CPU Profiling with pprof
Key points:
- Import net/http/pprof for HTTP endpoint; runtime/pprof for file
- go tool pprof -http=:8080 for flame graph UI
- top: self time = direct cost; cumulative = total cost including callees
- go test -cpuprofile for benchmark profiling
Frequently asked questions
Is the “CPU Profiling with pprof” lesson free?
Yes — the full text of “CPU Profiling with pprof” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “CPU Profiling with pprof”?
Collecting and visualizing CPU profiles You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “CPU Profiling with pprof” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing Benchmarks with testing.B
- CPU Profiling with pprof
- Heap and Allocation Profiling
- Execution Tracing