use std::sync::Arc;
use std::time::Instant;
use supermachine::{Image, VmConfig};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let snap = format!(
"{}/.local/supermachine-snapshots/nginx_1_27-alpine",
std::env::var("HOME").unwrap()
);
let n = 5usize;
let image = Arc::new(Image::from_snapshot(&snap)?);
println!("=== priming pool with warm={n} ===");
{
let _vm = image.acquire_with(&VmConfig::new().with_pool_warm(n))?;
}
println!("=== {n} concurrent acquires ===");
let t0 = Instant::now();
let handles: Vec<_> = (0..n)
.map(|i| {
let image = Arc::clone(&image);
std::thread::spawn(move || {
let t1 = Instant::now();
let vm = image.acquire().unwrap();
let acquired = t1.elapsed();
let out = vm
.exec_builder()
.argv(["sh", "-c", "for j in 1 2 3 4 5; do echo $j; done"])
.output()
.unwrap();
let total = t1.elapsed();
println!(
" thread {i}: acquired in {acquired:?}, exec done in {total:?}, status={:?}",
out.status.code()
);
})
})
.collect();
for h in handles {
h.join().unwrap();
}
println!("\n{n} concurrent acquires + execs total: {:?}", t0.elapsed());
Ok(())
}