//! Verify that `Image::warm_pool(N)` returns only after N workers
//! are ready, so the next N `acquire()` calls don't block.

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

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let snap = format!(
        "{}/.local/supermachine-snapshots/nginx_1v",
        std::env::var("HOME")?
    );
    let n: usize = std::env::var("N")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(5);

    let image = Image::from_snapshot(&snap)?;

    println!("=== warm_pool({n}) ===");
    let t0 = Instant::now();
    image.warm_pool(n)?;
    let warm_ms = t0.elapsed();
    println!("  warm_pool returned in {warm_ms:?}");

    println!("=== {n} sequential acquires (each should be ~5ms warm restore) ===");
    for i in 0..n {
        let t = Instant::now();
        let vm = image.acquire()?;
        let ms = t.elapsed();
        println!("  acquire {i}: {ms:?}");
        drop(vm);
    }

    Ok(())
}
