//! Validate the KVM warm pool: build() warms the image into a snapshot once,
//! then each acquire() is a CoW restore (~ms) instead of a cold boot (~1 s).
//! Times a few acquires and checks each VM execs.

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
fn main() {
    use std::time::Instant;
    use supermachine::Image;

    eprintln!("=== from_oci(alpine) + pool().build() (warms snapshot once) ===");
    let t0 = Instant::now();
    let image = Image::from_oci("alpine").expect("from_oci");
    let pool = image.pool().min(0).max(4).build().expect("build");
    eprintln!("build (incl warm) took {:?}", t0.elapsed());

    for i in 0..3 {
        let t = Instant::now();
        let vm = pool.acquire().expect("acquire");
        let acq = t.elapsed();
        let o = vm
            .exec_builder()
            .argv(["/bin/echo", "pooled"])
            .output()
            .expect("exec");
        eprintln!(
            "acquire #{i}: {acq:?}  exec_ok={} out={:?}",
            o.success(),
            String::from_utf8_lossy(&o.stdout).trim_end()
        );
        drop(vm);
    }
    eprintln!("=== done ===");
}

#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
fn main() {
    eprintln!("kvm_pool is Linux/x86_64 only");
}
