benchmarks-mv
Measurement harnesses and evidence bundles for AIEN on the NVIDIA DGX Spark GB10. Speed is reported against the machine's physical limit and my own previous run. Attachment of AienOS.
Brought by drakestapleton
benchmarks-mv / src / main.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_benchmarks::{harness, models, svg_chart}; |
| 17 | |
| 18 | use clap::{Parser, Subcommand}; |
| 19 | use models::BenchmarkData; |
| 20 | use std::fs; |
| 21 | use std::path::Path; |
| 22 | |
| 23 | #[derive(Parser)] |
| 24 | #[command(name = "aien-benchmarks")] |
| 25 | #[command(about = "Sovereign Systems Benchmark Suite CLI & Measurement Harness")] |
| 26 | struct Cli { |
| 27 | #[command(subcommand)] |
| 28 | command: Option<Commands>, |
| 29 | |
| 30 | #[arg(short, long, default_value = "data/benchmarks_latest.json")] |
| 31 | input: String, |
| 32 | } |
| 33 | |
| 34 | #[derive(Subcommand)] |
| 35 | enum Commands { |
| 36 | /// Execute live measurement sweep, generate benchmark dataset, and render SVG charts |
| 37 | Measure { |
| 38 | #[arg(short, long, default_value_t = 10)] |
| 39 | concurrency: u32, |
| 40 | |
| 41 | #[arg(short, long, default_value_t = 500)] |
| 42 | requests: u32, |
| 43 | |
| 44 | #[arg(short, long, default_value_t = 20)] |
| 45 | warmup: u32, |
| 46 | |
| 47 | #[arg(long, default_value = "data/benchmarks_latest.json")] |
| 48 | output_json: String, |
| 49 | |
| 50 | #[arg(long, default_value = "assets")] |
| 51 | output_assets: String, |
| 52 | |
| 53 | #[arg(long)] |
| 54 | skip_standalone: bool, |
| 55 | |
| 56 | #[arg(long)] |
| 57 | skip_live: bool, |
| 58 | |
| 59 | /// Skip the streaming LLM endpoint |
| 60 | #[arg(long)] |
| 61 | skip_llm: bool, |
| 62 | |
| 63 | #[command(flatten)] |
| 64 | llm: LlmArgs, |
| 65 | }, |
| 66 | /// Measure one streaming LLM endpoint and write an aien-bench-record/1 JSON record plus raw samples |
| 67 | LlmStream { |
| 68 | #[command(flatten)] |
| 69 | llm: LlmArgs, |
| 70 | |
| 71 | /// Directory for the record and its samples file (relative to the working directory) |
| 72 | #[arg(long, default_value = "records")] |
| 73 | out_dir: String, |
| 74 | }, |
| 75 | /// Render SVG charts to assets/ directory |
| 76 | Generate { |
| 77 | #[arg(short, long, default_value = "assets")] |
| 78 | output: String, |
| 79 | }, |
| 80 | /// Print terminal ASCII summary table |
| 81 | Report, |
| 82 | /// Verify all metrics against their budget invariants |
| 83 | Verify, |
| 84 | /// Synchronize benchmark claims in documentation files from benchmarks_latest.json |
| 85 | SyncDocs { |
| 86 | /// Target markdown documentation files to synchronize |
| 87 | #[arg(short, long)] |
| 88 | target: Vec<String>, |
| 89 | |
| 90 | /// Check mode: exit with non-zero code if documentation differs from data |
| 91 | #[arg(long)] |
| 92 | check: bool, |
| 93 | }, |
| 94 | } |
| 95 | |
| 96 | /// Streaming LLM endpoint settings (shared by `measure` and `llm-stream`). Defaults are the |
| 97 | /// endpoint and model id the old hardcoded harness used; engine and precision default to |
| 98 | /// "unlabeled" so a record never claims hardware or numerics nobody stated. |
| 99 | #[derive(clap::Args, Clone, Debug)] |
| 100 | struct LlmArgs { |
| 101 | /// Endpoint host |
| 102 | #[arg(long, default_value = "127.0.0.1")] |
| 103 | llm_host: String, |
| 104 | /// Endpoint port |
| 105 | #[arg(long, default_value_t = 18006)] |
| 106 | llm_port: u16, |
| 107 | /// Request path (OpenAI-compatible chat completions) |
| 108 | #[arg(long, default_value = "/v1/chat/completions")] |
| 109 | llm_path: String, |
| 110 | /// Model id sent in the request and reported in the record |
| 111 | #[arg(long, default_value = "atlas-lightning-omni")] |
| 112 | llm_model: String, |
| 113 | /// Engine label reported in the record (what serves the endpoint) |
| 114 | #[arg(long, default_value = "unlabeled")] |
| 115 | llm_engine: String, |
| 116 | /// Precision label reported in the record (for example BF16); "unlabeled" when omitted |
| 117 | #[arg(long)] |
| 118 | llm_precision: Option<String>, |
| 119 | /// Context length label reported in the record (tokens) |
| 120 | #[arg(long)] |
| 121 | llm_context_length: Option<u64>, |
| 122 | /// max_tokens sent in each request |
| 123 | #[arg(long, default_value_t = 20)] |
| 124 | llm_max_tokens: u32, |
| 125 | /// Prompt sent as the single user message |
| 126 | #[arg( |
| 127 | long, |
| 128 | default_value = "Explain unified memory architecture in ten words." |
| 129 | )] |
| 130 | llm_prompt: String, |
| 131 | /// Measured requests (N); p95 is reported only when at least 20 succeed |
| 132 | #[arg(long, default_value_t = 1)] |
| 133 | llm_requests: u32, |
| 134 | /// Requests in flight at once |
| 135 | #[arg(long, default_value_t = 1)] |
| 136 | llm_concurrency: u32, |
| 137 | /// Warmup requests run first and discarded |
| 138 | #[arg(long, default_value_t = 1)] |
| 139 | llm_warmup: u32, |
| 140 | /// Do not send stream_options.include_usage |
| 141 | #[arg(long)] |
| 142 | llm_no_usage_request: bool, |
| 143 | /// TCP connect timeout in seconds |
| 144 | #[arg(long, default_value_t = 5)] |
| 145 | llm_connect_timeout_secs: u64, |
| 146 | /// Longest wait for any single socket read, in seconds (a timeout fails the request) |
| 147 | #[arg(long, default_value_t = 20)] |
| 148 | llm_read_timeout_secs: u64, |
| 149 | /// llm-stream: the serving binary the operator says answers on the endpoint; its sha256 is |
| 150 | /// bound into the record as backend.binary_sha256 (DRA-166). Not checked against the process |
| 151 | /// that answers, and the record says so in its limitations. |
| 152 | #[arg(long)] |
| 153 | llm_binary: Option<String>, |
| 154 | /// llm-stream: the model checkpoint the server loaded, a file or a directory of shards; its |
| 155 | /// sha256 (for a directory, the sha256 of its sorted per-file manifest) is bound into the |
| 156 | /// record as backend.model_sha256 (DRA-166) |
| 157 | #[arg(long)] |
| 158 | llm_model_file: Option<String>, |
| 159 | } |
| 160 | |
| 161 | impl LlmArgs { |
| 162 | fn to_config(&self) -> harness::llm_stream::StreamConfig { |
| 163 | harness::llm_stream::StreamConfig { |
| 164 | host: self.llm_host.clone(), |
| 165 | port: self.llm_port, |
| 166 | path: self.llm_path.clone(), |
| 167 | model: self.llm_model.clone(), |
| 168 | engine: self.llm_engine.clone(), |
| 169 | precision: self.llm_precision.clone(), |
| 170 | context_length: self.llm_context_length, |
| 171 | max_tokens: self.llm_max_tokens, |
| 172 | prompt: self.llm_prompt.clone(), |
| 173 | requests: self.llm_requests, |
| 174 | concurrency: self.llm_concurrency, |
| 175 | warmup: self.llm_warmup, |
| 176 | request_usage: !self.llm_no_usage_request, |
| 177 | connect_timeout: std::time::Duration::from_secs(self.llm_connect_timeout_secs), |
| 178 | read_timeout: std::time::Duration::from_secs(self.llm_read_timeout_secs), |
| 179 | binary_sha256: None, |
| 180 | model_sha256: None, |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | fn load_data(path: &str) -> BenchmarkData { |
| 186 | let raw = fs::read_to_string(path) |
| 187 | .unwrap_or_else(|e| panic!("Failed to read benchmark data file {}: {}", path, e)); |
| 188 | serde_json::from_str(&raw).unwrap_or_else(|e| panic!("Failed to parse benchmark JSON: {}", e)) |
| 189 | } |
| 190 | |
| 191 | fn save_data(path: &str, data: &BenchmarkData) { |
| 192 | if let Some(parent) = Path::new(path).parent() { |
| 193 | fs::create_dir_all(parent).expect("Failed to create parent directory for dataset"); |
| 194 | } |
| 195 | let json = serde_json::to_string_pretty(data).expect("Failed to serialize benchmark data"); |
| 196 | fs::write(path, json).expect("Failed to write benchmark data"); |
| 197 | println!(" [OK] Saved benchmark dataset to {}", path); |
| 198 | } |
| 199 | |
| 200 | fn render_all_svgs(data: &BenchmarkData, output_dir: &str) { |
| 201 | let out_dir = Path::new(output_dir); |
| 202 | fs::create_dir_all(out_dir).expect("Failed to create output directory"); |
| 203 | |
| 204 | let mem_svg = svg_chart::generate_memory_chart(data); |
| 205 | let mem_path = out_dir.join("memory_comparison.svg"); |
| 206 | fs::write(&mem_path, mem_svg).expect("Failed to write memory SVG"); |
| 207 | println!(" [OK] Rendered {}", mem_path.display()); |
| 208 | |
| 209 | let lat_svg = svg_chart::generate_latency_chart(data); |
| 210 | let lat_path = out_dir.join("latency_comparison.svg"); |
| 211 | fs::write(&lat_path, lat_svg).expect("Failed to write latency SVG"); |
| 212 | println!(" [OK] Rendered {}", lat_path.display()); |
| 213 | |
| 214 | println!( |
| 215 | "All SVG benchmark charts successfully updated in {}.", |
| 216 | output_dir |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | fn print_report(data: &BenchmarkData) { |
| 221 | println!("================================================================================"); |
| 222 | println!(" AIEN Sovereign Systems Performance Benchmark Report"); |
| 223 | println!( |
| 224 | " Hardware: {} ({} {})", |
| 225 | data.hardware.system, data.hardware.processor, data.hardware.architecture |
| 226 | ); |
| 227 | println!( |
| 228 | " Unified Memory: {} GB | OS: {}", |
| 229 | data.hardware.memory_unified_gb, data.hardware.os |
| 230 | ); |
| 231 | if !data.environment.rustc_version.is_empty() { |
| 232 | println!(" Compiler: {}", data.environment.rustc_version); |
| 233 | println!( |
| 234 | " Kernel: {} | Page Size: {} KB", |
| 235 | data.environment.kernel_version, data.environment.page_size_kb |
| 236 | ); |
| 237 | } |
| 238 | println!(" Timestamp: {}", data.timestamp); |
| 239 | println!("================================================================================\n"); |
| 240 | |
| 241 | println!("--- 1. Memory Resident Set Size (RSS) ---"); |
| 242 | println!( |
| 243 | "{:<28} | {:<32} | {:>10}", |
| 244 | "Service", "Architecture", "RSS (MB)" |
| 245 | ); |
| 246 | println!("{:-<28}-|-{:-<32}-|-{:-<10}", "", "", ""); |
| 247 | for m in &data.memory_rss { |
| 248 | println!( |
| 249 | "{:<28} | {:<32} | {:>10.2}", |
| 250 | m.service, m.architecture, m.rss_mb |
| 251 | ); |
| 252 | } |
| 253 | println!(); |
| 254 | |
| 255 | println!("--- 2. HTTP Gateway Latency (p50 TTFB) & Concurrency Throughput ---"); |
| 256 | println!( |
| 257 | "{:<32} | {:<24} | {:>10} | {:>10} | {:>10}", |
| 258 | "Endpoint", "Engine", "Req/sec", "p50 (ms)", "p95 (ms)" |
| 259 | ); |
| 260 | println!( |
| 261 | "{:-<32}-|-{:-<24}-|-{:-<10}-|-{:-<10}-|-{:-<10}", |
| 262 | "", "", "", "", "" |
| 263 | ); |
| 264 | for l in &data.latency_concurrency { |
| 265 | let label = format!("{} {}", l.service, l.endpoint); |
| 266 | println!( |
| 267 | "{:<32} | {:<24} | {:>10.1} | {:>10.2} | {:>10.2}", |
| 268 | label, l.engine, l.requests_per_sec, l.p50_ms, l.p95_ms |
| 269 | ); |
| 270 | } |
| 271 | println!(); |
| 272 | |
| 273 | println!("--- 3. Local Neural Inference & SIMD Vectors (Grace Blackwell) ---"); |
| 274 | println!( |
| 275 | "{:<42} | {:<32} | {:>10} | {:>10}", |
| 276 | "Workload", "Model / Kernel", "p50 (ms)", "p95 (ms)" |
| 277 | ); |
| 278 | println!("{:-<42}-|-{:-<32}-|-{:-<10}-|-{:-<10}", "", "", "", ""); |
| 279 | for n in &data.neural_inference { |
| 280 | let p50_str = if n.p50_ms < 0.01 { |
| 281 | format!("{:.4}", n.p50_ms) |
| 282 | } else { |
| 283 | format!("{:.2}", n.p50_ms) |
| 284 | }; |
| 285 | let p95_str = match n.p95_ms { |
| 286 | None => "n/a".to_string(), |
| 287 | Some(v) if v < 0.01 => format!("{:.4}", v), |
| 288 | Some(v) => format!("{:.2}", v), |
| 289 | }; |
| 290 | println!( |
| 291 | "{:<42} | {:<32} | {:>10} | {:>10}", |
| 292 | n.workload, n.model, p50_str, p95_str |
| 293 | ); |
| 294 | } |
| 295 | println!( |
| 296 | "Not claimed results (DRA-166): these rows bind no serving binary, model digest, tolerance" |
| 297 | ); |
| 298 | println!( |
| 299 | "or parity verdict. Claimed LLM numbers come only from llm-stream records that pass bench_verify_records." |
| 300 | ); |
| 301 | println!("================================================================================"); |
| 302 | } |
| 303 | |
| 304 | fn main() { |
| 305 | let cli = Cli::parse(); |
| 306 | |
| 307 | match cli.command.unwrap_or(Commands::Report) { |
| 308 | Commands::Measure { |
| 309 | concurrency, |
| 310 | requests, |
| 311 | warmup, |
| 312 | output_json, |
| 313 | output_assets, |
| 314 | skip_standalone, |
| 315 | skip_live, |
| 316 | skip_llm, |
| 317 | llm, |
| 318 | } => { |
| 319 | let config = harness::MeasurementConfig { |
| 320 | concurrency, |
| 321 | requests, |
| 322 | warmup, |
| 323 | skip_standalone, |
| 324 | skip_live, |
| 325 | llm: (!skip_llm).then(|| llm.to_config()), |
| 326 | }; |
| 327 | |
| 328 | let data = match harness::run_measurement_suite(config) { |
| 329 | Ok(d) => d, |
| 330 | Err(e) => { |
| 331 | eprintln!("Measurement sweep error: {}", e); |
| 332 | std::process::exit(1); |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | print_report(&data); |
| 337 | save_data(&output_json, &data); |
| 338 | render_all_svgs(&data, &output_assets); |
| 339 | } |
| 340 | Commands::LlmStream { llm, out_dir } => { |
| 341 | use harness::llm_stream; |
| 342 | let mut cfg = llm.to_config(); |
| 343 | // DRA-166: bind the measured binary and model by content digest before measuring. |
| 344 | for (flag, file, slot) in [ |
| 345 | ("--llm-binary", &llm.llm_binary, &mut cfg.binary_sha256), |
| 346 | ( |
| 347 | "--llm-model-file", |
| 348 | &llm.llm_model_file, |
| 349 | &mut cfg.model_sha256, |
| 350 | ), |
| 351 | ] { |
| 352 | if let Some(f) = file { |
| 353 | match llm_stream::file_sha256(Path::new(f)) { |
| 354 | Ok(h) => *slot = Some(h), |
| 355 | Err(e) => { |
| 356 | eprintln!("llm-stream: {flag}: {e}; refusing to measure"); |
| 357 | std::process::exit(2); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | // Fail before measuring if the run cannot be bound to a commit. |
| 363 | let source = match llm_stream::source_identity() { |
| 364 | Ok(s) => s, |
| 365 | Err(e) => { |
| 366 | eprintln!("llm-stream: {}; refusing to write an unbound record", e); |
| 367 | std::process::exit(1); |
| 368 | } |
| 369 | }; |
| 370 | println!( |
| 371 | "Streaming benchmark: {} (model {}, engine {}), {} request(s), concurrency {}, warmup {}", |
| 372 | cfg.endpoint(), |
| 373 | cfg.model, |
| 374 | cfg.engine, |
| 375 | cfg.requests, |
| 376 | cfg.concurrency, |
| 377 | cfg.warmup |
| 378 | ); |
| 379 | let run = llm_stream::run_stream_benchmark(&cfg); |
| 380 | let summary = llm_stream::summarize(&run.samples, run.wall); |
| 381 | harness::inference_bench::print_stream_summary(&summary); |
| 382 | // Paths in the record are relative to the working directory, the repository root |
| 383 | // that bench_verify_records resolves evidence against by default. |
| 384 | let root = Path::new("."); |
| 385 | match llm_stream::write_run(root, Path::new(&out_dir), &cfg, &run, &summary, &source) { |
| 386 | Ok(path) => { |
| 387 | println!("record: {}", path.display()); |
| 388 | if summary.requests_ok == 0 { |
| 389 | std::process::exit(2); |
| 390 | } |
| 391 | // The record must meet its own contract, binding included (DRA-166). |
| 392 | let defects = fs::read_to_string(&path) |
| 393 | .map_err(|e| e.to_string()) |
| 394 | .and_then(|t| { |
| 395 | serde_json::from_str::<serde_json::Value>(&t).map_err(|e| e.to_string()) |
| 396 | }) |
| 397 | .map(|v| { |
| 398 | let mut out = Vec::new(); |
| 399 | let name = path.to_string_lossy(); |
| 400 | aien_benchmarks::evidence::check_record(root, &name, &v, &mut out); |
| 401 | out |
| 402 | }); |
| 403 | match defects { |
| 404 | Ok(d) if d.is_empty() => {} |
| 405 | Ok(d) => { |
| 406 | for x in &d { |
| 407 | eprintln!("{x}"); |
| 408 | } |
| 409 | // A refused record and its samples are renamed to `*.rejected`, so |
| 410 | // they stay for inspection but are no longer `.json` files that the |
| 411 | // records verifier walks (a later `verify-records` would fail on them). |
| 412 | let samples = path.with_extension("samples.json"); |
| 413 | for f in [&path, &samples] { |
| 414 | let mut to = f.clone().into_os_string(); |
| 415 | to.push(".rejected"); |
| 416 | if let Err(e) = fs::rename(f, &to) { |
| 417 | eprintln!("llm-stream: rename {}: {}", f.display(), e); |
| 418 | std::process::exit(1); |
| 419 | } |
| 420 | } |
| 421 | eprintln!( |
| 422 | "llm-stream: the record does not meet aien-bench-record/1 ({} defect(s)); it is not a claimed result and was renamed to {}.rejected (bind the measured files with --llm-binary and --llm-model-file)", |
| 423 | d.len(), |
| 424 | path.display() |
| 425 | ); |
| 426 | std::process::exit(3); |
| 427 | } |
| 428 | Err(e) => { |
| 429 | eprintln!("llm-stream: re-read {}: {}", path.display(), e); |
| 430 | std::process::exit(1); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | Err(e) => { |
| 435 | eprintln!("llm-stream: {}", e); |
| 436 | std::process::exit(1); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | Commands::Report => { |
| 441 | let data = load_data(&cli.input); |
| 442 | print_report(&data); |
| 443 | } |
| 444 | Commands::Generate { output } => { |
| 445 | let data = load_data(&cli.input); |
| 446 | render_all_svgs(&data, &output); |
| 447 | } |
| 448 | Commands::Verify => { |
| 449 | let data = load_data(&cli.input); |
| 450 | for m in &data.memory_rss { |
| 451 | if m.service == "openclaw-rs" || m.service == "aegis-runtime" { |
| 452 | assert!(m.rss_mb < 10.0, "{} RSS exceeded 10MB budget", m.service); |
| 453 | } |
| 454 | if m.service == "cortex-rs" { |
| 455 | assert!(m.rss_mb < 25.0, "cortex-rs RSS exceeded 25MB budget"); |
| 456 | } |
| 457 | } |
| 458 | for l in &data.latency_concurrency { |
| 459 | if l.engine.contains("Rust") { |
| 460 | assert!(l.p50_ms < 10.0, "Rust service latency exceeded 10ms p50"); |
| 461 | assert!( |
| 462 | l.requests_per_sec > 1000.0, |
| 463 | "Rust service throughput below 1000 req/s" |
| 464 | ); |
| 465 | } |
| 466 | } |
| 467 | println!("All invariant checks passed successfully."); |
| 468 | } |
| 469 | Commands::SyncDocs { target, check } => { |
| 470 | let data = load_data(&cli.input); |
| 471 | let table = generate_markdown_table(&data); |
| 472 | |
| 473 | // Targets named with --target must exist; only the implicit defaults may be absent. |
| 474 | let named = !target.is_empty(); |
| 475 | let targets = if named { |
| 476 | target.clone() |
| 477 | } else { |
| 478 | vec![ |
| 479 | "../README.md".to_string(), |
| 480 | "../docs/STATE_OF_AIEN.md".to_string(), |
| 481 | ] |
| 482 | }; |
| 483 | |
| 484 | let mut mismatch = false; |
| 485 | for t in &targets { |
| 486 | let p = Path::new(t); |
| 487 | if !p.exists() { |
| 488 | if named { |
| 489 | eprintln!("Target file not found: {}", t); |
| 490 | mismatch = true; |
| 491 | } else { |
| 492 | println!("Default target file not found, skipping: {}", t); |
| 493 | } |
| 494 | continue; |
| 495 | } |
| 496 | let content = fs::read_to_string(p).expect("Failed to read target file"); |
| 497 | let updated = match sync_markdown_content(&content, &table) { |
| 498 | Some(updated) => updated, |
| 499 | None => { |
| 500 | if content.contains("<!-- AIEN:BENCHMARKS:START -->") |
| 501 | && content.contains("<!-- AIEN:BENCHMARKS:END -->") |
| 502 | { |
| 503 | eprintln!( |
| 504 | "Target has benchmark markers out of order (no AIEN:BENCHMARKS:END after START): {}", |
| 505 | t |
| 506 | ); |
| 507 | } else { |
| 508 | eprintln!( |
| 509 | "Target has no benchmark markers (needs AIEN:BENCHMARKS:START and END): {}", |
| 510 | t |
| 511 | ); |
| 512 | } |
| 513 | mismatch = true; |
| 514 | continue; |
| 515 | } |
| 516 | }; |
| 517 | |
| 518 | if updated != content { |
| 519 | if check { |
| 520 | eprintln!("Benchmark table out of date in: {}", t); |
| 521 | mismatch = true; |
| 522 | } else { |
| 523 | fs::write(p, &updated).expect("Failed to write target file"); |
| 524 | println!("Synchronized benchmark table into: {}", t); |
| 525 | } |
| 526 | } else { |
| 527 | println!("Benchmark table already in sync: {}", t); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if mismatch { |
| 532 | std::process::exit(1); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | pub fn generate_markdown_table(data: &BenchmarkData) -> String { |
| 539 | let mut out = String::new(); |
| 540 | out.push_str("<!-- AIEN:BENCHMARKS:START -->\n"); |
| 541 | out.push_str("<!-- Sourced automatically from benchmarks/data/benchmarks_latest.json (Measurement Suite v"); |
| 542 | out.push_str(&data.version); |
| 543 | out.push_str(") -->\n"); |
| 544 | out.push_str("| Workload / Service | Architecture | Measurement ID | Resident Memory (RSS) | p50 Latency | Throughput |\n"); |
| 545 | out.push_str("| :--- | :--- | :--- | :---: | :---: | :---: |\n"); |
| 546 | |
| 547 | for mem in &data.memory_rss { |
| 548 | let lat = data |
| 549 | .latency_concurrency |
| 550 | .iter() |
| 551 | .find(|l| l.service == mem.service); |
| 552 | let id = mem.measurement_id.as_deref().unwrap_or("BENCH-UNKNOWN"); |
| 553 | let rss_str = format!("{:.2} MB", mem.rss_mb); |
| 554 | let (lat_str, tps_str) = match lat { |
| 555 | Some(l) => ( |
| 556 | format!("{:.2} ms", l.p50_ms), |
| 557 | format!("{:.0} req/s", l.requests_per_sec), |
| 558 | ), |
| 559 | None => ("N/A".to_string(), "N/A".to_string()), |
| 560 | }; |
| 561 | |
| 562 | out.push_str(&format!( |
| 563 | "| {} | {} | `{}` | {} | {} | {} |\n", |
| 564 | mem.role, mem.architecture, id, rss_str, lat_str, tps_str |
| 565 | )); |
| 566 | } |
| 567 | |
| 568 | out.push_str("\n*Hardware Reference: "); |
| 569 | out.push_str(&data.hardware.system); |
| 570 | out.push_str(" ("); |
| 571 | out.push_str(&data.hardware.processor); |
| 572 | out.push_str(", "); |
| 573 | out.push_str(&data.hardware.memory_unified_gb.to_string()); |
| 574 | out.push_str(" GB unified memory). All metrics measured under concurrency C="); |
| 575 | out.push_str(&data.environment.concurrency_tested.to_string()); |
| 576 | out.push_str(" over "); |
| 577 | out.push_str(&data.environment.requests_per_endpoint.to_string()); |
| 578 | out.push_str(" requests per endpoint.*\n"); |
| 579 | out.push_str("<!-- AIEN:BENCHMARKS:END -->"); |
| 580 | out |
| 581 | } |
| 582 | |
| 583 | /// Replaces the span from the START marker through the END marker with `replacement`. |
| 584 | /// Returns `None` when either marker is missing or END comes before START, so the |
| 585 | /// caller can never mistake a document without a table for one that is in sync. |
| 586 | pub fn sync_markdown_content(content: &str, replacement: &str) -> Option<String> { |
| 587 | let start_marker = "<!-- AIEN:BENCHMARKS:START -->"; |
| 588 | let end_marker = "<!-- AIEN:BENCHMARKS:END -->"; |
| 589 | |
| 590 | let start_idx = content.find(start_marker)?; |
| 591 | let end_idx = start_idx + content[start_idx..].find(end_marker)? + end_marker.len(); |
| 592 | |
| 593 | let mut out = String::with_capacity(content.len() + replacement.len()); |
| 594 | out.push_str(&content[..start_idx]); |
| 595 | out.push_str(replacement); |
| 596 | out.push_str(&content[end_idx..]); |
| 597 | Some(out) |
| 598 | } |
| 599 | |
| 600 | #[cfg(test)] |
| 601 | mod tests { |
| 602 | use super::*; |
| 603 | |
| 604 | #[test] |
| 605 | fn test_svg_generation() { |
| 606 | let raw = include_str!("../data/benchmarks_20260919.json"); |
| 607 | let data: BenchmarkData = serde_json::from_str(raw).expect("Valid JSON"); |
| 608 | let mem_svg = svg_chart::generate_memory_chart(&data); |
| 609 | assert!(mem_svg.contains("<svg"), "Must contain SVG opening tag"); |
| 610 | assert!(mem_svg.contains("</svg>"), "Must contain SVG closing tag"); |
| 611 | assert!(!mem_svg.contains('\u{2014}'), "Must not contain em dash"); |
| 612 | assert!(!mem_svg.contains('\u{2013}'), "Must not contain en dash"); |
| 613 | assert!( |
| 614 | !mem_svg.to_lowercase().contains("python"), |
| 615 | "No outside comparison in memory chart" |
| 616 | ); |
| 617 | |
| 618 | let lat_svg = svg_chart::generate_latency_chart(&data); |
| 619 | assert!(lat_svg.contains("<svg"), "Must contain SVG opening tag"); |
| 620 | assert!(lat_svg.contains("</svg>"), "Must contain SVG closing tag"); |
| 621 | assert!(!lat_svg.contains('\u{2014}'), "Must not contain em dash"); |
| 622 | assert!(!lat_svg.contains('\u{2013}'), "Must not contain en dash"); |
| 623 | } |
| 624 | |
| 625 | #[test] |
| 626 | fn test_sync_markdown_table_generation() { |
| 627 | let raw = include_str!("../data/benchmarks_latest.json"); |
| 628 | let data: BenchmarkData = serde_json::from_str(raw).expect("Valid JSON"); |
| 629 | let table = generate_markdown_table(&data); |
| 630 | |
| 631 | assert!(table.contains("<!-- AIEN:BENCHMARKS:START -->")); |
| 632 | assert!(table.contains("<!-- AIEN:BENCHMARKS:END -->")); |
| 633 | assert!(table.contains("BENCH-OPENCLAW-RSS-001")); |
| 634 | assert!( |
| 635 | !table.to_lowercase().contains("python"), |
| 636 | "No outside comparison rows" |
| 637 | ); |
| 638 | assert!(table.contains("BENCH-CORTEX-RSS-001")); |
| 639 | assert!(!table.contains('\u{2014}'), "Must not contain em dash"); |
| 640 | assert!(!table.contains('\u{2013}'), "Must not contain en dash"); |
| 641 | |
| 642 | let doc = "# Header\n<!-- AIEN:BENCHMARKS:START -->\nold table\n<!-- AIEN:BENCHMARKS:END -->\n# Footer"; |
| 643 | let synced = sync_markdown_content(doc, &table).expect("markers present"); |
| 644 | assert!(sync_markdown_content("# no markers", &table).is_none()); |
| 645 | assert!(sync_markdown_content( |
| 646 | "<!-- AIEN:BENCHMARKS:END -->\n<!-- AIEN:BENCHMARKS:START -->", |
| 647 | &table |
| 648 | ) |
| 649 | .is_none()); |
| 650 | assert!(synced.contains("# Header")); |
| 651 | assert!(synced.contains("# Footer")); |
| 652 | assert!(synced.contains("BENCH-OPENCLAW-RSS-001")); |
| 653 | assert!(!synced.contains("old table")); |
| 654 | } |
| 655 | } |