Performance
Purpose
Turn "it feels slow" into a measured, fixed, and regression-guarded engineering task.
The skill fixes the methodology (profile before optimizing), the toolchain per
language, the benchmark hygiene rules, and the latency-analysis workflow for p99/p999
work.
When to Use / When NOT to Use
Use when:
- A service or function is slow and the cause is unknown ("this is slow", "CPU is
pegged", "p99 regressed").
- A hot path needs optimization with proof ("optimize this loop", "cut
allocations").
- Adding a performance regression gate for a critical component.
Do NOT use when:
- The problem is missing observability (you cannot see what is slow) — set up
observability first; profiling needs a target.
- The slowness is a capacity/planning issue (traffic doubled, needs scaling) —
that is architecture and SRE planning, not code optimization.
Prerequisites & Assumptions
- Go: built-in
testing, pprof, benchstat; Java: JFR + async-profiler,
JMH for benchmarks; Python: cProfile/py-spy, pytest-benchmark, timeit.
- Benchmarks run on quiet machines/CI runners with controlled resources;
comparisons use statistical tooling (
benchstat), never single runs.
- Representative workload or trace exists to profile against.
Step-by-Step Workflow
- Measure first — capture a baseline: CPU profile, memory profile, and
allocation profile (Go) or flame graph (Java/Python) under realistic load.
No baseline, no optimization.
- Localize — identify the top cost centers; state the expected win of fixing
each ("froto — 30% of CPU in JSON marshaling").
- Hypothesize and fix one thing — one optimization per change: pre-allocation,
buffer reuse, algorithm change, batched I/O. Keep it behavior-preserving
(
code-quality/refactoring).
- Re-measure — rerun the same benchmark/profile;
benchstat the before/after
with enough iterations (≥10 runs); report the delta with variance.
- Guard the win — keep the benchmark in the repo (
testing/unit-testing
benchmark hooks) and add it to the CI comparison for critical paths.
- Document — record what was tried and rejected (ADR if architectural; the
change log line otherwise).
Enterprise Rules & Standards (MUST / MUST NOT)
- MUST profile before optimizing; unmeasured "optimizations" are rejected in
review regardless of plausibility.
- MUST compare benchmarks statistically (benchstat or ≥10-run medians with
variance), never single-run numbers.
- MUST keep benchmarks representative: real data shapes and sizes, not
microbenchmarks of synthetic strings.
- MUST verify behavior is unchanged after optimization (tests green,
outputs identical).
- SHOULD target allocations first in Go hot paths (escape analysis,
pre-sized slices/maps,
sync.Pool for reusable buffers with measured wins).
- SHOULD measure tail latency (p99/p999), not just the mean; means hide
queueing and GC pauses.
- MUST NOT optimize without a regression guard — an unmeasured win
disappears with the next refactor.
- MUST NOT trade correctness or security for speed (unvalidated caching,
disabling TLS) without an explicit, documented decision.
Examples
Good — benchmark with representative input (Go, compiles):
var orderJSON = []byte(`{"id":"o-123","total":4200,"items":[{"sku":"A-1","qty":2}]}`)
func BenchmarkParseOrder(b *testing.B) {
b.ReportAllocs()
for b.Loop() { // Go 1.24+: b.Loop replaces the classic range loop
if _, err := ParseOrder(orderJSON); err != nil {
b.Fatal(err)
}
}
}
go test -bench=ParseOrder -benchmem -count=10 ./... | benchstat - old.txt new.txt
Bad — the single-run microbenchmark "proving" the rewrite:
Ran the loop once on my laptop while a build was running: 1.2ms vs 1.5ms,
rewrote everything to unsafe pointer tricks.
Why it fails: one noisy run, unrepresentative input, and a correctness hazard —
none of which would survive a benchstat comparison.
Checklist
Common Pitfalls
- Premature micro-optimization — rewriting clean code for imagined wins; the
profile usually points elsewhere.
- Benchmarker's bias — optimizing what is easy to benchmark, not what is slow
in production; profile with real traces.
- GC whack-a-mole (Go) — "fixing" allocations with
sync.Pool everywhere
before checking the GC pause share of the profile.
- Ignoring the tails — optimizing median latency while p99 (the pager) is
dominated by lock contention.
References
1---2name: performance3description: 'Profiles and optimizes system performance — CPU/memory profiling, benchmark methodology, allocation analysis, and p99 latency work with measurement-first discipline. Trigger with "this is slow", "profile this service", "optimize this hot path", or "benchmark before merging".'4metadata:5 source: https://skillmd.com/skills/pnguyen215/performance6---78# Performance910## Purpose1112Turn "it feels slow" into a measured, fixed, and regression-guarded engineering task.13The skill fixes the methodology (profile before optimizing), the toolchain per14language, the benchmark hygiene rules, and the latency-analysis workflow for p99/p99915work.1617## When to Use / When NOT to Use1819Use when:2021- A service or function is slow and the cause is unknown ("this is slow", "CPU is22 pegged", "p99 regressed").23- A hot path needs optimization with proof ("optimize this loop", "cut24 allocations").25- Adding a performance regression gate for a critical component.2627Do NOT use when:2829- The problem is missing observability (you cannot see what is slow) — set up30 `observability` first; profiling needs a target.31- The slowness is a capacity/planning issue (traffic doubled, needs scaling) —32 that is architecture and SRE planning, not code optimization.3334## Prerequisites & Assumptions3536- Go: built-in `testing`, `pprof`, `benchstat`; Java: JFR + async-profiler,37 JMH for benchmarks; Python: cProfile/py-spy, pytest-benchmark, `timeit`.38- Benchmarks run on quiet machines/CI runners with controlled resources;39 comparisons use statistical tooling (`benchstat`), never single runs.40- Representative workload or trace exists to profile against.4142## Step-by-Step Workflow43441. **Measure first** — capture a baseline: CPU profile, memory profile, and45 allocation profile (Go) or flame graph (Java/Python) under realistic load.46 No baseline, no optimization.472. **Localize** — identify the top cost centers; state the expected win of fixing48 each ("froto — 30% of CPU in JSON marshaling").493. **Hypothesize and fix one thing** — one optimization per change: pre-allocation,50 buffer reuse, algorithm change, batched I/O. Keep it behavior-preserving51 (`code-quality/refactoring`).524. **Re-measure** — rerun the same benchmark/profile; `benchstat` the before/after53 with enough iterations (≥10 runs); report the delta with variance.545. **Guard the win** — keep the benchmark in the repo (`testing/unit-testing`55 benchmark hooks) and add it to the CI comparison for critical paths.566. **Document** — record what was tried and rejected (ADR if architectural; the57 change log line otherwise).5859## Enterprise Rules & Standards (MUST / MUST NOT)6061- **MUST** profile before optimizing; unmeasured "optimizations" are rejected in62 review regardless of plausibility.63- **MUST** compare benchmarks statistically (benchstat or ≥10-run medians with64 variance), never single-run numbers.65- **MUST** keep benchmarks representative: real data shapes and sizes, not66 microbenchmarks of synthetic strings.67- **MUST** verify behavior is unchanged after optimization (tests green,68 outputs identical).69- **SHOULD** target allocations first in Go hot paths (escape analysis,70 pre-sized slices/maps, `sync.Pool` for reusable buffers with measured wins).71- **SHOULD** measure tail latency (p99/p999), not just the mean; means hide72 queueing and GC pauses.73- **MUST NOT** optimize without a regression guard — an unmeasured win74 disappears with the next refactor.75- **MUST NOT** trade correctness or security for speed (unvalidated caching,76 disabling TLS) without an explicit, documented decision.7778## Examples7980Good — benchmark with representative input (Go, compiles):8182```go83var orderJSON = []byte(`{"id":"o-123","total":4200,"items":[{"sku":"A-1","qty":2}]}`)8485func BenchmarkParseOrder(b *testing.B) {86 b.ReportAllocs()87 for b.Loop() { // Go 1.24+: b.Loop replaces the classic range loop88 if _, err := ParseOrder(orderJSON); err != nil {89 b.Fatal(err)90 }91 }92}93```9495```sh96go test -bench=ParseOrder -benchmem -count=10 ./... | benchstat - old.txt new.txt97```9899Bad — the single-run microbenchmark "proving" the rewrite:100101```text102Ran the loop once on my laptop while a build was running: 1.2ms vs 1.5ms,103rewrote everything to unsafe pointer tricks.104```105106Why it fails: one noisy run, unrepresentative input, and a correctness hazard —107none of which would survive a benchstat comparison.108109## Checklist110111- [ ] Baseline profile captured and attached to the task.112- [ ] Top cost centers named with expected win percentages.113- [ ] One optimization per change; tests green after each.114- [ ] Before/after compared with `benchstat` (or ≥10-run medians).115- [ ] Benchmark committed and wired into CI comparison for critical paths.116- [ ] No security or correctness trade-offs without a documented decision.117118## Common Pitfalls119120- **Premature micro-optimization** — rewriting clean code for imagined wins; the121 profile usually points elsewhere.122- **Benchmarker's bias** — optimizing what is easy to benchmark, not what is slow123 in production; profile with real traces.124- **GC whack-a-mole (Go)** — "fixing" allocations with `sync.Pool` everywhere125 before checking the GC pause share of the profile.126- **Ignoring the tails** — optimizing median latency while p99 (the pager) is127 dominated by lock contention.128129## References130131- Go pprof documentation: https://pkg.go.dev/net/http/pprof132- benchstat: https://pkg.go.dev/golang.org/x/perf/cmd/benchstat133- JMH (Java Microbenchmark Harness): https://github.com/openjdk/jmh134- Python profiling: https://docs.python.org/3/library/profile.html