use std::time::Duration;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use draupnir::{
boot_fleet_and_await, boot_fleet_and_await_parallel, Boot, BootSpec, Lifecycle, Machine,
PowerState, Result, WaitOptions,
};
struct LatentNode {
latency: Duration,
}
impl Boot for LatentNode {
fn boot(&self, spec: &BootSpec) -> Result<Machine> {
Ok(Machine::started(format!("id-{}", spec.name), spec))
}
}
impl Lifecycle for LatentNode {
fn power_on(&self, _m: &Machine) -> Result<()> {
Ok(())
}
fn power_off(&self, _m: &Machine) -> Result<()> {
Ok(())
}
fn status(&self, _m: &Machine) -> Result<PowerState> {
std::thread::sleep(self.latency);
Ok(PowerState::On)
}
}
fn bench_fleet_boot(c: &mut Criterion) {
let backend = LatentNode {
latency: Duration::from_millis(2),
};
let spec = BootSpec::container("node", "redis:7");
let opts = WaitOptions::bounded(Duration::from_secs(30), Duration::from_millis(1));
let mut group = c.benchmark_group("fleet_boot");
for n in [4usize, 8, 16] {
group.bench_with_input(BenchmarkId::new("serial", n), &n, |b, &n| {
b.iter(|| boot_fleet_and_await(&spec, n, &backend, &opts));
});
group.bench_with_input(BenchmarkId::new("parallel", n), &n, |b, &n| {
b.iter(|| boot_fleet_and_await_parallel(&spec, n, &backend, &opts));
});
}
group.finish();
}
criterion_group!(benches, bench_fleet_boot);
criterion_main!(benches);