This directory contains comprehensive performance benchmarks for the Keystone HMAS using Google Benchmark.
Keystone includes 45+ benchmark tests across 5 benchmark suites:
- hierarchy_benchmarks (5 benchmarks) - Sync vs async agent hierarchies
- message_pool_benchmarks (10 benchmarks) - Message pooling performance
- distributed_benchmarks (8 benchmarks) - Distributed work-stealing
- message_bus_benchmarks (11 benchmarks) - MessageBus routing and delivery
- resilience_benchmarks (20 benchmarks) - Retry policy, circuit breaker, heartbeat
Benchmarks should be built in Release mode for accurate performance measurements:
# Clean build
rm -rf build
mkdir build && cd build
# Configure with Release mode
cmake -DCMAKE_BUILD_TYPE=Release -G Ninja ..
# Build all benchmarks
ninja
# Benchmark executables are now available:
# - hierarchy_benchmarks
# - message_pool_benchmarks
# - distributed_benchmarks
# - message_bus_benchmarks
# - resilience_benchmarks# Run all benchmarks with automated regression detection
./scripts/run_benchmarks.sh
# Run a specific benchmark suite
./build/message_bus_benchmarks
# Run with filter
./build/message_bus_benchmarks --benchmark_filter=BM_MessageRoutingThe scripts/run_benchmarks.sh script provides:
- Automated execution of all benchmark suites
- Result aggregation in JSON format
- Baseline management for regression detection
- Performance regression detection (>10% slowdown triggers failure)
# Basic usage
./scripts/run_benchmarks.sh
# Save current run as baseline
./scripts/run_benchmarks.sh --baseline
# Compare against baseline (CI/CD)
./scripts/run_benchmarks.sh --compare benchmarks/results/baseline.json
# Run specific benchmarks
./scripts/run_benchmarks.sh --filter BM_MessageRouting
# Output formats
./scripts/run_benchmarks.sh --format json
./scripts/run_benchmarks.sh --format csvMeasures performance of sync vs async agent hierarchies:
./build/hierarchy_benchmarksBenchmarks:
BM_Sync4LayerHierarchy- Synchronous 4-layer message flowBM_Async4LayerHierarchy_4Workers- Async with 4 worker threadsBM_Async4LayerHierarchy_8Workers- Async with 8 worker threadsBM_AsyncTaskAgentThroughput- Task agent message throughputBM_SchedulerSubmissionRate- Work-stealing scheduler submission rate
Key Metrics:
- Messages/second throughput
- Latency per message
- Scalability with worker count
Measures message pooling effectiveness:
./build/message_pool_benchmarksBenchmarks:
BM_MessageCreation_NoPooling- Baseline allocation overheadBM_MessageCreation_WithPooling- Pooled allocationBM_MessageBurst_*- Burst traffic patternsBM_SteadyState_*- Steady-state trafficBM_PoolStatistics- Pool statistics overheadBM_PoolHitRate- Cache hit rate measurementBM_ThreadLocalPooling- Thread-local pool performance
Key Metrics:
- Allocation speed (allocations/sec)
- Pool hit rate (%)
- Memory reuse efficiency
Measures distributed work-stealing performance across simulated NUMA nodes:
./build/distributed_benchmarksBenchmarks:
BM_WorkStealing_LocalOnly- Single-node baselineBM_WorkStealing_TwoNodes_*- Cross-node stealing with varying latenciesBM_LoadBalancing_Imbalanced- Load balancing effectivenessBM_NetworkOverhead_MessageOnly- Network overhead measurementBM_AgentAffinity_Registered- CPU affinity impactBM_PacketLoss_Impact- Packet loss resilience
Key Metrics:
- Work stealing latency
- Load balance ratio
- Network overhead (%)
- Packet loss impact
Measures MessageBus routing and delivery performance:
./build/message_bus_benchmarksBenchmarks:
BM_MessageRouting_SingleAgent- Single agent routing latencyBM_MessageRouting_FanOut- Fan-out to N agents (8-512)BM_AgentRegistration- Agent registration overheadBM_AgentUnregistration- Unregistration overheadBM_AgentLookup- hasAgent() lookup speed (8-1024 agents)BM_ListAgents- List all agents overheadBM_ConcurrentRouting- Multi-threaded routing (1-8 threads)BM_MessageRouting_WithPayload- Payload size impact (64B-64KB)BM_MessageRoundTrip- Round-trip latencyBM_MessageBroadcast- Broadcast to N agents (8-256)
Key Metrics:
- Routing latency (ns/message)
- Throughput (messages/sec)
- Scalability with agent count
- Payload size impact
Measures retry policy, circuit breaker, and heartbeat monitor performance:
./build/resilience_benchmarksBenchmarks:
Retry Policy (8 benchmarks):
BM_RetryPolicy_Creation- Policy creation overheadBM_RetryPolicy_ShouldRetry- shouldRetry() latencyBM_RetryPolicy_BackoffCalculation- Backoff delay calculationBM_RetryPolicy_FullSequence- Full retry sequence (1-64 retries)BM_RetryPolicy_VaryingMultiplier- Multiplier impact (1.0-5.0)
Circuit Breaker (8 benchmarks):
BM_CircuitBreaker_Creation- CB creation overheadBM_CircuitBreaker_AllowRequest_Closed- Request check latencyBM_CircuitBreaker_RecordSuccess- Success recordingBM_CircuitBreaker_RecordFailure- Failure recordingBM_CircuitBreaker_StateTransition- State transition latencyBM_CircuitBreaker_GetState- State queryBM_CircuitBreaker_Concurrent- Multi-threaded access (1-8 threads)
Heartbeat Monitor (6 benchmarks):
BM_HeartbeatMonitor_Creation- Monitor creationBM_HeartbeatMonitor_RegisterAgent- Agent registrationBM_HeartbeatMonitor_RecordHeartbeat- Heartbeat recordingBM_HeartbeatMonitor_IsAgentAlive- Liveness checkBM_HeartbeatMonitor_GetDeadAgents- Dead agent detection (8-512 agents)BM_HeartbeatMonitor_ConcurrentHeartbeat- Concurrent heartbeat (1-8 threads)
Key Metrics:
- Retry policy overhead (ns)
- Circuit breaker latency (ns)
- Heartbeat recording speed
- Concurrent access performance
Based on initial benchmarks, Keystone targets:
| Component | Metric | Target | Notes |
|---|---|---|---|
| MessageBus Routing | Latency | < 500 ns | Single agent, no payload |
| MessageBus Routing | Throughput | > 2M msg/sec | Single thread |
| Message Pool | Allocation | > 10M alloc/sec | With pooling enabled |
| Work-Stealing | Steal Latency | < 10 us | Same node |
| Work-Stealing | Remote Steal | < 100 us | Cross-node (100us RTT) |
| Retry Policy | shouldRetry | < 20 ns | Check only |
| Circuit Breaker | allowRequest | < 50 ns | Closed state |
| Heartbeat | Record | < 100 ns | Single heartbeat |
Google Benchmark supports latency percentiles using SetStatistics:
BENCHMARK(BM_MessageRouting)
->Repetitions(1000)
->ComputeStatisticsWithPercentiles({50, 95, 99, 99.9});This reports:
- p50 (median) - Typical latency
- p95 - 95th percentile
- p99 - 99th percentile (tail latency)
- p99.9 - 99.9th percentile (worst-case)
The run_benchmarks.sh script implements automated regression detection:
# 1. Establish baseline (after implementing a feature)
./scripts/run_benchmarks.sh --baseline
# 2. Make changes to code
# ... edit code ...
# 3. Run regression check
./scripts/run_benchmarks.sh --compare benchmarks/results/baseline.json
# If regressions detected (>10% slowdown), script exits with error code 1- Regression: >10% slower than baseline (ratio > 1.10)
- Improvement: >10% faster than baseline (ratio < 0.90)
- Unchanged: Within ±10% of baseline
✓ Passing: 38
↑ Improvements: 3
↓ Regressions: 2
=== REGRESSIONS (>10% slower) ===
BM_MessageRouting_FanOut/512
Baseline: 245.23 us
Current: 285.67 us
Change: +16.5% (x1.16)
In CI/CD pipelines (GitHub Actions):
# .github/workflows/benchmarks.yml
- name: Run Benchmarks
run: |
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -G Ninja ..
ninja
cd ..
./scripts/run_benchmarks.sh --compare benchmarks/results/baseline.jsonThis ensures no performance regressions are merged to main.
For detailed performance analysis:
# Run with profiler
perf record -g ./build/message_bus_benchmarks --benchmark_filter=BM_MessageRouting_SingleAgent
perf report
# Flamegraph visualization
perf script | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > flamegraph.svgIf benchmarks show high variance:
# Increase minimum time
./build/message_bus_benchmarks --benchmark_min_time=5.0
# Increase repetitions
./build/message_bus_benchmarks --benchmark_repetitions=10
# Disable CPU frequency scaling
sudo cpupower frequency-set --governor performanceIf throughput is unexpectedly low:
- Verify Release build:
cmake -DCMAKE_BUILD_TYPE=Release - Check CPU governor:
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor - Disable thermal throttling monitoring
- Run on isolated CPU core:
taskset -c 0 ./build/message_bus_benchmarks
- Create 5 comprehensive benchmark suites (45+ benchmarks total)
- Implement message bus performance benchmarks (11 tests)
- Implement resilience performance benchmarks (20 tests)
- Add CMake integration for new benchmarks
- Create automated benchmark runner script
- Implement regression detection (>10% threshold)
- Establish performance baselines and targets
- Document benchmark usage and CI/CD integration
- Run initial baseline and commit results (optional)
Next: Phase 9.5 - CI/CD Quality Gates