//! Unpaced logical-frame and capture timing through the real headless host. use prometeu_host_headless::{AdvanceFrameOutcome, HeadlessHost}; use std::time::Instant; fn stats(mut times: Vec) -> serde_json::Value { times.sort_unstable(); let n = times.len(); serde_json::json!({"mean_us":times.iter().sum::() as f64/n as f64/1000.0, "p95_us":times[(n*95).div_ceil(100)-1] as f64/1000.0, "p99_us":times[(n*99).div_ceil(100)-1] as f64/1000.0, "max_us":times[n-1] as f64/1000.0, "over_60hz_budget":times.iter().filter(|&&t|t>1_000_000_000/60).count()}) } fn main() { let mut args = std::env::args().skip(1); let cartridge = args.next().expect("cartridge directory"); let count: usize = args.next().map(|s| s.parse().expect("frame count")).unwrap_or(600); assert!(count > 0); let mut host = HeadlessHost::new(); host.boot(cartridge).expect("boot"); let mut advance = Vec::with_capacity(count); let mut capture = Vec::with_capacity(count); let mut previous = None; let mut published = 0; let mut checksum = 0; for i in 0..count + 120 { let start = Instant::now(); assert!(matches!( host.advance_frame().expect("advance"), AdvanceFrameOutcome::FrameCompleted { .. } )); let elapsed = start.elapsed().as_nanos() as u64; let start = Instant::now(); let frame = host.capture_frame().expect("published frame").frame; let capture_ns = start.elapsed().as_nanos() as u64; assert_eq!((frame.width, frame.height, frame.stride_pixels), (640, 360, 640)); assert_ne!(previous, Some((frame.ownership, frame.frame_id))); previous = Some((frame.ownership, frame.frame_id)); checksum ^= frame.pixels[(i * 173) % frame.pixels.len()]; if i >= 120 { advance.push(elapsed); capture.push(capture_ns); published += 1; } } let state = host.get_state(); println!( "{}", serde_json::json!({"schema":1,"width":640,"height":360,"frames":count,"warmup_frames":120, "completed_logical_frames_total":state.frame_index,"host_ticks_total":state.tick_index, "published_frames_measured":published,"repeated_publications_measured":0, "advance_including_vm_render_publish":stats(advance),"owned_capture":stats(capture),"checksum":checksum, "vm_hz":null,"physical_display_hz":null, "limitations":"unpaced real VM/headless; capture copies owned pixels; no GPU, realtime cadence or physical display"}) ); }