# Go Testing Benchmarks

> Benchmark patterns for performance testing Use when this capability is needed.

- Skill: `tomevault-io/go-testing-benchmarks` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/go-testing-benchmarks`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/go-testing-benchmarks/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/go-testing-benchmarks

---


# Benchmarks

Measure function performance with benchmark tests.

## CORRECT

```go
func Benchmark_Fibonacci(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Fibonacci(10)
    }
}

func Benchmark_Fibonacci_Cases(b *testing.B) {
    cases := []struct {
        name string
        n    int
    }{
        {name: "small", n: 10},
        {name: "medium", n: 20},
        {name: "large", n: 30},
    }

    for _, bc := range cases {
        b.Run(bc.name, func(b *testing.B) {
            for i := 0; i < b.N; i++ {
                Fibonacci(bc.n)
            }
        })
    }
}

func Benchmark_StringConcat(b *testing.B) {
    b.ResetTimer()  // Exclude setup time
    for i := 0; i < b.N; i++ {
        _ = "hello" + "world"
    }
}
```

**Run benchmarks:**
```bash
go test -bench=. -benchmem
```

**Why:**
- b.N adjusted automatically for stable timing
- Sub-benchmarks compare variations
- b.ResetTimer() excludes setup
- -benchmem shows allocation stats

## WRONG

```go
func Benchmark_Fibonacci(b *testing.B) {
    Fibonacci(10)  // Missing loop
}

func Benchmark_NoReset(b *testing.B) {
    // Expensive setup
    data := generateLargeData()
    // Missing b.ResetTimer()
    for i := 0; i < b.N; i++ {
        Process(data)
    }
}
```

**Problems:**
- No b.N loop = invalid benchmark
- Setup time included in measurement
- Inaccurate performance results

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/jamesprial) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-13 -->

