use aspect_std::AllowlistAspect;
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use std::hint::black_box;
const SIZES: &[usize] = &[1, 8, 64, 256];
fn make_entries(n: usize) -> Vec<String> {
(0..n).map(|i| format!("user_{i:08}")).collect()
}
#[inline]
fn baseline_is_allowed(entries: &[String], identity: &str) -> bool {
entries.iter().any(|u| u == "*" || u == identity)
}
fn bench_allowlist(c: &mut Criterion) {
let mut hit_group = c.benchmark_group("allowlist_hit");
for &n in SIZES {
let entries = make_entries(n);
let target = entries.last().cloned().unwrap_or_else(|| "user_0".into());
let aspect = AllowlistAspect::new(entries.clone());
hit_group.bench_with_input(
BenchmarkId::new("baseline_vec_any", n),
&(entries.clone(), target.clone()),
|b, (e, t)| {
b.iter(|| baseline_is_allowed(black_box(e), black_box(t)));
},
);
hit_group.bench_with_input(
BenchmarkId::new("aspect_is_allowed", n),
&(aspect.clone(), target.clone()),
|b, (a, t)| {
b.iter(|| a.is_allowed(black_box(t)));
},
);
}
hit_group.finish();
let mut miss_group = c.benchmark_group("allowlist_miss");
for &n in SIZES {
let entries = make_entries(n);
let target = "not_in_list".to_string();
let aspect = AllowlistAspect::new(entries.clone());
miss_group.bench_with_input(
BenchmarkId::new("baseline_vec_any", n),
&(entries.clone(), target.clone()),
|b, (e, t)| {
b.iter(|| baseline_is_allowed(black_box(e), black_box(t)));
},
);
miss_group.bench_with_input(
BenchmarkId::new("aspect_is_allowed", n),
&(aspect.clone(), target.clone()),
|b, (a, t)| {
b.iter(|| a.is_allowed(black_box(t)));
},
);
}
miss_group.finish();
let mut wildcard_group = c.benchmark_group("allowlist_wildcard");
{
let entries = vec!["*".to_string()];
let target = "anyone_at_all".to_string();
let aspect = AllowlistAspect::new(entries.clone());
wildcard_group.bench_function("baseline_vec_any", |b| {
b.iter(|| baseline_is_allowed(black_box(&entries), black_box(&target)));
});
wildcard_group.bench_function("aspect_is_allowed", |b| {
b.iter(|| aspect.is_allowed(black_box(&target)));
});
}
wildcard_group.finish();
}
criterion_group!(benches, bench_allowlist);
criterion_main!(benches);