|
| 1 | +use anyhow::{anyhow, Result}; |
| 2 | +use serde::Serialize; |
| 3 | +use std::fs; |
| 4 | +use std::path::Path; |
| 5 | +use std::process::Command; |
| 6 | +use std::time::Instant; |
| 7 | + |
| 8 | +#[allow(dead_code)] |
| 9 | +#[derive(Serialize)] |
| 10 | +struct BenchmarkReport { |
| 11 | + phase: String, |
| 12 | + status: String, |
| 13 | + run_ms: u64, |
| 14 | + context_all_ms: u64, |
| 15 | + latest_report_valid: bool, |
| 16 | + performance_report_valid: bool, |
| 17 | + context_render_bytes: u64, |
| 18 | + checked_artifacts: Vec<String>, |
| 19 | + notes: String, |
| 20 | +} |
| 21 | + |
| 22 | +#[allow(dead_code)] |
| 23 | +pub fn run_benchmark_action() -> Result<()> { |
| 24 | + println!("=== agy-ct benchmark ==="); |
| 25 | + println!("Performance baseline measured on local validation environment."); |
| 26 | + println!("No performance optimization was performed in this phase."); |
| 27 | + println!("Measurements are local and environment-specific."); |
| 28 | + println!(); |
| 29 | + |
| 30 | + let exe = std::env::current_exe()?; |
| 31 | + |
| 32 | + // Measure agy-ct run |
| 33 | + println!("Running: agy-ct run..."); |
| 34 | + let start_run = Instant::now(); |
| 35 | + let run_status = Command::new(&exe).arg("run").status()?; |
| 36 | + let run_ms = start_run.elapsed().as_millis() as u64; |
| 37 | + |
| 38 | + if !run_status.success() { |
| 39 | + return Err(anyhow!("agy-ct run failed during benchmark")); |
| 40 | + } |
| 41 | + println!(" [PASS] agy-ct run ({} ms)", run_ms); |
| 42 | + |
| 43 | + // Measure agy-ct context all |
| 44 | + println!("Running: agy-ct context all..."); |
| 45 | + let start_context = Instant::now(); |
| 46 | + let context_status = Command::new(&exe).args(["context", "all"]).status()?; |
| 47 | + let context_all_ms = start_context.elapsed().as_millis() as u64; |
| 48 | + |
| 49 | + if !context_status.success() { |
| 50 | + return Err(anyhow!("agy-ct context all failed during benchmark")); |
| 51 | + } |
| 52 | + println!(" [PASS] agy-ct context all ({} ms)", context_all_ms); |
| 53 | + |
| 54 | + // Verify latest.json parses |
| 55 | + let reports_dir = Path::new("../reports"); |
| 56 | + let latest_json_path = if reports_dir.exists() { |
| 57 | + reports_dir.join("latest.json") |
| 58 | + } else { |
| 59 | + Path::new("reports/latest.json").to_path_buf() |
| 60 | + }; |
| 61 | + |
| 62 | + let latest_report_valid = if latest_json_path.exists() { |
| 63 | + let content = fs::read_to_string(&latest_json_path)?; |
| 64 | + serde_json::from_str::<serde_json::Value>(&content).is_ok() |
| 65 | + } else { |
| 66 | + false |
| 67 | + }; |
| 68 | + println!( |
| 69 | + " [PASS] reports/latest.json parses successfully: {}", |
| 70 | + latest_report_valid |
| 71 | + ); |
| 72 | + |
| 73 | + // Verify context_render.txt size |
| 74 | + let spark_dir = Path::new("../artifacts/spark"); |
| 75 | + let render_path = if spark_dir.exists() { |
| 76 | + spark_dir.join("context_render.txt") |
| 77 | + } else { |
| 78 | + Path::new("artifacts/spark/context_render.txt").to_path_buf() |
| 79 | + }; |
| 80 | + |
| 81 | + let context_render_bytes = if render_path.exists() { |
| 82 | + fs::metadata(&render_path)?.len() |
| 83 | + } else { |
| 84 | + 0 |
| 85 | + }; |
| 86 | + println!( |
| 87 | + " [PASS] artifacts/spark/context_render.txt size: {} bytes", |
| 88 | + context_render_bytes |
| 89 | + ); |
| 90 | + |
| 91 | + let status = if run_status.success() |
| 92 | + && context_status.success() |
| 93 | + && latest_report_valid |
| 94 | + && context_render_bytes > 0 |
| 95 | + { |
| 96 | + "PASS" |
| 97 | + } else { |
| 98 | + "FAIL" |
| 99 | + }; |
| 100 | + |
| 101 | + let report = BenchmarkReport { |
| 102 | + phase: "6J".to_string(), |
| 103 | + status: status.to_string(), |
| 104 | + run_ms, |
| 105 | + context_all_ms, |
| 106 | + latest_report_valid, |
| 107 | + performance_report_valid: true, |
| 108 | + context_render_bytes, |
| 109 | + checked_artifacts: vec![ |
| 110 | + "artifacts/spark/extraction.spkg".to_string(), |
| 111 | + "artifacts/spark/context.json".to_string(), |
| 112 | + "artifacts/spark/context_render.txt".to_string(), |
| 113 | + "reports/latest.json".to_string(), |
| 114 | + "reports/performance_baseline.json".to_string(), |
| 115 | + ], |
| 116 | + notes: "Performance baseline measured on local validation environment. No performance optimization was performed in this phase. Measurements are local and environment-specific.".to_string(), |
| 117 | + }; |
| 118 | + |
| 119 | + let dest_dir = Path::new("../reports"); |
| 120 | + let dest_file = if dest_dir.exists() || fs::create_dir_all(dest_dir).is_ok() { |
| 121 | + dest_dir.join("performance_baseline.json") |
| 122 | + } else { |
| 123 | + fs::create_dir_all("reports")?; |
| 124 | + Path::new("reports/performance_baseline.json").to_path_buf() |
| 125 | + }; |
| 126 | + |
| 127 | + let serialized = serde_json::to_string_pretty(&report)?; |
| 128 | + fs::write(&dest_file, serialized)?; |
| 129 | + |
| 130 | + println!(); |
| 131 | + println!("benchmark result: {}", status); |
| 132 | + |
| 133 | + if status == "PASS" { |
| 134 | + Ok(()) |
| 135 | + } else { |
| 136 | + Err(anyhow!("Benchmark validation checks failed")) |
| 137 | + } |
| 138 | +} |
0 commit comments