Go ships with its profiling and performance tools in the standard distribution. Use runtime/pprof or net/http/pprof to collect CPU, heap, goroutine, block and mutex profiles, go test -bench to measure small pieces of code, and runtime/trace with go tool trace to see how goroutines are scheduled over time. Together they let you find the real bottleneck before you change any code.
The usual loop is: measure with a benchmark, profile to find where time or memory goes, change one thing, then measure again. This article shows each tool with runnable code and explains how to read the output.
net/http/pprofruntime/pproftesting packageruntime/traceEach tool answers a different question, so pick the one that matches your symptom.
| Symptom | Tool | What it shows |
|---|---|---|
| High CPU usage | CPU profile | Which functions burn CPU time |
| Memory keeps growing or GC is busy | Heap and allocs profiles | Which call sites allocate |
| Requests stall but CPU is idle | Block and mutex profiles | Where goroutines wait on channels or locks |
| Goroutine count keeps rising | Goroutine profile | Stack of every live goroutine |
| Latency spikes, uneven parallelism | Execution trace | Scheduler, GC and syscall timeline |
| Need to compare two implementations | Benchmark with benchstat | Statistically sound timing differences |
A profile is a statistical sample, so it tells you where to look. A trace records events in order, so it tells you when things happened.
net/http/pprofImporting net/http/pprof for its side effect registers handlers under /debug/pprof/ on http.DefaultServeMux. Serve them on a separate, private listener so they are never exposed to the internet.
While the program runs, collect a 30 second CPU profile and open the interactive viewer:
Inside the prompt, three commands cover most needs:
top10 lists the ten most expensive functions. flat is time spent in the function itself and cum includes its callees.list work prints the source of matching functions with time per line.web renders a call graph and needs Graphviz installed.For a browser UI with flame graphs, run go tool pprof -http=:8081 <profile>. You can pass either a URL or a saved profile file.
The same endpoint serves several profile types:
heap shows memory that is live at the last completed GC. Use -sample_index=inuse_space for current usage and alloc_space for everything allocated since start.allocs is the same data with allocation counts as the default view, which helps when many small allocations drive GC cost.goroutine shows every goroutine stack. A growing count of identical stacks usually means a leak.block and mutex are disabled by default because they add overhead. Turn them on in main:Use a rate of 1 only while investigating. In production a larger sampling interval keeps the cost low.
To compare two heap snapshots and see only what grew, use go tool pprof -base old.pb.gz new.pb.gz.
runtime/pprofPrograms that are not servers can write a profile to a file directly.
Then run go tool pprof cpu.pb.gz. For a heap snapshot, call pprof.WriteHeapProfile(f) or pprof.Lookup("heap").WriteTo(f, 0) at the point you care about.
testing packageBenchmarks live in _test.go files. From Go 1.24 you can write the loop with b.Loop(), which keeps the compiler from optimising the work away and excludes setup code from the timing. On older versions use for i := 0; i < b.N; i++.
Run them with allocation statistics and write profiles at the same time:
The output has columns for nanoseconds per operation, bytes per operation and allocations per operation. Run each benchmark several times (-count=10) and compare old and new results with benchstat, available from golang.org/x/perf/cmd/benchstat, rather than trusting a single run.
runtime/traceA trace records goroutine creation and blocking, GC phases, syscalls and processor activity. It is the right tool when the CPU profile looks flat but latency is poor.
Open the result with:
The tool starts a local web UI with a timeline per processor, goroutine analysis and scheduler latency views. For a service, fetch a trace from /debug/pprof/trace?seconds=5. Go 1.25 added a flight recorder in runtime/trace that keeps a rolling in-memory window of recent events, so you can save a trace right after something unusual happens.
You can also add your own regions and tasks with trace.NewTask and trace.WithRegion to label parts of a request.
make([]T, 0, n), use strings.Builder, and reuse buffers with sync.Pool when measurements justify it./debug/pprof with a private port or authentication.default.pgo in the main package directory to improve builds. Typical gains are modest, so measure them.CPU and heap profiling are cheap enough for production when sampled at default rates. Expose the endpoints only on an internal address, because they reveal internals and a long trace or profile request adds load.
flat is time spent inside the function's own code. cum is the function plus everything it called. A function with low flat and high cum is a good place to look at its callees.
Either the function really does not allocate or the compiler removed the work. Use b.Loop() or store results in a package-level variable so the work cannot be eliminated.
Start with pprof to see where resources go. Use trace when the problem is timing, such as goroutines waiting, GC pauses or poor parallelism.
Go gives you everything needed to tune a program without third-party tools. Use benchmarks for controlled comparisons, pprof for CPU, memory, blocking and goroutine questions, and runtime/trace for timeline problems. Measure, change one thing, and measure again.