Go ships its benchmarking and profiling tools with the standard toolchain. You measure speed with go test -bench, and you find out where time and memory go with pprof, the execution tracer and go build escape analysis. The reliable workflow is to write a benchmark, profile it, change one thing, and compare results with benchstat before and after.
Optimising without measurement usually targets the wrong code, so start with a benchmark that reflects real usage.
A benchmark is a function named BenchmarkXxx in a _test.go file. Since Go 1.24 the preferred loop form is b.Loop(), which runs the setup once, prevents the compiler from optimising away the measured call, and decides the iteration count for you. The classic for i := 0; i < b.N; i++ loop still works and is common in older code.
Run it with allocation statistics:
The columns are the iteration count, time per operation, bytes allocated per operation and allocations per operation. The -8 suffix is GOMAXPROCS. Use -count to repeat runs so you can judge noise, and -run=^$ to skip unit tests while benchmarking.
Use b.Run to compare variants and b.ResetTimer() after expensive setup when you use the older loop form.
A single run is not evidence. Save outputs from before and after, then compare them statistically.
benchstat reports the change per benchmark with a confidence interval, and prints ~ when the difference is not statistically significant.
Benchmarks can write profiles directly.
The web UI shows a flame graph, a call graph and a source view. In the terminal, go tool pprof cpu.out opens an interactive prompt with top, list FuncName and web.
| Profile | What it shows | Typical question |
|---|---|---|
| CPU | Where samples of running code land | Which function burns CPU? |
Heap (-memprofile) | Live and total allocations | What allocates most? |
| Allocs | All allocations since start | Where is garbage created? |
| Block | Time blocked on sync primitives | Where do goroutines wait? |
| Mutex | Contention on mutexes | Which lock is hot? |
| Goroutine | Stacks of all goroutines | Are goroutines leaking? |
Use -alloc_space or -inuse_space in go tool pprof to switch the heap view between total allocated bytes and bytes still live.
For a long-running HTTP service, import net/http/pprof for its side effects and expose it on an internal port only.
Then capture a 30 second CPU profile:
Never expose /debug/pprof on a public interface, because it leaks internals and can be used to slow the process down.
CPU profiles do not show scheduling delays, garbage collection pauses or goroutine blocking over time. The execution tracer does.
To understand allocations at compile time, ask the compiler why a value moves to the heap:
Lines such as moved to heap: x or escapes to heap point to allocations you may remove by returning values instead of pointers or by reusing buffers with sync.Pool.
Go 1.21 and later can also use profile-guided optimization (PGO). Place a representative CPU profile as default.pgo in the main package directory and go build uses it automatically to inline hot calls and devirtualize interface calls. Treat the gains as workload dependent and measure them.
-count=10 and -benchmem.list.make([]T, 0, n) or avoiding repeated string concatenation.benchstat.b.N is the number of iterations the testing package chooses so the benchmark runs long enough to be measurable. In Go 1.24 and later, b.Loop() replaces the manual loop and manages the count for you.
Add -benchmem to the command, or call b.ReportAllocs() inside the benchmark. The output includes B/op and allocs/op.
CPU profiling has low overhead, commonly a few percent, so short samples are normally acceptable. Keep the endpoint private and authenticated, and sample for limited periods.
A benchmark measures how fast something runs. A profile explains where the time or memory goes. Use benchmarks to detect and confirm change, and profiles to decide what to change.
Go's toolchain covers the whole measurement loop: go test -bench for timing, -benchmem for allocations, benchstat for trustworthy comparisons, pprof for CPU and memory hot spots, go tool trace for scheduling behaviour, and compiler escape analysis for allocations. Measure first, change one thing at a time, and keep the benchmark with the code so regressions are easy to catch.