//! Time how fast `image.acquire()` returns when the hidden pool
//! is warm. Compare first-acquire (cold pool init) vs subsequent
//! acquires (snapshot restore only).

use std::time::Instant;
use supermachine::Image;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let snap = format!(
        "{}/.local/supermachine-snapshots/nginx_1_27-alpine",
        std::env::var("HOME").unwrap()
    );
    let image = Image::from_snapshot(&snap)?;

    let n = 10;
    let mut elapsed = Vec::with_capacity(n);
    for i in 0..n {
        let t0 = Instant::now();
        {
            let vm = image.acquire()?;
            // Confirm the VM is actually live by running a tiny exec.
            let out = vm.exec_builder().argv(["true"]).output()?;
            assert!(out.success(), "true exited with {:?}", out.status.code());
        }
        let dt = t0.elapsed();
        elapsed.push(dt);
        println!("acquire {i:2}: {dt:?}");
    }

    let cold = elapsed[0];
    let warm: std::time::Duration = elapsed[1..].iter().sum::<std::time::Duration>() / (n as u32 - 1);
    println!("\ncold-pool first acquire:  {cold:?}");
    println!("warm-pool subsequent (avg): {warm:?}");

    Ok(())
}
