scx_pandemonium/cli/
report.rs1use anyhow::Result;
2
3use super::LOG_DIR;
4
5fn chrono_stamp() -> String {
6 let output = std::process::Command::new("date")
7 .arg("+%Y%m%d-%H%M%S")
8 .output()
9 .ok();
10 match output {
11 Some(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).trim().to_string(),
12 _ => "unknown".to_string(),
13 }
14}
15
16pub fn save_report(content: &str, prefix: &str) -> Result<String> {
17 std::fs::create_dir_all(LOG_DIR)?;
18 let stamp = chrono_stamp();
19 let path = format!("{}/{}-{}.log", LOG_DIR, prefix, stamp);
20 std::fs::write(&path, content)?;
21 Ok(path)
22}
23
24pub fn mean_stdev(values: &[f64]) -> (f64, f64) {
25 let n = values.len();
26 if n == 0 {
27 return (0.0, 0.0);
28 }
29 let m: f64 = values.iter().sum::<f64>() / n as f64;
30 if n == 1 {
31 return (m, 0.0);
32 }
33 let variance: f64 = values.iter().map(|x| (x - m).powi(2)).sum::<f64>() / (n - 1) as f64;
34 (m, variance.sqrt())
35}
36
37pub fn percentile(sorted_vals: &[f64], p: f64) -> f64 {
38 if sorted_vals.is_empty() {
39 return 0.0;
40 }
41 let idx = (sorted_vals.len() as f64 * p / 100.0) as usize;
42 let idx = idx.min(sorted_vals.len() - 1);
43 sorted_vals[idx]
44}
45
46pub fn format_delta(delta_pct: f64, label: &str) -> String {
47 if delta_pct < 0.0 {
48 format!(
49 "{} DELTA: {:+.1}% (PANDEMONIUM IS {:.1}% FASTER)",
50 label,
51 delta_pct,
52 delta_pct.abs()
53 )
54 } else if delta_pct > 0.0 {
55 format!(
56 "{} DELTA: {:+.1}% (PANDEMONIUM IS {:.1}% SLOWER)",
57 label, delta_pct, delta_pct
58 )
59 } else {
60 format!("{} DELTA: 0.0% (NO DIFFERENCE)", label)
61 }
62}
63
64pub fn format_latency_delta(delta_us: f64, label: &str) -> String {
65 if delta_us < 0.0 {
66 format!(
67 "{} LATENCY DELTA: {:+.0}us (PANDEMONIUM IS {:.0}us BETTER)",
68 label,
69 delta_us,
70 delta_us.abs()
71 )
72 } else if delta_us > 0.0 {
73 format!(
74 "{} LATENCY DELTA: {:+.0}us (PANDEMONIUM IS {:.0}us WORSE)",
75 label, delta_us, delta_us
76 )
77 } else {
78 format!("{} LATENCY DELTA: 0us (SAME)", label)
79 }
80}