go-benchmark
Performance benchmarking and optimization workflow for Go, focusing on measured improvements.
Install
mkdir -p .claude/skills/go-benchmark && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/17176" && unzip -o skill.zip -d .claude/skills/go-benchmark && rm skill.zipInstalls to .claude/skills/go-benchmark
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Use when writing Go benchmarks or when asked to measure/optimize/improve the performance of a function — establishes correct benchmarks, measures with -benchmem and pprof, optimizes hot paths, and proves the gain (and no regression) with benchstat.Key capabilities
- →Write correct Go benchmarks using `b.Loop()` and `b.ReportAllocs()`
- →Measure performance with `go test -bench` and `pprof`
- →Optimize hot paths by reducing allocations and avoiding reflection
- →Prove performance gains with `benchstat` before and after changes
- →Prevent dead-code elimination in benchmarks using a package-level sink
- →Use table-driven sub-benchmarks for input sizes
How it works
The skill guides the process of Go benchmarking by establishing correct benchmarks, measuring performance with `pprof`, optimizing hot paths, and proving improvements with `benchstat`. It emphasizes measuring before optimizing.
Inputs & outputs
When to use go-benchmark
- →Optimize performance in hot paths
- →Benchmark Go functions
- →Validate code improvements
About this skill
Go Benchmarking & Performance
Goal: only optimize what's measured. Write a correct benchmark, measure, optimize, and
prove the improvement with benchstat before/after.
When to benchmark (the right targets)
Benchmark a function when: it's on a hot path (called in tight loops — e.g. the candidate-search and image-distance core of the port), it allocates heavily, or someone claims it's "slow". Do NOT micro-optimize cold code — clarity wins there (see go-style-guide).
Writing a correct benchmark (Go 1.24+)
func BenchmarkDistance(b *testing.B) {
img := loadFixture() // setup OUTSIDE the loop
b.ReportAllocs() // always report allocs
b.ResetTimer() // if setup was non-trivial
for b.Loop() { // Go 1.24+: not `for i := 0; i < b.N; i++`
sink = distance(img) // assign to a package-level sink…
}
}
var sink int // …to stop the compiler eliminating the call
b.Loop()(Go 1.24+) — keeps setup/cleanup out of the timed region automatically.b.ReportAllocs()— allocs/op is usually the real lever.- Prevent dead-code elimination: store results in a package-level
sink. - Table-driven sub-benchmarks with
b.Run(name, …)for input sizes. b.RunParallelfor contention;b.SetBytes(n)for throughput (MB/s).
Measure
mise run bench # go test -bench -benchmem ./...
go test -bench=Distance -benchmem -cpuprofile cpu.prof -memprofile mem.prof ./internal/...
go tool pprof -top cpu.prof # find the hot frames / allocations
Optimize (typical levers, cheapest first)
- Reduce allocations: preallocate slices/maps with known size, reuse buffers
(
sync.Pool,bytes.Buffer), avoid[]byte↔stringcopies (strings.Builder). - Avoid reflection / interface boxing on hot paths; use generics or concrete types.
- Hoist work out of loops; precompute; use
slices/mapshelpers. - Algorithmic improvement (better complexity) beats micro-tuning — escalate to
algo-architectif the win is algorithmic.
Prove the gain (mandatory)
mise run bench:baseline # save baseline BEFORE changing code
# …optimize…
mise run bench:compare # benchstat baseline vs new → shows %delta + significance
Keep a change only if benchstat shows a statistically significant improvement (and no
regression in allocs or other benchmarks). Commit the benchmark alongside the optimization.
Notes
- Benchmark artifacts (
bench*.txt,*.prof) are gitignored and cleaned byrepo-janitor. - Don't sacrifice clarity for a tiny gain — measure first, and only optimize where it matters.
When not to use it
- →When micro-optimizing cold code
- →When sacrificing clarity for tiny performance gains
- →When the win is algorithmic and requires `algo-architect`
Limitations
- →The skill focuses on Go 1.24+ benchmarking features.
- →The skill requires `benchstat` for proving performance gains.
- →The skill requires `pprof` for finding hot frames and allocations.
How it compares
This workflow standardizes Go benchmarking and optimization by enforcing a measure-optimize-prove cycle with specific tools and techniques, unlike ad-hoc optimization attempts.
Compared to similar skills
go-benchmark side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| go-benchmark (this skill) | 0 | 2mo | Review | Intermediate |
| golang-performance | 4 | 7mo | Review | Advanced |
| common-performance-engineering | 0 | 1mo | No flags | Intermediate |
| golang-pro | 14 | 4mo | No flags | Advanced |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
golang-performance
MadAppGang
Use when profiling Go applications (pprof), running benchmarks, optimizing memory/CPU usage, or debugging performance bottlenecks in production Go code.
common-performance-engineering
HoangNguyen0403
Enforce universal standards for high-performance development. Use when profiling bottlenecks, reducing latency, fixing memory leaks, improving throughput, or optimizing algorithm complexity in any language.
golang-pro
sickn33
Master Go 1.21+ with modern patterns, advanced concurrency, performance optimization, and production-ready microservices. Expert in the latest Go ecosystem including generics, workspaces, and cutting-edge frameworks. Use PROACTIVELY for Go development, architecture design, or performance optimization.
go-concurrency-patterns
wshobson
Master Go concurrency with goroutines, channels, sync primitives, and context. Use when building concurrent Go applications, implementing worker pools, or debugging race conditions.
generating-grpc-services
jeremylongshore
Generate gRPC service definitions, stubs, and implementations from Protocol Buffers. Use when creating high-performance gRPC services. Trigger with phrases like "generate gRPC service", "create gRPC API", or "build gRPC server".
go-patterns
tuyenht
Các mẫu thiết kế Go: concurrency, interfaces, error handling và clean architecture.