Performance Benchmarking
Run Go benchmarks to measure and compare performance. Flags regressions exceeding 10%.
Usage
/benchmark- Run benchmarks on all packages/benchmark ./internal/index/...- Run benchmarks on specific package/benchmark -compare ./...- Compare against saved baseline
Steps
- Identify benchmark targets
- If
$ARGUMENTSspecifies a package, use that - Otherwise, find packages with
*_test.gofiles containingBenchmarkfunctions - Focus on performance-critical packages:
internal/index/,internal/repo/,internal/api/
- Run benchmarks
```bash
go test -bench=. -benchmem -count=5 $ARGUMENTS 2>&1
```
- Use
-count=5for statistical reliability - Use
-benchmemto track allocations
- Parse and analyze results
For each benchmark, extract:
ns/op- Nanoseconds per operationB/op- Bytes allocated per operationallocs/op- Allocations per operation
- Check for baseline comparison
- Look for
.benchmark-baselinefile in project root - If
-compareflag used and baseline exists, compare results
- Report findings
Format results as a table:
```
| Benchmark | ns/op | B/op | allocs/op | Change |
|-----------|-------|------|-----------|--------|
```
Flag any metric that regressed more than 10% with a warning.
- Offer to save baseline
If no baseline exists or results improved, offer to save current results:
```bash
go test -bench=. -benchmem -count=5 ./... > .benchmark-baseline
```
Performance-Critical Packages
Pay special attention to:
internal/index/- Trigram search (hot path)internal/repo/- Repository access patternsinternal/api/search/- Search request handlinginternal/storage/- S3 result compression
Example Output
Benchmark Results for ./internal/index/...
| Benchmark | ns/op | B/op | allocs/op |
|---------------------|----------|-------|-----------|
| BenchmarkSearch-8 | 1234567 | 4096 | 12 |
| BenchmarkOpen-8 | 567890 | 8192 | 24 |
No regressions detected (threshold: 10%)