tezvyn:

Go's pprof: Finding Your Code's Hotspots

AI-drafted, machine-checkedSource: github.comintermediate

pprof is a heat map for your code, revealing which functions consume the most CPU. It samples your program's call stacks to find performance hotspots. Use it to diagnose slow API endpoints or high-CPU background jobs. The footgun: profiling under no load.

WHY IT EXISTS When an application is slow, guessing where the bottleneck is is inefficient and often wrong. You need empirical data to guide optimization efforts. pprof was created to provide a data-driven way to find performance hotspots, replacing guesswork with concrete evidence of where a program spends its CPU cycles.

THE MENTAL MODEL Think of pprof as a detective taking snapshots of your program's activity. It doesn't watch every single instruction. Instead, it periodically (e.g., 100 times per second) records what function is currently running and its entire call stack. After collecting thousands of these samples, it aggregates them. Functions that appear in many samples are the "hotspots" where your program is spending the most time. It's a statistical method, not an exact count, but it's highly effective for finding significant performance issues.

HOW IT WORKS The Go runtime includes a built-in profiler. When you enable CPU profiling, the runtime sets a timer. By default, every 10 milliseconds it interrupts the program and records the call stack of the currently executing goroutine. These stacks are collected into a binary format called profile.proto. The go tool pprof command then reads this data. It can symbolize the memory addresses back into human-readable function names and line numbers, allowing you to visualize the data as a text report, a call graph, or in an interactive terminal for deeper analysis.

WHEN TO USE IT Use pprof when your Go application has unexpectedly high CPU usage or a specific operation is unacceptably slow. It's the first tool to reach for when optimizing Go code. It is especially powerful for long-running services, as you can connect to a live server, collect a profile for 30 seconds, and analyze it without restarting the service. This is ideal for debugging performance issues in production or staging environments under real load.

WHEN NOT TO USE IT CPU profiling is for CPU time, not for blocking operations. If your program is slow because it's waiting on a network request, a database query, or a disk read, the CPU profiler won't show you that. For I/O-bound or concurrency-related slowness (like waiting on a channel or mutex), use other profiles like the block, mutex, or goroutine profiles, which are also part of the pprof ecosystem but track different events.

ONE CANONICAL EXAMPLE A web server has a /v1/users endpoint that is slow. You enable pprof by importing net/http/pprof. You then run go tool pprof http://your-server/debug/pprof/profile?seconds=30. After 30 seconds, pprof drops you into an interactive shell. Typing top shows the top 10 functions by CPU usage. You see 80% of the time is spent in a function called json.Marshal. This tells you that JSON serialization is your bottleneck, not database access or business logic.

Read the original → github.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.