What this thread does
Do first: capture a 30-second CPU profile during the slow workload with go tool pprof 'http://127.0.0.1:6060/debug/pprof/profile?seconds=30', then choose application CPU, allocation or waiting below.
As of November 2026, this recipe targets Go 1.27.3; reference date: 2026-11-08. Confirm the deployed binary's compiler with go version -m ./service, replacing ./service with its actual path (official command guidance).
If no diagnostic listener exists, add this to the service: imports log, net/http and net/http/pprof, then call startProfiles() once during startup. Keep the public server on its own explicit application mux. The dedicated mux below exposes only the listed profiles (Go 1.27.3 HTTP profiler).
func startProfiles() {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
go func() {
log.Print(http.ListenAndServe("127.0.0.1:6060", mux))
}()
}
Run the command from the service's network namespace, or through an authenticated tunnel to this loopback listener. Replay representative traffic while profiling. In the interactive prompt use top and top -cum: flat values identify work in a function itself; cumulative values include callees (official profiling walkthrough). Compare CPU use and request latency over the same window. CPU samples describe running code, so a quiet CPU profile alone cannot explain a long request (official diagnostics guide).
1. Application functions dominate CPU
As of November 2026, Go 1.27.3: follow the command reference and pprof source-listing documentation. At the prompt, use list YourHotFunction, replacing the name with a function from the report. Keep the matching source and binary available. Trace a high cumulative caller down to the costly line rather than rewriting the whole caller.
If that line recomputes a request-invariant value inside a loop, move that calculation outside the loop while preserving error handling. Repeat the same traffic window: compare CPU seconds per completed request and latency, then compare response contents. A smaller percentage alone is insufficient if total CPU or throughput changed.
2. Allocation or GC work dominates CPU
As of November 2026, Go 1.27.3: use the runtime profile definitions and HTTP delta-profile parameters.
go tool pprof -alloc_space 'http://127.0.0.1:6060/debug/pprof/allocs?seconds=30'
Follow the largest application allocation stack with list YourAllocatingFunction. This window measures allocated bytes, including objects subsequently collected; it is different from live heap. If the stack repeatedly grows a result slice and the bounded result count is known, initialize its capacity from that count before appending. Keep the public result equivalent.
Repeat a window with the same completed request count. Look for fewer allocated bytes per request and less allocation/GC CPU in the CPU profile, without worse latency. Do not compare an entire process lifetime against a 30-second window.
3. CPU is underused and requests queue behind a shared lock
As of November 2026, Go 1.27.3: enable runtime.SetMutexProfileFraction(1) temporarily in a controlled reproduction, importing runtime; restore the previous rate afterward. The runtime setting returns that previous rate. Then collect:
go tool pprof 'http://127.0.0.1:6060/debug/pprof/mutex?seconds=30'
Use the mutex profile interpretation: the stack identifies the end of the critical section, often an unlock, and values approximate cumulative waiter time. Inspect the protected section. If it performs remote I/O, move the I/O outside the lock only after defining how to preserve consistency and handle concurrent changes.
Repeat at the same concurrency and request mix. Compare contention delay per request, throughput and tail latency; exercise concurrent updates for correctness. Profiling every contention event adds overhead, so remove that setting before the final ordinary-load comparison.
Evidence and limits
The release history dates Go 1.27.3 to 2026-10-19. Public report: func25's issue #73760, 2025-05-18, uses Go 1.23.9 and reports inaccurate attribution among lock holders. It is historical evidence for interpreting contention cautiously, not proof of the same defect in Go 1.27.3. The current API already describes these values as approximate.
Profiles are sampled, aggregate concurrent work and can perturb timing. If CPU and contention are both small, inspect the goroutine profile with go tool pprof 'http://127.0.0.1:6060/debug/pprof/goroutine' and inspect downstream request timings; waiting for a database or network does not become application CPU work. Pprof allocation stacks locate allocations, not the reference that keeps an object alive. Keep profiles and their listener private; remove temporary instrumentation afterward.
Not for: Node.js CPU profiling; see node-app-slow-find-cpu-hot-function-cpu-prof-cpuprofile
Started from a post by @func25, packaged by the editor. Original post