rigspark_cli/
performance.rs1use serde::Serialize;
2use std::io;
3
4#[derive(Clone, Serialize)]
5#[serde(rename_all = "camelCase")]
6pub struct Sample {
7 pub elapsed_ms: f64,
8 pub peak_rss_bytes: Option<u64>,
9}
10
11#[derive(Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct Limits {
14 pub p90_ms: f64,
15 pub peak_rss_bytes: u64,
16 pub executable_bytes: u64,
17}
18
19impl Default for Limits {
20 fn default() -> Self {
21 Self {
22 p90_ms: 100.0,
23 peak_rss_bytes: 64 * 1024 * 1024,
24 executable_bytes: 32 * 1024 * 1024,
25 }
26 }
27}
28
29#[derive(Serialize)]
30#[serde(rename_all = "camelCase")]
31pub struct Report {
32 pub median_ms: f64,
33 pub p90_ms: f64,
34 pub peak_rss_bytes: Option<u64>,
35 pub executable_bytes: u64,
36 pub samples: Vec<Sample>,
37 pub failures: Vec<String>,
38}
39
40pub fn evaluate(samples: &[Sample], executable_bytes: u64, limits: &Limits) -> io::Result<Report> {
41 if samples.len() < 20
42 || samples.len() > 1000
43 || executable_bytes == 0
44 || !limits.p90_ms.is_finite()
45 || limits.p90_ms <= 0.0
46 || limits.peak_rss_bytes == 0
47 || limits.executable_bytes == 0
48 || samples.iter().any(|sample| {
49 !sample.elapsed_ms.is_finite()
50 || sample.elapsed_ms <= 0.0
51 || sample.peak_rss_bytes == Some(0)
52 })
53 {
54 return Err(io::Error::other("invalid performance samples or limits"));
55 }
56 let mut durations: Vec<_> = samples.iter().map(|sample| sample.elapsed_ms).collect();
57 durations.sort_by(f64::total_cmp);
58 let median_ms = durations[durations.len().div_ceil(2) - 1];
59 let p90_ms = durations[(durations.len() * 9).div_ceil(10) - 1];
60 let peak_rss_bytes = samples.iter().try_fold(0, |maximum, sample| {
61 sample.peak_rss_bytes.map(|value| maximum.max(value))
62 });
63 let mut failures = Vec::new();
64 if p90_ms > limits.p90_ms {
65 failures.push(format!("p90 {p90_ms:.3}ms exceeds {}ms", limits.p90_ms));
66 }
67 match peak_rss_bytes {
68 None => failures.push("peak RSS unknown; memory budget is not verified".into()),
69 Some(bytes) if bytes > limits.peak_rss_bytes => failures.push(format!(
70 "peak RSS {bytes} exceeds {} bytes",
71 limits.peak_rss_bytes
72 )),
73 _ => (),
74 }
75 if executable_bytes > limits.executable_bytes {
76 failures.push(format!(
77 "executable {executable_bytes} exceeds {} bytes",
78 limits.executable_bytes
79 ));
80 }
81 Ok(Report {
82 median_ms,
83 p90_ms,
84 peak_rss_bytes,
85 executable_bytes,
86 samples: samples.to_vec(),
87 failures,
88 })
89}
90
91pub fn peak_rss(platform: &str, stderr: &str) -> Option<u64> {
92 let values: Vec<_> = stderr
93 .lines()
94 .filter_map(|line| {
95 let raw = match platform {
96 "linux" => line.strip_prefix("LLMUP_PEAK_RSS_KIB="),
97 "macos" => line.trim().strip_suffix("maximum resident set size"),
98 _ => None,
99 }?;
100 Some(raw.trim().parse::<u64>().ok())
101 })
102 .collect();
103 if values.len() != 1 {
104 return None;
105 }
106 let value = values[0]?;
107 let bytes = if platform == "linux" {
108 value.checked_mul(1024)?
109 } else {
110 value
111 };
112 (bytes > 0).then_some(bytes)
113}