use criterion::{black_box, criterion_group, criterion_main, Criterion};
use kerf::{matches, PatternMatcher};
fn benchmark_pattern_matching(c: &mut Criterion) {
let test_values = vec![
"module_a",
"module_b_service",
"api_service_v1",
"database_connection",
"network_request_handler",
"security_auth_token",
"logging_system",
"config_parser",
"file_system_watcher",
"event_dispatcher",
];
c.bench_function("old_regex_exact", |b| {
b.iter(|| {
for value in &test_values {
black_box(matches("module_a", value));
}
})
});
c.bench_function("old_regex_prefix", |b| {
b.iter(|| {
for value in &test_values {
black_box(matches("module_*", value));
}
})
});
c.bench_function("old_regex_suffix", |b| {
b.iter(|| {
for value in &test_values {
black_box(matches("*_service", value));
}
})
});
c.bench_function("old_regex_contains", |b| {
b.iter(|| {
for value in &test_values {
black_box(matches("*service*", value));
}
})
});
let exact_matcher = PatternMatcher::new("module_a");
c.bench_function("new_exact", |b| {
b.iter(|| {
for value in &test_values {
black_box(exact_matcher.matches(value));
}
})
});
let prefix_matcher = PatternMatcher::new("module_*");
c.bench_function("new_prefix", |b| {
b.iter(|| {
for value in &test_values {
black_box(prefix_matcher.matches(value));
}
})
});
let suffix_matcher = PatternMatcher::new("*_service");
c.bench_function("new_suffix", |b| {
b.iter(|| {
for value in &test_values {
black_box(suffix_matcher.matches(value));
}
})
});
let contains_matcher = PatternMatcher::new("*service*");
c.bench_function("new_contains", |b| {
b.iter(|| {
for value in &test_values {
black_box(contains_matcher.matches(value));
}
})
});
let wildcard_matcher = PatternMatcher::new("*");
c.bench_function("new_wildcard", |b| {
b.iter(|| {
for value in &test_values {
black_box(wildcard_matcher.matches(value));
}
})
});
let complex_pattern = "api_*_v[0-9]+";
c.bench_function("old_regex_complex", |b| {
b.iter(|| {
for value in &test_values {
black_box(matches(complex_pattern, value));
}
})
});
let complex_matcher = PatternMatcher::new(complex_pattern);
c.bench_function("new_regex_complex", |b| {
b.iter(|| {
for value in &test_values {
black_box(complex_matcher.matches(value));
}
})
});
}
criterion_group!(benches, benchmark_pattern_matching);
criterion_main!(benches);