|
| 1 | +//! Stability Layer Benchmarks |
| 2 | +//! |
| 3 | +//! Benchmarks for crash recovery, warmup tracking, and exhaustion monitoring. |
| 4 | +
|
| 5 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 6 | +use mindfry::stability::{ |
| 7 | + ExhaustionLevel, ExhaustionMonitor, RecoveryAnalyzer, RecoveryState, ShutdownMarker, |
| 8 | + WarmupTracker, |
| 9 | +}; |
| 10 | + |
| 11 | +// ═══════════════════════════════════════════════════════════════ |
| 12 | +// RECOVERY BENCHMARKS |
| 13 | +// ═══════════════════════════════════════════════════════════════ |
| 14 | + |
| 15 | +fn bench_recovery_analyzer_new(c: &mut Criterion) { |
| 16 | + let marker = Some(ShutdownMarker::graceful()); |
| 17 | + |
| 18 | + c.bench_function("recovery_analyzer_new", |b| { |
| 19 | + b.iter(|| black_box(RecoveryAnalyzer::new(marker.clone()))) |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +fn bench_recovery_analyzer_analyze(c: &mut Criterion) { |
| 24 | + let marker = Some(ShutdownMarker::graceful()); |
| 25 | + let analyzer = RecoveryAnalyzer::new(marker); |
| 26 | + |
| 27 | + c.bench_function("recovery_analyzer_analyze", |b| { |
| 28 | + b.iter(|| black_box(analyzer.analyze())) |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +fn bench_recovery_state_intensity(c: &mut Criterion) { |
| 33 | + c.bench_function("recovery_state_intensity", |b| { |
| 34 | + b.iter(|| { |
| 35 | + black_box(RecoveryState::Normal.intensity()); |
| 36 | + black_box(RecoveryState::Shock.intensity()); |
| 37 | + black_box(RecoveryState::Coma.intensity()); |
| 38 | + }) |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +// ═══════════════════════════════════════════════════════════════ |
| 43 | +// WARMUP BENCHMARKS |
| 44 | +// ═══════════════════════════════════════════════════════════════ |
| 45 | + |
| 46 | +fn bench_warmup_tracker_new(c: &mut Criterion) { |
| 47 | + c.bench_function("warmup_tracker_new", |b| { |
| 48 | + b.iter(|| black_box(WarmupTracker::new())) |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +fn bench_warmup_tracker_is_ready(c: &mut Criterion) { |
| 53 | + let tracker = WarmupTracker::new(); |
| 54 | + |
| 55 | + c.bench_function("warmup_tracker_is_ready", |b| { |
| 56 | + b.iter(|| black_box(tracker.is_ready())) |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +fn bench_warmup_tracker_state(c: &mut Criterion) { |
| 61 | + let tracker = WarmupTracker::new(); |
| 62 | + |
| 63 | + c.bench_function("warmup_tracker_state", |b| { |
| 64 | + b.iter(|| black_box(tracker.state())) |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +fn bench_warmup_tracker_clone(c: &mut Criterion) { |
| 69 | + let tracker = WarmupTracker::new(); |
| 70 | + |
| 71 | + c.bench_function("warmup_tracker_clone", |b| { |
| 72 | + b.iter(|| black_box(tracker.clone())) |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +// ═══════════════════════════════════════════════════════════════ |
| 77 | +// EXHAUSTION BENCHMARKS |
| 78 | +// ═══════════════════════════════════════════════════════════════ |
| 79 | + |
| 80 | +fn bench_exhaustion_level_from_energy(c: &mut Criterion) { |
| 81 | + c.bench_function("exhaustion_level_from_energy", |b| { |
| 82 | + b.iter(|| { |
| 83 | + black_box(ExhaustionLevel::from_energy(0.9)); |
| 84 | + black_box(ExhaustionLevel::from_energy(0.5)); |
| 85 | + black_box(ExhaustionLevel::from_energy(0.1)); |
| 86 | + }) |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +fn bench_exhaustion_allows_writes(c: &mut Criterion) { |
| 91 | + c.bench_function("exhaustion_allows_writes", |b| { |
| 92 | + b.iter(|| { |
| 93 | + black_box(ExhaustionLevel::Normal.allows_writes()); |
| 94 | + black_box(ExhaustionLevel::Elevated.allows_writes()); |
| 95 | + black_box(ExhaustionLevel::Exhausted.allows_writes()); |
| 96 | + black_box(ExhaustionLevel::Emergency.allows_writes()); |
| 97 | + }) |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +fn bench_exhaustion_monitor_default(c: &mut Criterion) { |
| 102 | + c.bench_function("exhaustion_monitor_new", |b| { |
| 103 | + b.iter(|| black_box(ExhaustionMonitor::default())) |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +criterion_group!( |
| 108 | + benches, |
| 109 | + // Recovery |
| 110 | + bench_recovery_analyzer_new, |
| 111 | + bench_recovery_analyzer_analyze, |
| 112 | + bench_recovery_state_intensity, |
| 113 | + // Warmup |
| 114 | + bench_warmup_tracker_new, |
| 115 | + bench_warmup_tracker_is_ready, |
| 116 | + bench_warmup_tracker_state, |
| 117 | + bench_warmup_tracker_clone, |
| 118 | + // Exhaustion |
| 119 | + bench_exhaustion_level_from_energy, |
| 120 | + bench_exhaustion_allows_writes, |
| 121 | + bench_exhaustion_monitor_default, |
| 122 | +); |
| 123 | + |
| 124 | +criterion_main!(benches); |
0 commit comments