Skip to content

Commit 8001cfe

Browse files
committed
Merge branch 'develop'
2 parents c3ba0a3 + 90efe21 commit 8001cfe

245 files changed

Lines changed: 44038 additions & 710 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.claude/skills/LICENSE

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
The skills in this directory were originally imported from:
2+
3+
https://github.com/samber/cc-skills-golang
4+
5+
They are provided under the MIT License, reproduced below. Local
6+
modifications within this repository are also released under the MIT
7+
License. The copyright notice below must be preserved in copies or
8+
substantial portions of these skills.
9+
10+
----------------------------------------------------------------------
11+
12+
MIT License
13+
14+
Copyright (c) 2026 Samuel Berthe
15+
16+
Permission is hereby granted, free of charge, to any person obtaining a copy
17+
of this software and associated documentation files (the "Software"), to deal
18+
in the Software without restriction, including without limitation the rights
19+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+
copies of the Software, and to permit persons to whom the Software is
21+
furnished to do so, subject to the following conditions:
22+
23+
The above copyright notice and this permission notice shall be included in all
24+
copies or substantial portions of the Software.
25+
26+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
SOFTWARE.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
name: golang-benchmark
3+
description: "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."
4+
user-invocable: true
5+
license: MIT
6+
compatibility: Designed for Claude Code or similar AI coding agents, and for projects using Golang.
7+
metadata:
8+
author: samber
9+
version: "1.1.3"
10+
openclaw:
11+
emoji: "📊"
12+
homepage: https://github.com/samber/cc-skills-golang
13+
requires:
14+
bins:
15+
- go
16+
- benchstat
17+
install:
18+
- kind: go
19+
package: golang.org/x/perf/cmd/benchstat@latest
20+
bins: [benchstat]
21+
allowed-tools: Read Edit Write Glob Grep Bash(go:*) Bash(golangci-lint:*) Bash(git:*) Agent WebFetch Bash(benchstat:*) Bash(benchdiff:*) Bash(cob:*) Bash(gobenchdata:*) Bash(curl:*) mcp__context7__resolve-library-id mcp__context7__query-docs WebSearch AskUserQuestion
22+
---
23+
24+
**Persona:** You are a Go performance measurement engineer. You never draw conclusions from a single benchmark run — statistical rigor and controlled conditions are prerequisites before any optimization decision.
25+
26+
**Thinking mode:** Use `ultrathink` for benchmark analysis, profile interpretation, and performance comparison tasks. Deep reasoning prevents misinterpreting profiling data and ensures statistically sound conclusions.
27+
28+
# Go Benchmarking & Performance Measurement
29+
30+
Performance improvement does not exist without measures — if you can measure it, you can improve it.
31+
32+
This skill covers the full measurement workflow: write a benchmark, run it, profile the result, compare before/after with statistical rigor, and track regressions in CI. For optimization patterns to apply after measurement, → See `samber/cc-skills-golang@golang-performance` skill. For pprof setup on running services, → See `samber/cc-skills-golang@golang-troubleshooting` skill.
33+
34+
## Writing Benchmarks
35+
36+
### `b.Loop()` (Go 1.24+) — preferred
37+
38+
`b.Loop()` prevents the compiler from optimizing away the code under test — without it, the compiler can detect dead results and eliminate them, producing misleadingly fast numbers. It also excludes setup code before the loop from timing automatically.
39+
40+
```go
41+
func BenchmarkParse(b *testing.B) {
42+
data := loadFixture("large.json") // setup — excluded from timing
43+
for b.Loop() {
44+
Parse(data) // compiler cannot eliminate this call
45+
}
46+
}
47+
```
48+
49+
Existing `for range b.N` benchmarks still work but should migrate to `b.Loop()` — the old pattern requires manual `b.ResetTimer()` and a package-level sink variable to prevent dead code elimination.
50+
51+
### Memory tracking
52+
53+
```go
54+
func BenchmarkAlloc(b *testing.B) {
55+
b.ReportAllocs() // or run with -benchmem flag
56+
for b.Loop() {
57+
_ = make([]byte, 1024)
58+
}
59+
}
60+
```
61+
62+
`b.ReportMetric()` adds custom metrics (e.g., throughput):
63+
64+
```go
65+
b.ReportMetric(float64(totalBytes)/b.Elapsed().Seconds(), "bytes/s")
66+
```
67+
68+
### Sub-benchmarks and table-driven
69+
70+
```go
71+
func BenchmarkEncode(b *testing.B) {
72+
for _, size := range []int{64, 256, 4096} {
73+
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
74+
data := make([]byte, size)
75+
for b.Loop() {
76+
Encode(data)
77+
}
78+
})
79+
}
80+
}
81+
```
82+
83+
## Running Benchmarks
84+
85+
```bash
86+
go test -bench=BenchmarkEncode -benchmem -count=10 ./pkg/... | tee bench.txt
87+
```
88+
89+
| Flag | Purpose |
90+
| ---------------------- | ----------------------------------------- |
91+
| `-bench=.` | Run all benchmarks (regexp filter) |
92+
| `-benchmem` | Report allocations (B/op, allocs/op) |
93+
| `-count=10` | Run 10 times for statistical significance |
94+
| `-benchtime=3s` | Minimum time per benchmark (default 1s) |
95+
| `-cpu=1,2,4` | Run with different GOMAXPROCS values |
96+
| `-cpuprofile=cpu.prof` | Write CPU profile |
97+
| `-memprofile=mem.prof` | Write memory profile |
98+
| `-trace=trace.out` | Write execution trace |
99+
100+
**Output format:** `BenchmarkEncode/size=64-8 5000000 230.5 ns/op 128 B/op 2 allocs/op` — the `-8` suffix is GOMAXPROCS, `ns/op` is time per operation, `B/op` is bytes allocated per op, `allocs/op` is heap allocation count per op.
101+
102+
## Profiling from Benchmarks
103+
104+
Generate profiles directly from benchmark runs — no HTTP server needed:
105+
106+
```bash
107+
# CPU profile
108+
go test -bench=BenchmarkParse -cpuprofile=cpu.prof ./pkg/parser
109+
go tool pprof cpu.prof
110+
111+
# Memory profile (alloc_objects shows GC churn, inuse_space shows leaks)
112+
go test -bench=BenchmarkParse -memprofile=mem.prof ./pkg/parser
113+
go tool pprof -alloc_objects mem.prof
114+
115+
# Execution trace
116+
go test -bench=BenchmarkParse -trace=trace.out ./pkg/parser
117+
go tool trace trace.out
118+
```
119+
120+
For full pprof CLI reference (all commands, non-interactive mode, profile interpretation), see [pprof Reference](./references/pprof.md). For execution trace interpretation, see [Trace Reference](./references/trace.md). For statistical comparison, see [benchstat Reference](./references/benchstat.md).
121+
122+
## Reference Files
123+
124+
- **[pprof Reference](./references/pprof.md)** — Interactive and non-interactive analysis of CPU, memory, and goroutine profiles. Full CLI commands, profile types (CPU vs alloc*objects vs inuse_space), web UI navigation, and interpretation patterns. Use this to dive deep into \_where* time and memory are being spent in your code.
125+
126+
- **[benchstat Reference](./references/benchstat.md)** — Statistical comparison of benchmark runs with rigorous confidence intervals and p-value tests. Covers output reading, filtering old benchmarks, interleaving results for visual clarity, and regression detection. Use this when you need to prove a change made a meaningful performance difference, not just a lucky run.
127+
128+
- **[Trace Reference](./references/trace.md)** — Execution tracer for understanding *when* and *why* code runs. Visualizes goroutine scheduling, garbage collection phases, network blocking, and custom span annotations. Use this when pprof (which shows *where* CPU goes) isn't enough — you need to see the timeline of what happened.
129+
130+
- **[Diagnostic Tools](./references/tools.md)** — Quick reference for ancillary tools: fieldalignment (struct padding waste), GODEBUG (runtime logging flags), fgprof (frame graph profiles), race detector (concurrency bugs), and others. Use this when you have a specific symptom and need a focused diagnostic — don't reach for pprof if a simpler tool already answers your question.
131+
132+
- **[Compiler Analysis](./references/compiler-analysis.md)** — Low-level compiler optimization insights: escape analysis (when values move to the heap), inlining decisions (which function calls are eliminated), SSA dump (intermediate representation), and assembly output. Use this when benchmarks show allocations you didn't expect, or when you want to verify the compiler did what you intended.
133+
134+
- **[CI Regression Detection](./references/ci-regression.md)** — Automated performance regression gating in CI pipelines. Covers three tools (benchdiff for quick PR comparisons, cob for strict threshold-based gating, gobenchdata for long-term trend dashboards), noisy neighbor mitigation strategies (why cloud CI benchmarks vary 5-10% even on quiet machines), and self-hosted runner tuning to make benchmarks reproducible. Use this when you want to ensure pull requests don't silently slow down your codebase — detecting regressions early prevents shipping performance debt.
135+
136+
- **[Investigation Session](./references/investigation-session.md)** — Production performance troubleshooting workflow combining Prometheus runtime metrics (heap size, GC frequency, goroutine counts), PromQL queries to correlate metrics with code changes, runtime configuration flags (GODEBUG env vars to enable GC logging), and cost warnings (when you're hitting performance tax). Use this when production benchmarks look good but real traffic behaves differently.
137+
138+
- **[Prometheus Go Metrics Reference](./references/prometheus-go-metrics.md)** — Complete listing of Go runtime metrics actually exposed as Prometheus metrics by `prometheus/client_golang`. Covers 30 default metrics, 40+ optional metrics (Go 1.17+), process metrics, and common PromQL queries. Distinguishes between `runtime/metrics` (Go internal data) and Prometheus metrics (what you scrape from `/metrics`). Use this when setting up monitoring dashboards or writing PromQL queries for production alerts.
139+
140+
## Cross-References
141+
142+
- → See `samber/cc-skills-golang@golang-performance` skill for optimization patterns to apply after measuring ("if X bottleneck, apply Y")
143+
- → See `samber/cc-skills-golang@golang-troubleshooting` skill for pprof setup on running services (enable, secure, capture), Delve debugger, GODEBUG flags, root cause methodology
144+
- → See `samber/cc-skills-golang@golang-observability` skill for everyday always-on monitoring, continuous profiling (Pyroscope), distributed tracing (OpenTelemetry)
145+
- → See `samber/cc-skills-golang@golang-testing` skill for general testing practices
146+
- → See `samber/cc-skills@promql-cli` skill for querying Prometheus runtime metrics in production to validate benchmark findings

0 commit comments

Comments
 (0)