/performant-golang:go-perf-methodologyfrom performant-golang
Guides Go performance work as a measure-first discipline AT DEPTH — the deep peer of go-performance. Owns the tool-selection matrix (which question a benchmark answers vs a CPU profile vs a heap profile's inuse/alloc vs the execution tracer vs block/mutex profiles vs GODEBUG=gctrace), Brendan Gregg's USE method mapped to Go resources, statistical rigor (why one run lies, benchstat ≥10 runs / p-value / geomean), micro-vs-macro benchmark design, the order of operations and Amdahl's law. Auto-invokes when planning a Go optimization, choosing a diagnostic, setting a perf budget, or judging whether a change is worth it, and on "is this worth optimizing", "which profiler should I use", "how do I know my optimization worked", "why does my benchmark say X but prod says Y". Routes the high-level loop to go-performance, benchmark mechanics to go-testing-advanced, benchstat details to go-perf-benchmarking-statistics.
Ships in performant-golang. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
"You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is." — Rob Pike, Notes on Programming in C
The high-level loop — profile or benchmark → change one thing → measure again — belongs to go-performance; read it first. This skill is its deep peer. It owns the layer beneath the loop: which diagnostic answers which question (and which is blind to which problem), Brendan Gregg's USE method as a saturation discipline, the statistical method that makes an A/B trustworthy, when a microbenchmark lies, the order of operations on a real slow system, reproducible measurement environments, CI regression gating, and tail-latency pitfalls. Everything else routes to a go-perf-* leaf.
All Go below builds clean under gofmt, go vet, and go test on Go 1.26.
The single most expensive methodology error after "optimizing without measuring" is measuring with the wrong tool. The Go diagnostics page partitions diagnostics into four groups — profiling, tracing, debugging, and runtime statistics — and warns they are not interchangeable (Diagnostics). The decision layer:
| Question / symptom | Right diagnostic | Wrong tool (why) | Routes to |
|---|---|---|---|
| "Is this function faster / fewer allocs now?" | benchmark + benchstat, n≥10 | one run (inside the noise) | go-perf-benchmarking-statistics |
| "Where does CPU time go?" | CPU profile (-cpuprofile) | tracer ("not great … for excessive CPU usage" [Diag]) | go-perf-pprof-profiling |
| "What allocates / leaks?" | heap profile — inuse_* (live now) vs alloc_* (cumulative) | CPU profile | go-perf-pprof-profiling |
| "Why slow sometimes / tail spikes / poor parallelism?" | execution tracer + flight recorder | CPU profile (blind to off-CPU waits) | go-perf-execution-tracer |
| "Which lock or channel is contended?" | block / mutex profile (enable first) | CPU profile (off-CPU blind) | go-perf-block-mutex-profiles |
| "Are goroutines piling up / leaking?" | goroutine profile (stack traces of all goroutines) | — | go-perf-block-mutex-profiles |
| "Does this value escape to the heap?" | go build -gcflags=-m | heap profile alone | go-perf-escape-analysis |
| "Is GC overhead the cost?" | GODEBUG=gctrace=1, /gc/* runtime/metrics | guessing at GOGC | go-perf-gc-tuning |
| "Which resource is saturated at all?" | USE method (§3) | jumping to "optimize the function" | this skill |
Two rules carry this table:
block and mutex profiles record nothing until enabled — "not enabled by default" — via runtime.SetBlockProfileRate and runtime.SetMutexProfileFraction (Diagnostics). Asking for a contention profile without setting the rate gets you an empty profile, not "no contention."
A profile is itself a statistical sample with known blind spots, not ground truth. When CPU profiling is on, "the Go program stops about 100 times per second and records a sample consisting of the program counters on the currently executing goroutine's stack" (Profiling Go Programs). The memory profiler "only records information for approximately one block per half megabyte allocated (the '1-in-524288 sampling rate'), so these are approximations" (pprof).
Consequences for how you read a profile:
top shows flat time ("the number of samples in which the function was running, as opposed to waiting for a called function to return"); add -cum to "sort by … cumulative" and find expensive callers up the tree (pprof). Reading flat time when you want cumulative (or vice versa) misattributes the cost.Subcommand depth (list, peek, disasm, -diff_base, labels) → go-perf-pprof-profiling.
Before optimizing a function, ask which resource is the bottleneck. Brendan Gregg's USE method checks three things for every resource: Utilization ("the average time that the resource was busy servicing work"), Saturation ("the degree to which the resource has extra work which it can't service, often queued"), and Errors ("the count of error events"). The central instruction: "For every resource, check utilization, saturation, and errors" — a methodology that "solves about 80% of server issues with 5% of the effort" (Gregg, The USE Method). A resource includes software resources like "mutex locks, thread pools, and process capacity" (Gregg).
The Go translation — each resource to its diagnostic:
| Resource | Utilization | Saturation (the tell) | Go tool |
|---|---|---|---|
| CPU | CPU% / GOMAXPROCS busy | runnable-queue depth | GODEBUG=schedtrace, CPU profile |
| Memory | heap in-use bytes | GC frequency, swap, OOM | runtime/metrics /gc/heap/*, gctrace |
| Scheduler / goroutines | P busy | /sched/latencies:seconds (runnable→running wait) | execution tracer, schedtrace |
| Locks (software resource) | time held | block/mutex profile contention | mutex / block profile |
| Channels / worker pool | workers busy | buffered-queue depth, blocked sends | tracer, block profile |
| GC | GC CPU fraction | mark-assist time | /sched/pauses/total/gc:seconds, gctrace |
The decisive distinction: high utilization is not the problem; saturation is. A CPU pinned at 100% with an empty run queue is healthy; one at 60% with a deep run queue or lock queue is the real bottleneck — exactly the off-CPU case a CPU profile cannot see (§1). USE forces "is it CPU, memory/GC, locks, or the scheduler?" before any code change, so you don't CPU-optimize a memory- or lock-bound service.
go-performance says "use benchstat"; here is why, conceptually (the commands and flags are go-perf-benchmarking-statistics). benchstat "computes statistical summaries and A/B comparisons of Go benchmarks," and "each benchmark should be run at least 10 times to gather a statistically significant sample of results" (benchstat).
Encode/format=json-48 1.718µ ± 1% 1.423µ ± 1% -17.20% (p=0.000 n=10)
Encode/format=gob-48 3.066µ ± 0% 3.070µ ± 2% ~ (p=0.446 n=10)
Five principles below the one-line "use benchstat":
ns/op pair cannot separate a real delta from CPU-frequency, GC, and scheduling noise. The test needs a sample to compute a p-value at all.~ is a result, not a failure. It "means benchstat did not detect a statistically significant difference" (benchstat) — i.e. don't ship the complexity, the change bought nothing measurable.A microbenchmark is a hypothesis generator, not proof of a production win: "Microbenchmarks help but full system testing is best" (go-perfbook). The ways one misleads:
GOMAXPROCS cores under load.go-testing-advanced; the methodology warning lives here).Confirm a microbenchmark win with a macro/system benchmark or a production profile before claiming it: "some issues not apparent on your laptop might be visible once deployed" at "250k reqs/second on a 40 core server" (go-perfbook).
Optimization is "refactoring … [that] improves performance … generally … at the cost of readability" (go-perfbook) — so the bar to start is a measured goal you're missing, and the order is fixed (go-perfbook):
~ means it didn't work.Spend effort top-down: algorithm/data structure first, Go-specific implementation tweaks second, unsafe/assembly last. "Data dominates. If you've chosen the right data structures … the algorithms will almost always be self-evident" (Pike). Algorithmic wins dwarf micro-tuning — go-perfbook cites a ~30,000× algorithmic gain (1991–2008) against compilers merely doubling every 18 years (Proebsting's Law) (go-perfbook).
Amdahl's law is the cold-path guard. "Doubling speed in a routine consuming 5% of runtime yields only 2.5% total improvement. Focus on bottlenecks consuming 80%+ of runtime" (go-perfbook). A 50% speedup on a path that runs 0.1% of the time is noise you paid clarity for. The counterweight to Knuth's "premature optimization" is premature pessimization — don't "write code that is slower than it needs to be … when equivalently complex code would be faster" (go-perfbook); the rule is "don't micro-tune unmeasured," not "never think about performance."
The benchmark machine is itself a noise source; an unpinned machine makes §4's statistics meaningless. The dominant culprit is frequency scaling — turbo boost and thermal throttling make the same code benchmark 20%+ differently run-to-run.
perflock addresses both noise sources: it "is a simple locking wrapper for running benchmarks on shared hosts," acquiring "a system-wide lock while running command" (perflock). Two facts matter:
-governor defaults to 90% — it sets "CPU frequency to percent between the min and max while running command," pinning the clock below turbo so frequency is constant, and restores the governor on release (perflock).Other controls (commands → go-perf-benchmarking-statistics / go-perf-os-tooling): pin the CPU governor to performance and disable turbo; taskset/isolcpus to keep the OS scheduler off the bench cores; fix GOMAXPROCS explicitly — and note Go 1.25 container-aware GOMAXPROCS changes the default under a cgroup CPU limit, so verify the floor when benching in a container (Go 1.25); quiesce the machine; prefer bare metal over a noisy-neighbor VM. Re-baseline after a Go upgrade — Green Tea GC is default in 1.26 and shifts the GC baseline (Go 1.26). Without governor pinning + serialization + n≥10, the delta is inside the noise floor and the U-test has nothing to bite on.
For a latency-sensitive service, report p99/p999, not the mean — and beware the measurement trap. Coordinated omission (Gil Tene, How NOT to Measure Latency): a load generator that waits for each request to finish before sending the next coordinates with the system under test — when the server stalls for 5 s, the generator sends nothing during the stall, recording one bad outlier instead of the hundreds of requests that should have been issued and would each show multi-second latency. The result is "catastrophically optimistic the moment the system under test starts to struggle." The fix is intended-start-time correction (measure latency from when a request should have started): tools wrk2 (constant rate -R + HdrHistogram) and Vegeta do this.
In Go, read /sched/pauses/total/gc:seconds (the GC stop-the-world pause distribution; /gc/pauses:seconds is its deprecated alias) and /sched/latencies:seconds from runtime/metrics. The dominant tail lever is usually lowering the allocation rate (which lowers GC frequency), ahead of any GC knob — Go's STW pauses are sub-millisecond and rarely dominate the tail. Depth → go-perf-tail-latency.
A regression that isn't gated lands silently. Wire a job that runs the suite -count≥10 on the PR vs its base on the same runner, pipes both to benchstat, and fails the build when a tracked benchmark regresses past a threshold (p<0.05 by the U-test, not eyeballing). Principles:
allocs/op is deterministic per code path, far less noisy than ns/op, so an alloc-count budget is the cheapest, most reliable gate — a jump from 1→3 allocs/op is a real regression even when timing is noisy.ns/op thresholds are fragile; compare PR vs base in the same job and treat ~ as "no regression."~ as "no win" (benchstat).GOGC/GOMEMLIMIT before USE confirms GC is the bottleneck — the usual fix is fewer allocations (§3, §8).This plugin (go-perf-* depth):
go-perf-benchmarking-statistics — benchstat flags, p-values, geomean, sample size, perflock how-to, A/B method, CI gating commands.go-perf-pprof-profiling — CPU/heap pprof subcommands, inuse vs alloc, differential profiling, labels, production profiling.go-perf-block-mutex-profiles — block/mutex/goroutine profiles and the enablement gotcha.go-perf-execution-tracer — runtime/trace, go tool trace, the flight recorder (1.25).go-perf-escape-analysis — reading -gcflags=-m; what escapes to the heap.go-perf-gc-tuning — GOGC/GOMEMLIMIT, gctrace, allocation rate as the primary lever.go-perf-tail-latency — p99/p999, coordinated omission, latency histograms.go-perf-os-tooling — Linux perf, flame graphs, perflock as a benchmark stabilizer.Parent / sibling marketplace (go-* correctness):
go-performance — the measure-first overview and the 3-step loop this skill deepens.go-testing-advanced — benchmark mechanics: for b.Loop(), b.ReportAllocs, b.ResetTimer, b.RunParallel, the sink/DCE trap.go-idiomatic-discipline — the policy root; unmeasured micro-opt vs clarity is its axis-2 ("clear AND correct") failure.go-version-feature-map — version floors: container-aware GOMAXPROCS 1.25, Green Tea GC default 1.26.Methodology anti-patterns in LLM-generated Go performance work, each with wrong/right contrast and citations:
Source provenance for every claim in this skill:
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub ctoth/golang-skills-plugin --plugin performant-golang34 skills in this plugin
/go-perf-allocator-internalsWhat a Go heap allocation costs — the ~70 size classes and rounding waste (unsafe.Sizeof vs real slot), the tiny allocator packing sub-16B pointer-free objects, the lock-free per-P mcache → mcentral → mheap hierarchy, and scannable vs noscan spans (pointer-free objects cut GC cost; "1 alloc/op" hides different costs). Fires on "still slow after removing allocs", "reduce GC pressure", "size class", "how does Go allocate". Routes to go-perf-escape-analysis, go-perf-sync-pool, go-perf-gc-tuning, go-perf-struct-layout./go-perf-atomics-vs-locksGuides the atomics-vs-locks performance tradeoff in Go — typed sync/atomic ops (atomic.Int64/Pointer/Bool, 1.19) are a single CAS-style instruction, cheaper than a mutex uncontended but still cache-line-bouncing when contended; sync.Mutex's fast path is itself a CAS that only parks the goroutine when contended; RWMutex wins only for long read sections and is slower (scales worse) than a plain Mutex for short ones; the atomic.Pointer copy-on-write config swap; when lock-free isn't worth the correctness risk. Fires on "atomic vs mutex", "is RWMutex faster", "lock-free counter", "reduce locking overhead", "atomic.Pointer config". Routes copylock/atomic-API correctness to go-sync-primitives, memory ordering to go-race-and-memory-model, measuring to go-perf-block-mutex-profiles.Other plugins' skills for the same job