By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A benchmark tells you whether a change helped; a profile tells you where the time goes. Run the benchmark first to establish a baseline, then take a CPU or memory profile to find the hot path. Optimizing by inspection is how teams rewrite the wrong function and make the code harder to read for no measurable gain.
Benchmarks are noisy. Run them with -count=5 or more, look at the distribution, and compare before/after with a tool or a table rather than eyeballing a single line. b.ReportAllocs() adds allocation counts, and b.SetBytes reports throughput. Keep the benchmark function's work identical to production or you will optimize an imaginary path.
Go's runtime/pprof package (and net/http/pprof for live services) produces four profiles you will actually use: CPU time, heap allocations, mutex contention and goroutine blocking. go tool pprof turns them into a top list, a call graph or a flame graph in your browser. For a live service, mount /debug/pprof on a localhost-only listener with authentication; it is a diagnostic backdoor, not a public endpoint.
package analyze_test
import (
"testing"
"relay/internal/analyze"
)
func BenchmarkParseLine(b *testing.B) {
line := []byte(`{"service":"api","level":"info","status":200,"message":"ok"}`)
b.ReportAllocs()
b.SetBytes(int64(len(line)))
for i := 0; i < b.N; i++ {
if _, err := analyze.ParseLine(line); err != nil {
b.Fatal(err)
}
}
}package main
import (
"log/slog"
"net/http"
_ "net/http/pprof"
)
func main() {
slog.Info("pprof listening on 127.0.0.1:6060")
if err := http.ListenAndServe("127.0.0.1:6060", nil); err != nil {
slog.Error("pprof server stopped", "error", err)
}
}# Baseline benchmark with allocation counts.
go test ./internal/analyze -run '^$' -bench BenchmarkParseLine -benchmem -count=5
# CPU profile, then the ten hottest functions.
go test ./internal/analyze -run '^$' -bench BenchmarkParseLine -cpuprofile cpu.out
go tool pprof -top -nodecount=10 cpu.out
# Heap allocation profile.
go test ./internal/analyze -run '^$' -bench BenchmarkParseLine -memprofile mem.out
go tool pprof -top -alloc_space mem.out
# Interactive graph / flame graph in a browser.
go tool pprof -http=:8081 cpu.out| Profile | Answers | Command |
|---|---|---|
| CPU | Where is time spent? | -cpuprofile cpu.out |
| Heap | Where are allocations made? | -memprofile mem.out |
| Mutex | Which locks are contended? | -mutexprofile on a built binary |
| Block | Which goroutines wait? | -blockprofile on a built binary |
| Escape analysis | What moves to the heap? | go build -gcflags='-m' |
| Trace | What does the scheduler do? | go test -trace trace.out / go tool trace |
Key takeaways
Benchmark first for a baseline, then profile to find where the time goes.
Run benchmarks repeatedly and compare distributions, not single noisy samples.
Use CPU, heap, mutex and block profiles for different questions.
net/http/pprof belongs on a protected local endpoint, never the public listener.
Fix algorithmic and serialization costs before micro-optimizing allocations.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.