CPU Profiling with pprof
Collecting and visualizing CPU profiles
CPU Profiling with pprof is a free Go Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
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
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “CPU Profiling with pprof” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “CPU Profiling with pprof”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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