use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use std::hint::black_box;
use wp_model_core::raw::RawData;
use wp_primitives::Parser;
use wpl::{WplEvaluator, wpl_express};
fn build_failing_evaluators(n: usize) -> Vec<WplEvaluator> {
let fail_templates = [
r#"(digit)"#,
r#"(ip)"#,
r#"(time)"#,
r#"(float)"#,
r#"(digit, ip)"#,
r#"(ip, time)"#,
r#"(digit, time)"#,
r#"(digit, digit)"#,
];
(0..n)
.map(|i| {
let tmpl = fail_templates[i % fail_templates.len()];
let expr = wpl_express.parse(tmpl).expect("parse wpl");
WplEvaluator::from(&expr, None).expect("build evaluator")
})
.collect()
}
fn bench_proc_path(c: &mut Criterion) {
let rule_counts = [5, 10, 20, 30];
let payload = RawData::from_string(
"The quick brown fox jumps over the lazy dog. No structured data here at all.",
);
let mut group = c.benchmark_group("multi_rule_error/proc");
group.measurement_time(std::time::Duration::from_secs(3));
for &n in &rule_counts {
let evaluators = build_failing_evaluators(n);
group.throughput(Throughput::Elements(n as u64));
group.bench_function(BenchmarkId::from_parameter(n), |b| {
b.iter(|| {
for eval in &evaluators {
let _ = black_box(eval.proc(0, payload.clone(), 0));
}
})
});
}
group.finish();
}
fn bench_proc_ref_path(c: &mut Criterion) {
let rule_counts = [5, 10, 20, 30];
let payload = RawData::from_string(
"The quick brown fox jumps over the lazy dog. No structured data here at all.",
);
let mut group = c.benchmark_group("multi_rule_error/proc_ref");
group.measurement_time(std::time::Duration::from_secs(3));
for &n in &rule_counts {
let evaluators = build_failing_evaluators(n);
group.throughput(Throughput::Elements(n as u64));
group.bench_function(BenchmarkId::from_parameter(n), |b| {
b.iter(|| {
for eval in &evaluators {
let _ = black_box(eval.proc_ref(0, &payload, 0));
}
})
});
}
group.finish();
}
fn bench_partial_match(c: &mut Criterion) {
let rule_counts = [5, 10, 20, 30];
let payload =
RawData::from_string("alpha beta gamma delta epsilon zeta eta theta iota kappa lambda");
let fail_types = ["digit", "ip", "time", "float"];
let mut group = c.benchmark_group("multi_rule_error/partial_match");
group.measurement_time(std::time::Duration::from_secs(3));
for &n in &rule_counts {
let evaluators: Vec<WplEvaluator> = (0..n)
.map(|i| {
let ft = fail_types[i % fail_types.len()];
let rule = format!("(chars, {ft})");
let expr = wpl_express.parse(&rule).expect("parse wpl");
WplEvaluator::from(&expr, None).expect("build evaluator")
})
.collect();
group.throughput(Throughput::Elements(n as u64));
group.bench_function(BenchmarkId::from_parameter(n), |b| {
b.iter(|| {
for eval in &evaluators {
let _ = black_box(eval.proc_ref(0, &payload, 0));
}
})
});
}
group.finish();
}
criterion_group!(
benches,
bench_proc_path,
bench_proc_ref_path,
bench_partial_match,
);
criterion_main!(benches);