go-testing-benchmarks

Benchmark patterns for performance testing

⭐2stars🔀0forks📁View on GitHub🕐Updated Dec 28, 2025

When & Why to Use This Skill

This Claude skill provides standardized benchmark patterns for Go performance testing, enabling developers to accurately measure function execution time and memory allocations. It helps in identifying performance bottlenecks and ensuring code efficiency by following Go's testing package best practices, such as proper loop implementation and timer resets.

Use Cases

  • Case 1: Measuring the execution time of core algorithms to identify performance bottlenecks in high-load applications.
  • Case 2: Comparing different code implementations or data structures to determine which version is more CPU and memory efficient.
  • Case 3: Profiling memory allocations using the -benchmem flag to optimize garbage collection overhead in Go services.
  • Case 4: Implementing sub-benchmarks to evaluate how function performance scales across different input sizes (e.g., small vs. large datasets).
namego-testing-benchmarks
descriptionBenchmark patterns for performance testing

Benchmarks

Measure function performance with benchmark tests.

CORRECT

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:

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

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