Stage 3 · Build
Performance, Memory & Debugging
Production Benchmarking
benchstat, realistic fixtures, shadow traffic, and safe profiling in live services.
benchstat
benchstat compares benchmark results and reports statistical significance. It tells you whether a change is a real improvement or just noise.
# Install benchstat
go install golang.org/x/perf/cmd/benchstat@latest
# Save baseline
go test -bench=. -benchmem -count=10 > old.txt
# Make changes, save new results
go test -bench=. -benchmem -count=10 > new.txt
# Compare
benchstat old.txt new.txt
# Output:
# name old time/op new time/op delta
# Parse-8 1.23µs ± 2% 0.98µs ± 1% -20.33% (p=0.000 n=10+10)
# Marshal-8 2.45µs ± 3% 2.41µs ± 2% ~ (p=0.142 n=10+10)
# name old alloc/op new alloc/op delta
# Parse-8 256B ± 0% 128B ± 0% -50.00% (p=0.000 n=10+10)
# Marshal-8 512B ± 0% 512B ± 0% ~ (p=1.000 n=10+10)
# Filter by name
benchstat -filter 'Parse' old.txt new.txt
# HTML output
benchstat -html old.txt new.txt > report.htmlbenchstat requires multiple runs (count=10) for statistical analysis. The p-value indicates whether the change is significant. p < 0.05 means statistically significant. delta shows the percentage change.
Realistic Fixtures
Benchmarks with synthetic data do not reflect real performance. Use realistic fixtures — data that matches production characteristics in size, complexity, and distribution.
Benchmark Metrics
Shadow Traffic
Shadow traffic replays real production requests against a new version. It measures real-world performance without affecting users.
Safe Profiling in Production
Regression Detection
# GitHub Actions benchmark comparison
name: Benchmark
on:
pull_request:
paths:
- '**/*.go'
jobs:
benchmark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22"
- name: Save baseline
run: |
git checkout main
go test -bench=. -benchmem -count=5 > old.txt
- name: Save PR results
run: |
git checkout -
go test -bench=. -benchmem -count=5 > new.txt
- name: Compare
run: |
go install golang.org/x/perf/cmd/benchstat@latest
benchstat old.txt new.txt
- name: Fail on regression
run: |
benchstat -delta 1% old.txt new.txt | grep -q "regression" && exit 1 || trueCI benchmark comparison catches performance regressions before they reach production. Save baseline from main, compare with PR results, and fail on significant regressions. The delta threshold controls sensitivity.
Run benchmarks on every PR. Compare with the baseline. Fail on regressions. This creates a culture of performance awareness. Small regressions accumulate — catch them early when they are easy to fix.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.