Skip to content

gozephyr/circuit-breaker-benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Circuit Breaker Benchmark

A simple, one-time benchmark suite to compare gozephyr/cbreak vs sony/gobreaker circuit breaker implementations.

Quick Start

# Install dependencies
go mod tidy

# Run all benchmarks
./scripts/run_all.sh

# Or run manually
go test -bench=. -benchmem ./benchmarks/...

Benchmark Categories

1. Throughput Tests (throughput_test.go)

  • Normal throughput performance
  • High-load scenarios
  • Context-aware execution
  • Memory allocation patterns

2. Latency Tests (latency_test.go)

  • Response time distribution
  • High latency scenarios
  • Concurrent execution
  • Timeout handling

3. Memory Tests (memory_test.go)

  • Memory allocation efficiency
  • High-load memory usage
  • Failure scenario memory impact
  • State transition overhead

4. Stress Tests (stress_test.go)

  • Failure detection speed
  • Recovery time
  • Intermittent failures
  • Concurrent failure scenarios

Real Benchmark Results

Performance Comparison (AMD Ryzen 5 7430U, 29GB RAM)

System Information:

  • OS: Linux x86_64
  • CPU: AMD Ryzen 5 7430U with Radeon Graphics
  • RAM: 29Gi
  • Go Version: go1.23.0

High Latency Scenarios

BenchmarkCbreakHighLatency-12                 10         100248743 ns/op             731 B/op            10 allocs/op
BenchmarkGobreakerHighLatency-12              10         100221760 ns/op               8 B/op             0 allocs/op

Analysis:

  • 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

Timeout Handling

BenchmarkCbreakLatencyWithTimeout-12              932079              1280 ns/op             278 B/op             5 allocs/op
BenchmarkGobreakerLatencyWithTimeout-12               22          50182295 ns/op               4 B/op             0 allocs/op

Analysis:

  • 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

Failure Detection

BenchmarkCbreakFailureDetection-12        153067              7359 ns/op              16 B/op             1 allocs/op
BenchmarkGobreakerFailureDetection-12             512650              2331 ns/op               0 B/op             0 allocs/op

Analysis:

  • 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

Performance Summary

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

Recommendations

Choose cbreak when

  • 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

Choose gobreaker when

  • 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

Usage

Run All Benchmarks

./scripts/run_all.sh

Run Specific Tests

# 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/

Generate Visualizations

# After running benchmarks
python3 scripts/plot_results.py results/benchmark_output.txt results/

Understanding Results

Key Metrics

  • 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)

Expected "Failures"

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

Prerequisites

  • Go 1.21 or later
  • Python 3.7+ (for visualization)
  • matplotlib (for charts): pip install matplotlib numpy

License

MIT License - see LICENSE file for details.

Acknowledgments

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published