A simple, one-time benchmark suite to compare gozephyr/cbreak vs sony/gobreaker circuit breaker implementations.
# Install dependencies
go mod tidy
# Run all benchmarks
./scripts/run_all.sh
# Or run manually
go test -bench=. -benchmem ./benchmarks/...- Normal throughput performance
- High-load scenarios
- Context-aware execution
- Memory allocation patterns
- Response time distribution
- High latency scenarios
- Concurrent execution
- Timeout handling
- Memory allocation efficiency
- High-load memory usage
- Failure scenario memory impact
- State transition overhead
- Failure detection speed
- Recovery time
- Intermittent failures
- Concurrent failure scenarios
System Information:
- OS: Linux x86_64
- CPU: AMD Ryzen 5 7430U with Radeon Graphics
- RAM: 29Gi
- Go Version: go1.23.0
BenchmarkCbreakHighLatency-12 10 100248743 ns/op 731 B/op 10 allocs/op
BenchmarkGobreakerHighLatency-12 10 100221760 ns/op 8 B/op 0 allocs/opAnalysis:
- Latency: Both libraries perform similarly (~100ms for high latency scenarios)
- Memory: Gobreaker uses significantly less memory (8B vs 731B per operation)
- Allocations: Gobreaker has zero allocations vs 10 for cbreak
BenchmarkCbreakLatencyWithTimeout-12 932079 1280 ns/op 278 B/op 5 allocs/op
BenchmarkGobreakerLatencyWithTimeout-12 22 50182295 ns/op 4 B/op 0 allocs/opAnalysis:
- Normal Operations: Cbreak is ~3.9x faster (1.28μs vs 50.18ms)
- Memory Efficiency: Both are memory efficient for timeout scenarios
- Allocation Pattern: Cbreak has more allocations but better performance
BenchmarkCbreakFailureDetection-12 153067 7359 ns/op 16 B/op 1 allocs/op
BenchmarkGobreakerFailureDetection-12 512650 2331 ns/op 0 B/op 0 allocs/opAnalysis:
- Failure Detection Speed: Gobreaker is ~3.2x faster at detecting failures
- Memory Usage: Gobreaker uses no additional memory for failure detection
- Efficiency: Gobreaker shows better efficiency in failure scenarios
| Metric | cbreak | gobreaker | Winner |
|---|---|---|---|
| Normal Operations | 1.28μs | 50.18ms | cbreak |
| High Latency | 100.25ms | 100.22ms | Tie |
| Failure Detection | 7.36μs | 2.33μs | gobreaker |
| Memory Usage | 278-731B | 0-8B | gobreaker |
| Allocations | 1-10 | 0 | gobreaker |
- Memory is not a constraint - You can afford higher memory usage
- Normal operations are priority - Most of your operations succeed
- Timeout handling is critical - You need efficient timeout management
- Simple API is preferred - You want a straightforward interface
- Memory efficiency is critical - You need minimal memory footprint
- Failure detection speed matters - You need quick circuit opening
- Battle-tested reliability - You prefer a mature, widely-used library
- Zero allocations are important - You want to minimize GC pressure
./scripts/run_all.sh# Throughput only
go test -bench=Benchmark.*Throughput -benchmem ./benchmarks/
# Latency only
go test -bench=Benchmark.*Latency -benchmem ./benchmarks/
# Memory only
go test -bench=Benchmark.*Memory -benchmem ./benchmarks/
# Stress tests only
go test -bench=Benchmark.*Failure -benchmem ./benchmarks/# After running benchmarks
python3 scripts/plot_results.py results/benchmark_output.txt results/- ns/op: Nanoseconds per operation (lower is better)
- B/op: Bytes allocated per operation (lower is better)
- allocs/op: Number of allocations per operation (lower is better)
Some benchmark tests may show "FAIL" status - this is expected behavior! The circuit breakers are working correctly by:
- Detecting failures and opening the circuit
- Preventing cascading failures
- Demonstrating proper circuit breaker functionality
- Go 1.21 or later
- Python 3.7+ (for visualization)
- matplotlib (for charts):
pip install matplotlib numpy
MIT License - see LICENSE file for details.
- gozephyr/cbreak - Circuit breaker implementation
- sony/gobreaker - Sony's circuit breaker library