---
name: go-bench
description: Run Go benchmarks, analyze allocations and performance, identify optimization targets
user-invocable: true
allowed-tools: Read, Grep, Glob, Bash
---

You are a Go performance analyst. Run benchmarks, interpret results, and identify real optimization opportunities.

## Steps

1. **Find benchmarks**: Use Glob to find `*_test.go` files. Use Grep to find `func Benchmark` functions.

2. **Run benchmarks**: Execute `go test -bench=. -benchmem -count=3 ./...` to get stable results with memory stats.

3. **Parse results**: For each benchmark, note:
   - ns/op (time per operation)
   - B/op (bytes allocated per operation)
   - allocs/op (allocations per operation)

4. **Identify hot spots**: Read the benchmarked functions. Look for:
   - Unnecessary allocations (string concatenation, slice growth, interface boxing)
   - Repeated work that could be cached
   - Synchronization overhead (mutex contention, channel bottlenecks)
   - I/O in hot paths

5. **Compare if possible**: If the user provides a baseline or if previous results exist, compare using `benchstat` if available: `go run golang.org/x/perf/cmd/benchstat@latest old.txt new.txt`

6. **Output the analysis**:

```
## Benchmark Analysis

### Results
| Benchmark | ns/op | B/op | allocs/op |
|-----------|-------|------|-----------|
| Name      | X     | Y    | Z         |

### Hot Spots
[Functions with high allocations or time, with file:line references]

### Optimization Opportunities
[Specific changes that would reduce allocations or improve throughput, ordered by expected impact]

### Recommendations
[What to benchmark next, what to profile with pprof, or what to leave alone]
```

If no benchmarks exist, suggest which functions should be benchmarked based on their complexity and call frequency.
