#![allow(missing_docs)]

// Leases isn't ideal to use with looped benchmarks such as criterion/divan, as they rely on setup code in each iteration.
// Unfortunately, unlooped benchmarks such as iai cannot exclude setup code from the measurements. Results would need to be post processed.
//
// So while I recognize that these benchmarks aren't ideal, they should be better than guestimating optimizations.

use criterion::{Criterion, criterion_group, criterion_main};
use mac_address::MacAddress;
use toe_beans::v4::*;

fn benchmark_leases_offer_release(c: &mut Criterion) {
    let mut leases = Leases::new(LeaseConfig::default());
    let owner = MacAddress::new([1, 2, 3, 4, 5, 6]);

    c.bench_function("benchmark leases offer/release", |b| {
        b.iter(|| {
            let _ = leases.offer(owner, None);
            let _ = leases.release(owner);
        })
    });
}

fn benchmark_leases_ack_release(c: &mut Criterion) {
    let mut leases = Leases::new(LeaseConfig::default());
    let owner = MacAddress::new([1, 2, 3, 4, 5, 6]);

    c.bench_function("benchmark leases ack/release", |b| {
        b.iter(|| {
            let _ = leases.ack(owner);
            let _ = leases.release(owner);
        })
    });
}

fn benchmark_leases_extend(c: &mut Criterion) {
    let mut leases = Leases::new(LeaseConfig::default());
    let owner = MacAddress::new([1, 2, 3, 4, 5, 6]);
    leases.ack(owner).unwrap();

    c.bench_function("benchmark leases extend", |b| {
        b.iter(|| {
            let _ = leases.extend(owner);
        })
    });
}

fn benchmark_leases_verify(c: &mut Criterion) {
    let mut leases = Leases::new(LeaseConfig::default());
    let owner = MacAddress::new([1, 2, 3, 4, 5, 6]);
    let ip = leases.ack(owner).unwrap();

    c.bench_function("benchmark leases verify", |b| {
        b.iter(|| {
            let _ = leases.verify_lease(owner, &ip);
        })
    });
}

criterion_group!(
    benches,
    benchmark_leases_offer_release,
    benchmark_leases_ack_release,
    benchmark_leases_extend,
    benchmark_leases_verify
);
criterion_main!(benches);
