Loading...
Loading...
Golang benchmarking, profiling, and performance measurement. Use when writing, running, or comparing Go benchmarks, profiling hot paths with pprof, interpreting CPU/memory/trace profiles, analyzing results with benchstat, setting up CI benchmark regression detection, or investigating production performance with Prometheus runtime metrics. Also use when the developer needs deep analysis on a specific performance indicator - this skill provides the measurement methodology, while golang-performance provides the optimization patterns.
npx skill4agent add samber/cc-skills-golang golang-benchmarkultrathinksamber/cc-skills-golang@golang-performancesamber/cc-skills-golang@golang-troubleshootingb.Loop()b.Loop()func BenchmarkParse(b *testing.B) {
data := loadFixture("large.json") // setup — excluded from timing
for b.Loop() {
Parse(data) // compiler cannot eliminate this call
}
}for range b.Nb.Loop()b.ResetTimer()func BenchmarkAlloc(b *testing.B) {
b.ReportAllocs() // or run with -benchmem flag
for b.Loop() {
_ = make([]byte, 1024)
}
}b.ReportMetric()b.ReportMetric(float64(totalBytes)/b.Elapsed().Seconds(), "bytes/s")func BenchmarkEncode(b *testing.B) {
for _, size := range []int{64, 256, 4096} {
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
data := make([]byte, size)
for b.Loop() {
Encode(data)
}
})
}
}go test -bench=BenchmarkEncode -benchmem -count=10 ./pkg/... | tee bench.txt| Flag | Purpose |
|---|---|
| Run all benchmarks (regexp filter) |
| Report allocations (B/op, allocs/op) |
| Run 10 times for statistical significance |
| Minimum time per benchmark (default 1s) |
| Run with different GOMAXPROCS values |
| Write CPU profile |
| Write memory profile |
| Write execution trace |
BenchmarkEncode/size=64-8 5000000 230.5 ns/op 128 B/op 2 allocs/op-8ns/opB/opallocs/op# CPU profile
go test -bench=BenchmarkParse -cpuprofile=cpu.prof ./pkg/parser
go tool pprof cpu.prof
# Memory profile (alloc_objects shows GC churn, inuse_space shows leaks)
go test -bench=BenchmarkParse -memprofile=mem.prof ./pkg/parser
go tool pprof -alloc_objects mem.prof
# Execution trace
go test -bench=BenchmarkParse -trace=trace.out ./pkg/parser
go tool trace trace.outprometheus/client_golangruntime/metrics/metricssamber/cc-skills-golang@golang-performancesamber/cc-skills-golang@golang-troubleshootingsamber/cc-skills-golang@golang-observabilitysamber/cc-skills-golang@golang-testing