Benchmarking skill for comparing multiple implementations. Creates comprehensive performance comparisons with proper methodology...
This skill provides comprehensive benchmarking standards for performance comparison. Designed to compare multiple implementations in a single benchmark file.
| Metric | Requirement |
|---|---|
| Warm-up | Reset state between iterations |
| Fair Comparison | Same data sizes, same conditions |
| Multiple Sizes | Test small, medium, large data |
| Memory Reporting | Use b.ReportAllocs() |
| Clear Naming | BenchmarkOperation_Implementation_Size |
Identify:
Output Format:
## Benchmark Design: [comparison name]
### Implementations
- [Implementation 1]: Description
- [Implementation 2]: Description
### Operations
| Operation | Description | Why benchmark? |
|-----------|-------------|----------------|
| Write | Append data | Core operation |
| Read | Consume data | Core operation |
### Data Sizes
- Small: 64 bytes (cache-friendly)
- Medium: 1KB (typical payload)
- Large: 1MB (stress test)
STOP and wait for user approval.
Create benchmark matrix:
| Benchmark ID | Operation | Implementation | Size | Setup |
|---|---|---|---|---|
| B1.1 | Write | RingBuffer | 64B | New(1024) |
| B1.2 | Write | LinkedListBuffer | 64B | New() |
| B2.1 | Write | RingBuffer | 1KB | New(4096) |
| B2.2 | Write | LinkedListBuffer | 1KB | New() |
Considerations:
STOP and wait for user approval.
Follow these Go benchmarking standards:
benchmark_comparison_test.go
โโโ // Data generators
โ var smallData = make([]byte, 64)
โ var mediumData = make([]byte, 1024)
โ var largeData = make([]byte, 1<<20)
โ
โโโ // Comparison benchmarks (grouped by operation)
โ func BenchmarkWrite(b *testing.B)
โ โโโ b.Run("RingBuffer/64B", ...)
โ โโโ b.Run("RingBuffer/1KB", ...)
โ โโโ b.Run("LinkedList/64B", ...)
โ โโโ b.Run("LinkedList/1KB", ...)
โ
โโโ func BenchmarkRead(b *testing.B)
โ โโโ b.Run("RingBuffer/64B", ...)
โ โโโ ...
Comparison Benchmark (Grouped by Operation):
func BenchmarkWrite(b *testing.B) {
sizes := []struct {
name string
data []byte
}{
{"64B", make([]byte, 64)},
{"1KB", make([]byte, 1024)},
{"1MB", make([]byte, 1<<20)},
}
for _, size := range sizes {
// RingBuffer
b.Run("RingBuffer/"+size.name, func(b *testing.B) {
buf := NewRing(len(size.data) * 2)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.Write(size.data)
buf.Reset()
}
})
// LinkedListBuffer
b.Run("LinkedList/"+size.name, func(b *testing.B) {
buf := &LinkedListBuffer{}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.PushBack(size.data)
buf.Reset()
}
})
}
}
Read After Write Pattern:
func BenchmarkRead(b *testing.B) {
data := make([]byte, 1024)
readBuf := make([]byte, 1024)
b.Run("RingBuffer", func(b *testing.B) {
buf := NewRing(2048)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.Write(data)
buf.Read(readBuf)
}
})
b.Run("LinkedList", func(b *testing.B) {
buf := &LinkedListBuffer{}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.PushBack(data)
buf.Read(readBuf)
}
})
}
Memory-Heavy Benchmark:
func BenchmarkMemory(b *testing.B) {
b.Run("RingBuffer/Grow", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf := NewRing(64)
for j := 0; j < 1000; j++ {
buf.Write(make([]byte, 100))
}
}
})
}
# Run all benchmarks
go test -bench=. -benchmem
# Run specific comparison
go test -bench=BenchmarkWrite -benchmem
# Run with count for stability
go test -bench=. -benchmem -count=5
# Compare with benchstat
go test -bench=. -benchmem -count=10 > old.txt
# (make changes)
go test -bench=. -benchmem -count=10 > new.txt
benchstat old.txt new.txt
BenchmarkWrite/RingBuffer/64B-8 10000000 120 ns/op 0 B/op 0 allocs/op
BenchmarkWrite/LinkedList/64B-8 5000000 250 ns/op 64 B/op 1 allocs/op
| Column | Meaning |
|---|---|
-8 |
GOMAXPROCS (8 cores) |
10000000 |
Iterations run |
120 ns/op |
Time per operation |
0 B/op |
Bytes allocated per op |
0 allocs/op |
Allocations per op |
Interpretation: RingBuffer ~2x faster, no allocations.
Operation/Implementation/Size| Practice | Why |
|---|---|
| Pre-allocate data outside loop | Avoid measurement pollution |
Use b.StopTimer() / b.StartTimer() |
Exclude setup from measurement |
| Reset buffers after write | Simulate real usage pattern |
| Test multiple sizes | Find performance characteristics |
Run -count=10 |
Statistical stability |
After each step, ask:
"Please review the above and confirm if I should proceed to the next step."