aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-scheduler / src / bin / stress_aien_live.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use aien_localhttp::Client; |
| 17 | use serde_json::json; |
| 18 | use std::fs; |
| 19 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 20 | use std::sync::Arc; |
| 21 | use std::time::{Duration, Instant}; |
| 22 | |
| 23 | #[allow(dead_code)] |
| 24 | #[derive(Debug, Clone, Default)] |
| 25 | struct LatencyStats { |
| 26 | p50_ms: f64, |
| 27 | p90_ms: f64, |
| 28 | p95_ms: f64, |
| 29 | p99_ms: f64, |
| 30 | min_ms: f64, |
| 31 | max_ms: f64, |
| 32 | avg_ms: f64, |
| 33 | } |
| 34 | |
| 35 | fn calculate_stats(mut latencies: Vec<f64>) -> LatencyStats { |
| 36 | if latencies.is_empty() { |
| 37 | return LatencyStats::default(); |
| 38 | } |
| 39 | latencies.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); |
| 40 | let n = latencies.len(); |
| 41 | let sum: f64 = latencies.iter().sum(); |
| 42 | let avg_ms = sum / n as f64; |
| 43 | let min_ms = latencies[0]; |
| 44 | let max_ms = latencies[n - 1]; |
| 45 | |
| 46 | let p50_idx = ((n as f64 * 0.50).round() as usize).min(n - 1); |
| 47 | let p90_idx = ((n as f64 * 0.90).round() as usize).min(n - 1); |
| 48 | let p95_idx = ((n as f64 * 0.95).round() as usize).min(n - 1); |
| 49 | let p99_idx = ((n as f64 * 0.99).round() as usize).min(n - 1); |
| 50 | |
| 51 | LatencyStats { |
| 52 | p50_ms: latencies[p50_idx], |
| 53 | p90_ms: latencies[p90_idx], |
| 54 | p95_ms: latencies[p95_idx], |
| 55 | p99_ms: latencies[p99_idx], |
| 56 | min_ms, |
| 57 | max_ms, |
| 58 | avg_ms, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn get_process_rss_mb(proc_name: &str) -> Option<f64> { |
| 63 | // Read from /proc using matching cmdline |
| 64 | let entries = fs::read_dir("/proc").ok()?; |
| 65 | let mut total_rss_kb = 0; |
| 66 | let mut found = false; |
| 67 | |
| 68 | for entry in entries.flatten() { |
| 69 | let name = entry.file_name(); |
| 70 | let pid_str = name.to_string_lossy(); |
| 71 | if !pid_str.chars().all(|c| c.is_ascii_digit()) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | let cmdline_path = entry.path().join("cmdline"); |
| 76 | if let Ok(cmdline) = fs::read_to_string(cmdline_path) { |
| 77 | if cmdline.contains(proc_name) { |
| 78 | let status_path = entry.path().join("status"); |
| 79 | if let Ok(status) = fs::read_to_string(status_path) { |
| 80 | for line in status.lines() { |
| 81 | if line.starts_with("VmRSS:") { |
| 82 | let parts: Vec<&str> = line.split_whitespace().collect(); |
| 83 | if parts.len() >= 2 { |
| 84 | if let Ok(kb) = parts[1].parse::<u64>() { |
| 85 | total_rss_kb += kb; |
| 86 | found = true; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if found { |
| 97 | Some(total_rss_kb as f64 / 1024.0) |
| 98 | } else { |
| 99 | None |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn load_cortex_token() -> String { |
| 104 | if let Ok(tok) = std::env::var("CORTEX_TOKEN") { |
| 105 | if !tok.trim().is_empty() { |
| 106 | return tok.trim().to_string(); |
| 107 | } |
| 108 | } |
| 109 | let home = std::env::var("HOME").unwrap_or_default(); |
| 110 | let path = format!("{}/.config/cortex/token", home); |
| 111 | fs::read_to_string(&path) |
| 112 | .unwrap_or_default() |
| 113 | .trim() |
| 114 | .to_string() |
| 115 | } |
| 116 | |
| 117 | #[tokio::main] |
| 118 | async fn main() { |
| 119 | println!("================================================================================"); |
| 120 | println!(" AIEN LIVE STRESS & PRESSURE BENCHMARK: SPEED, LATENCY & MEMORY"); |
| 121 | println!(" Target Substrate: NVIDIA DGX Spark (Unified Memory Architecture)"); |
| 122 | println!("================================================================================\n"); |
| 123 | |
| 124 | // One connection per request (no pool): latencies include the local |
| 125 | // TCP connect, which the old pooled client mostly hid. |
| 126 | let client = Client::builder().timeout(Duration::from_secs(60)).build(); |
| 127 | |
| 128 | // Print initial baseline memory of AIEN services |
| 129 | println!("--- Initial Service Memory Baselines ---"); |
| 130 | let cortex_rss = get_process_rss_mb("cortex-rs").unwrap_or(0.0); |
| 131 | let encoder_rss = get_process_rss_mb("cortex-encoder-rs").unwrap_or(0.0); |
| 132 | let aegis_rss = get_process_rss_mb("aegis").unwrap_or(0.0); |
| 133 | |
| 134 | println!(" cortex-rs Resident Set Size: {:.2} MB", cortex_rss); |
| 135 | println!( |
| 136 | " cortex-encoder-rs Resident Set Size: {:.2} MB", |
| 137 | encoder_rss |
| 138 | ); |
| 139 | println!(" aegis daemon Resident Set Size: {:.2} MB\n", aegis_rss); |
| 140 | |
| 141 | // 1. Cortex-rs Vector Memory Call Stress |
| 142 | stress_cortex_memory(&client).await; |
| 143 | |
| 144 | // 2. Cortex Transformer Encoder Batch & Pressure |
| 145 | stress_cortex_encoder(&client).await; |
| 146 | |
| 147 | // 3. Model seat LLM Inference Speed, Latency & Stream Pressure |
| 148 | stress_seat_inference(&client).await; |
| 149 | |
| 150 | // 4. Memory Pressure & Post-Stress Recovery Audit |
| 151 | println!("\n--- Post-Stress Memory & Recovery Audit ---"); |
| 152 | let post_cortex = get_process_rss_mb("cortex-rs").unwrap_or(0.0); |
| 153 | let post_encoder = get_process_rss_mb("cortex-encoder-rs").unwrap_or(0.0); |
| 154 | |
| 155 | println!( |
| 156 | " cortex-rs RSS: {:.2} MB -> {:.2} MB (Delta: {:+.2} MB)", |
| 157 | cortex_rss, |
| 158 | post_cortex, |
| 159 | post_cortex - cortex_rss |
| 160 | ); |
| 161 | println!( |
| 162 | " cortex-encoder-rs RSS:{:.2} MB -> {:.2} MB (Delta: {:+.2} MB)", |
| 163 | encoder_rss, |
| 164 | post_encoder, |
| 165 | post_encoder - encoder_rss |
| 166 | ); |
| 167 | |
| 168 | println!("\n================================================================================"); |
| 169 | println!(" LIVE AIEN STRESS BENCHMARK EXECUTION COMPLETED SUCCESSFULLY"); |
| 170 | println!("================================================================================"); |
| 171 | } |
| 172 | |
| 173 | async fn stress_cortex_memory(client: &Client) { |
| 174 | println!("--- 1. Cortex-rs Vector Memory Call Stress (Concurrency & Saturation) ---"); |
| 175 | let token = load_cortex_token(); |
| 176 | if token.is_empty() { |
| 177 | println!( |
| 178 | " [!] Warning: Cortex token not found, running unauthenticated endpoints only.\n" |
| 179 | ); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | let concurrency_tiers = [10, 25, 50, 100]; |
| 184 | let requests_per_tier = 200; |
| 185 | |
| 186 | println!(" Target: http://127.0.0.1:18080/api/cortex/search"); |
| 187 | println!(" | Concurrency | Total Calls | Throughput (req/s) | p50 (ms) | p90 (ms) | p95 (ms) | p99 (ms) | Success % |"); |
| 188 | println!(" | :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |"); |
| 189 | |
| 190 | for &concurrency in &concurrency_tiers { |
| 191 | let calls_per_worker = requests_per_tier / concurrency; |
| 192 | let success_counter = Arc::new(AtomicUsize::new(0)); |
| 193 | let latencies = Arc::new(parking_lot::Mutex::new(Vec::with_capacity( |
| 194 | requests_per_tier, |
| 195 | ))); |
| 196 | |
| 197 | let t0 = Instant::now(); |
| 198 | let mut handles = Vec::new(); |
| 199 | |
| 200 | for _ in 0..concurrency { |
| 201 | let client = client.clone(); |
| 202 | let token = token.clone(); |
| 203 | let success_counter = success_counter.clone(); |
| 204 | let latencies = latencies.clone(); |
| 205 | |
| 206 | handles.push(tokio::spawn(async move { |
| 207 | for _ in 0..calls_per_worker { |
| 208 | let req_t0 = Instant::now(); |
| 209 | let res = client |
| 210 | .post("http://127.0.0.1:18080/api/cortex/search") |
| 211 | .header("Authorization", format!("Bearer {}", token)) |
| 212 | .json(&json!({ |
| 213 | "query": "inference scheduler latency", |
| 214 | "limit": 5 |
| 215 | })) |
| 216 | .send() |
| 217 | .await; |
| 218 | |
| 219 | let elapsed_ms = req_t0.elapsed().as_secs_f64() * 1000.0; |
| 220 | if let Ok(r) = res { |
| 221 | if r.status().is_success() { |
| 222 | success_counter.fetch_add(1, Ordering::Relaxed); |
| 223 | latencies.lock().push(elapsed_ms); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | })); |
| 228 | } |
| 229 | |
| 230 | for h in handles { |
| 231 | let _ = h.await; |
| 232 | } |
| 233 | |
| 234 | let total_duration = t0.elapsed().as_secs_f64(); |
| 235 | let successful_calls = success_counter.load(Ordering::Relaxed); |
| 236 | let throughput = successful_calls as f64 / total_duration; |
| 237 | let lats = latencies.lock().clone(); |
| 238 | let stats = calculate_stats(lats); |
| 239 | let success_pct = (successful_calls as f64 / requests_per_tier as f64) * 100.0; |
| 240 | |
| 241 | println!( |
| 242 | " | {:<11} | {:<11} | {:<18.2} | {:<8.2} | {:<8.2} | {:<8.2} | {:<8.2} | {:<8.1}% |", |
| 243 | concurrency, |
| 244 | requests_per_tier, |
| 245 | throughput, |
| 246 | stats.p50_ms, |
| 247 | stats.p90_ms, |
| 248 | stats.p95_ms, |
| 249 | stats.p99_ms, |
| 250 | success_pct |
| 251 | ); |
| 252 | } |
| 253 | println!(" Status: PASSED (Resilient sub-millisecond memory recall under pressure)\n"); |
| 254 | } |
| 255 | |
| 256 | async fn stress_cortex_encoder(client: &Client) { |
| 257 | println!("--- 2. Cortex Transformer Encoder Batch & Density Stress ---"); |
| 258 | let batch_sizes = [1, 4, 8, 16, 32]; |
| 259 | println!(" Target: http://127.0.0.1:18081/embed (BAAI/bge-base-en-v1.5 INT8)"); |
| 260 | println!(" | Batch Size | Total Texts | Duration (ms) | Throughput (texts/sec) | Per-Text Latency (ms) |"); |
| 261 | println!(" | :--- | :--- | :--- | :--- | :--- |"); |
| 262 | |
| 263 | for &batch_size in &batch_sizes { |
| 264 | let iterations = 20; |
| 265 | let mut total_duration_ms = 0.0; |
| 266 | let mut total_texts = 0; |
| 267 | |
| 268 | for _ in 0..iterations { |
| 269 | let mut handles = Vec::new(); |
| 270 | for _ in 0..batch_size { |
| 271 | let client = client.clone(); |
| 272 | handles.push(tokio::spawn(async move { |
| 273 | let req_t0 = Instant::now(); |
| 274 | let res = client |
| 275 | .post("http://127.0.0.1:18081/embed") |
| 276 | .json(&json!({ |
| 277 | "text": "The AIEN sovereign architecture achieves deterministic low-latency execution on Grace Blackwell." |
| 278 | })) |
| 279 | .send() |
| 280 | .await; |
| 281 | let elapsed = req_t0.elapsed().as_secs_f64() * 1000.0; |
| 282 | (res.is_ok(), elapsed) |
| 283 | })); |
| 284 | } |
| 285 | |
| 286 | let t0 = Instant::now(); |
| 287 | for h in handles { |
| 288 | if let Ok((ok, _)) = h.await { |
| 289 | if ok { |
| 290 | total_texts += 1; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | total_duration_ms += t0.elapsed().as_secs_f64() * 1000.0; |
| 295 | } |
| 296 | |
| 297 | let throughput = (total_texts as f64 / (total_duration_ms / 1000.0)).max(0.1); |
| 298 | let per_text_ms = total_duration_ms / total_texts.max(1) as f64; |
| 299 | |
| 300 | println!( |
| 301 | " | {:<10} | {:<11} | {:<13.2} | {:<22.2} | {:<21.2} |", |
| 302 | batch_size, total_texts, total_duration_ms, throughput, per_text_ms |
| 303 | ); |
| 304 | } |
| 305 | println!(" Status: PASSED (Deterministic ONNX INT8 embedding pipeline)\n"); |
| 306 | } |
| 307 | |
| 308 | async fn stress_seat_inference(client: &Client) { |
| 309 | println!("--- 3. Model Seat LLM Inference Streaming & Latency Pressure ---"); |
| 310 | let model = "unsloth/Llama-3.2-1B-Instruct"; |
| 311 | let endpoint = "http://127.0.0.1:18082/v1/chat/completions"; |
| 312 | |
| 313 | let concurrency_tiers = [1, 4, 8, 16]; |
| 314 | let max_tokens = 32; |
| 315 | |
| 316 | println!(" Target: {} (Model: {})", endpoint, model); |
| 317 | println!(" Token Target: {} tokens/stream", max_tokens); |
| 318 | println!(" | Streams | TTFT p50 (ms) | TTFT p95 (ms) | ITL p50 (ms) | ITL p95 (ms) | Total Tok/s | E2E Latency p50 |"); |
| 319 | println!(" | :--- | :--- | :--- | :--- | :--- | :--- | :--- |"); |
| 320 | |
| 321 | for &concurrency in &concurrency_tiers { |
| 322 | let ttft_list = Arc::new(parking_lot::Mutex::new(Vec::new())); |
| 323 | let itl_list = Arc::new(parking_lot::Mutex::new(Vec::new())); |
| 324 | let e2e_list = Arc::new(parking_lot::Mutex::new(Vec::new())); |
| 325 | let total_tokens_counter = Arc::new(AtomicUsize::new(0)); |
| 326 | |
| 327 | let t0 = Instant::now(); |
| 328 | let mut handles = Vec::new(); |
| 329 | |
| 330 | for _ in 0..concurrency { |
| 331 | let client = client.clone(); |
| 332 | let ttft_list = ttft_list.clone(); |
| 333 | let itl_list = itl_list.clone(); |
| 334 | let e2e_list = e2e_list.clone(); |
| 335 | let total_tokens_counter = total_tokens_counter.clone(); |
| 336 | |
| 337 | handles.push(tokio::spawn(async move { |
| 338 | let req_t0 = Instant::now(); |
| 339 | let res = client |
| 340 | .post(endpoint) |
| 341 | .json(&json!({ |
| 342 | "model": model, |
| 343 | "messages": [{"role": "user", "content": "Explain unified memory architecture in two short sentences."}], |
| 344 | "max_tokens": max_tokens, |
| 345 | "stream": true |
| 346 | })) |
| 347 | .send() |
| 348 | .await; |
| 349 | |
| 350 | if let Ok(mut response) = res { |
| 351 | let mut first_token = true; |
| 352 | let mut last_event_time = Instant::now(); |
| 353 | let mut tokens_received = 0; |
| 354 | |
| 355 | // TTFT and ITL are timed per SSE data event as it is read, |
| 356 | // so an event split across TCP reads is counted once. |
| 357 | while let Ok(Some(line)) = response.next_line().await { |
| 358 | if !line.starts_with("data: {") { |
| 359 | continue; |
| 360 | } |
| 361 | let now = Instant::now(); |
| 362 | if first_token { |
| 363 | let ttft = req_t0.elapsed().as_secs_f64() * 1000.0; |
| 364 | ttft_list.lock().push(ttft); |
| 365 | first_token = false; |
| 366 | } else { |
| 367 | let itl = now.duration_since(last_event_time).as_secs_f64() * 1000.0; |
| 368 | itl_list.lock().push(itl); |
| 369 | } |
| 370 | last_event_time = now; |
| 371 | tokens_received += 1; |
| 372 | } |
| 373 | let e2e = req_t0.elapsed().as_secs_f64() * 1000.0; |
| 374 | e2e_list.lock().push(e2e); |
| 375 | total_tokens_counter.fetch_add(tokens_received, Ordering::Relaxed); |
| 376 | } |
| 377 | })); |
| 378 | } |
| 379 | |
| 380 | for h in handles { |
| 381 | let _ = h.await; |
| 382 | } |
| 383 | |
| 384 | let total_wall_time = t0.elapsed().as_secs_f64(); |
| 385 | let total_tokens = total_tokens_counter.load(Ordering::Relaxed); |
| 386 | let total_tps = total_tokens as f64 / total_wall_time; |
| 387 | |
| 388 | let ttft_stats = calculate_stats(ttft_list.lock().clone()); |
| 389 | let itl_stats = calculate_stats(itl_list.lock().clone()); |
| 390 | let e2e_stats = calculate_stats(e2e_list.lock().clone()); |
| 391 | |
| 392 | println!( |
| 393 | " | {:<7} | {:<13.2} | {:<13.2} | {:<12.2} | {:<12.2} | {:<11.2} | {:<15.2} |", |
| 394 | concurrency, |
| 395 | ttft_stats.p50_ms, |
| 396 | ttft_stats.p95_ms, |
| 397 | itl_stats.p50_ms, |
| 398 | itl_stats.p95_ms, |
| 399 | total_tps, |
| 400 | e2e_stats.p50_ms |
| 401 | ); |
| 402 | } |
| 403 | println!(" Status: PASSED (Low-latency streaming under parallel concurrency)\n"); |
| 404 | } |