go-performance

Measure and optimize Go performance using benchmarks, pprof profiles, and benchstat comparisons.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-performance-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-performance
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-performance
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-performance-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers often optimize code based on intuition, which is wrong most of the time. This Skill enforces a measure-first discipline: define a metric, write an isolated benchmark, capture a baseline, diagnose with pprof, apply one optimization at a time, and verify gains with statistical rigor before committing. ## Core Features & Use Cases - Benchmark authoring and execution: Write atomic benchmarks with b.Loop(), sub-benchmarks for input sizes, and -benchmem -count=10 runs that record time, bytes, and allocations per operation. - Profile-driven diagnosis: Match pprof signals (alloc_objects, CPU, goroutine, block profiles) to bottleneck types and route to targeted fixes for memory, CPU, I/O, runtime, or caching issues. - Statistical comparison and review: Use benchstat to prove improvements are significant (rejecting ~ results), document numbers in perf(scope): commits, and detect regressions in CI with benchdiff, cob, or gobenchdata. - Use Case: A service's JSON parsing endpoint is slow. Benchmark the parser, capture a CPU and memory profile, discover reflection-driven allocations, switch to a code-generated marshaler, and confirm a 40% reduction with benchstat before merging. ## Quick Start Ask the AI to benchmark a specific Go function, profile it with pprof, and recommend one measured optimization with benchstat verification.

Frequently Asked Questions about go-performance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write a benchmark in Go?

Write a function named BenchmarkX taking *testing.B and loop with b.Loop() (Go 1.24+), which prevents dead-code elimination and excludes setup from timing. Run it with go test -bench=BenchmarkX -benchmem -count=10 to record ns/op, B/op, and allocs/op.

How do I compare Go benchmark results before and after a change?

Run benchmarks with -count=10 into old.txt and new.txt, then run benchstat old.txt new.txt. A result is a real improvement only when the p-value is below 0.05; a ~ symbol means no statistically significant difference and the gain cannot be claimed.

How do I find where my Go program spends CPU time?

Capture a CPU profile with go test -bench=X -cpuprofile=cpu.prof or from a running service via pprof, then analyze it with go tool pprof. Functions dominating the profile are the hot paths to optimize first.

Why is my Go HTTP client slow under concurrency?

The default http.Transport sets MaxIdleConnsPerHost to 2, so requests queue waiting for connections. Configure a custom Transport with higher MaxIdleConnsPerHost and MaxConnsPerHost, and always drain response bodies so connections return to the pool.

When should I not optimize Go code?

Do not optimize without a profile showing a measured bottleneck, since intuition about hot spots is wrong most of the time. Also rule out external causes first: if most latency is a slow database or API call, reducing allocations will not help.

How do I detect Go performance regressions in CI?

Use benchdiff for benchstat-based PR comparisons, cob for threshold-based gating, or gobenchdata for long-term trend dashboards. Run with -count=10 or more and use conservative thresholds because shared CI runners show 5-10% variance.