spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / evaluator / layers / performance.rs
| 1 | use super::LayerResult; |
| 2 | use crate::evaluator::stats::{BootstrapEstimate, StatisticalEngine, TailNonInferiorityResult}; |
| 3 | use serde::{Deserialize, Serialize}; |
| 4 | |
| 5 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 6 | pub struct PerformanceEvaluation { |
| 7 | pub bootstrap_estimate: BootstrapEstimate, |
| 8 | pub p95_non_inferiority: TailNonInferiorityResult, |
| 9 | pub p99_non_inferiority: TailNonInferiorityResult, |
| 10 | pub target_metric_improved: bool, |
| 11 | pub non_target_metrics_safe: bool, |
| 12 | pub passed: bool, |
| 13 | pub violations: Vec<String>, |
| 14 | } |
| 15 | |
| 16 | impl PerformanceEvaluation { |
| 17 | pub fn to_layer_result(&self) -> LayerResult { |
| 18 | let score = if self.passed { |
| 19 | 1.0 |
| 20 | } else if self.non_target_metrics_safe { |
| 21 | 0.5 |
| 22 | } else { |
| 23 | 0.0 |
| 24 | }; |
| 25 | |
| 26 | let summary = format!( |
| 27 | "Performance: delta={:.2}%, p_val={:.4}, p95_ci_upper={:.2}%, p99_ci_upper={:.2}%, sig={}", |
| 28 | self.bootstrap_estimate.delta_pct, |
| 29 | self.bootstrap_estimate.p_value, |
| 30 | self.p95_non_inferiority.ci_95_upper_pct, |
| 31 | self.p99_non_inferiority.ci_95_upper_pct, |
| 32 | if self.bootstrap_estimate.is_statistically_significant { "YES" } else { "NO" } |
| 33 | ); |
| 34 | |
| 35 | LayerResult { |
| 36 | layer_name: "Performance".to_string(), |
| 37 | is_hard_invariant: false, |
| 38 | passed: self.passed, |
| 39 | score, |
| 40 | summary, |
| 41 | violations: self.violations.clone(), |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub struct PerformanceLayer; |
| 47 | |
| 48 | impl PerformanceLayer { |
| 49 | pub fn evaluate_latencies( |
| 50 | parent_latencies_us: &[f64], |
| 51 | candidate_latencies_us: &[f64], |
| 52 | target_is_latency_reduction: bool, |
| 53 | resamples: usize, |
| 54 | seed: Option<u64>, |
| 55 | ) -> Result<PerformanceEvaluation, String> { |
| 56 | Self::evaluate_latencies_with_policy( |
| 57 | parent_latencies_us, |
| 58 | candidate_latencies_us, |
| 59 | target_is_latency_reduction, |
| 60 | true, |
| 61 | 1.0, |
| 62 | resamples, |
| 63 | seed, |
| 64 | ) |
| 65 | } |
| 66 | |
| 67 | pub fn evaluate_latencies_with_policy( |
| 68 | parent_latencies_us: &[f64], |
| 69 | candidate_latencies_us: &[f64], |
| 70 | target_is_latency_reduction: bool, |
| 71 | require_significant_improvement: bool, |
| 72 | non_inferiority_margin_pct: f64, |
| 73 | resamples: usize, |
| 74 | seed: Option<u64>, |
| 75 | ) -> Result<PerformanceEvaluation, String> { |
| 76 | let mut violations = Vec::new(); |
| 77 | |
| 78 | let boot = StatisticalEngine::bootstrap_paired_comparison( |
| 79 | parent_latencies_us, |
| 80 | candidate_latencies_us, |
| 81 | resamples, |
| 82 | seed, |
| 83 | )?; |
| 84 | |
| 85 | let target_metric_improved = if target_is_latency_reduction { |
| 86 | boot.is_statistically_significant && boot.observed_delta_mean < 0.0 |
| 87 | } else { |
| 88 | boot.is_statistically_significant && boot.observed_delta_mean > 0.0 |
| 89 | }; |
| 90 | |
| 91 | if require_significant_improvement && !target_metric_improved { |
| 92 | violations.push(format!( |
| 93 | "Target metric did not achieve statistical significance (p={:.4}, delta={:.2}%)", |
| 94 | boot.p_value, boot.delta_pct |
| 95 | )); |
| 96 | } |
| 97 | |
| 98 | let p95_res = StatisticalEngine::evaluate_tail_non_inferiority( |
| 99 | parent_latencies_us, |
| 100 | candidate_latencies_us, |
| 101 | 95.0, |
| 102 | non_inferiority_margin_pct, |
| 103 | resamples, |
| 104 | seed.map(|s| s.wrapping_add(1)), |
| 105 | )?; |
| 106 | |
| 107 | if !p95_res.passes_non_inferiority { |
| 108 | violations.push(format!( |
| 109 | "p95 tail latency exceeded non-inferiority bound: upper_ci={:.2}% > {:.1}%", |
| 110 | p95_res.ci_95_upper_pct, non_inferiority_margin_pct, |
| 111 | )); |
| 112 | } |
| 113 | |
| 114 | let p99_res = StatisticalEngine::evaluate_tail_non_inferiority( |
| 115 | parent_latencies_us, |
| 116 | candidate_latencies_us, |
| 117 | 99.0, |
| 118 | non_inferiority_margin_pct, |
| 119 | resamples, |
| 120 | seed.map(|s| s.wrapping_add(2)), |
| 121 | )?; |
| 122 | |
| 123 | if !p99_res.passes_non_inferiority { |
| 124 | violations.push(format!( |
| 125 | "p99 tail latency exceeded non-inferiority bound: upper_ci={:.2}% > {:.1}%", |
| 126 | p99_res.ci_95_upper_pct, non_inferiority_margin_pct, |
| 127 | )); |
| 128 | } |
| 129 | |
| 130 | let non_target_metrics_safe = |
| 131 | p95_res.passes_non_inferiority && p99_res.passes_non_inferiority; |
| 132 | let passed = if require_significant_improvement { |
| 133 | target_metric_improved && non_target_metrics_safe |
| 134 | } else { |
| 135 | non_target_metrics_safe |
| 136 | }; |
| 137 | |
| 138 | Ok(PerformanceEvaluation { |
| 139 | bootstrap_estimate: boot, |
| 140 | p95_non_inferiority: p95_res, |
| 141 | p99_non_inferiority: p99_res, |
| 142 | target_metric_improved, |
| 143 | non_target_metrics_safe, |
| 144 | passed, |
| 145 | violations, |
| 146 | }) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[cfg(test)] |
| 151 | mod tests { |
| 152 | use super::*; |
| 153 | |
| 154 | #[test] |
| 155 | fn test_performance_evaluation_improvement_passes() { |
| 156 | let parent = vec![ |
| 157 | 100.0, 101.0, 99.0, 102.0, 98.0, 100.5, 101.5, 99.5, 100.0, 101.0, 100.0, 101.0, 99.0, |
| 158 | 102.0, 98.0, 100.5, 101.5, 99.5, 100.0, 101.0, 100.0, 101.0, 99.0, 102.0, 98.0, 100.5, |
| 159 | 101.5, 99.5, 100.0, 101.0, |
| 160 | ]; |
| 161 | let candidate = vec![ |
| 162 | 80.0, 81.0, 79.0, 82.0, 78.0, 80.5, 81.5, 79.5, 80.0, 81.0, 80.0, 81.0, 79.0, 82.0, |
| 163 | 78.0, 80.5, 81.5, 79.5, 80.0, 81.0, 80.0, 81.0, 79.0, 82.0, 78.0, 80.5, 81.5, 79.5, |
| 164 | 80.0, 81.0, |
| 165 | ]; |
| 166 | |
| 167 | let eval = PerformanceLayer::evaluate_latencies(&parent, &candidate, true, 2000, Some(42)) |
| 168 | .unwrap(); |
| 169 | assert!(eval.passed); |
| 170 | assert!(eval.target_metric_improved); |
| 171 | assert!(eval.non_target_metrics_safe); |
| 172 | |
| 173 | let lr = eval.to_layer_result(); |
| 174 | assert!(lr.passed); |
| 175 | assert!((lr.score - 1.0).abs() < 1e-6); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn test_performance_evaluation_regression_fails() { |
| 180 | let parent = vec![100.0; 30]; |
| 181 | let candidate = vec![110.0; 30]; |
| 182 | |
| 183 | let eval = PerformanceLayer::evaluate_latencies(&parent, &candidate, true, 2000, Some(42)) |
| 184 | .unwrap(); |
| 185 | assert!(!eval.passed); |
| 186 | assert!(!eval.target_metric_improved); |
| 187 | assert!(!eval.non_target_metrics_safe); |
| 188 | } |
| 189 | } |