bench: add RegexSet benchmarks

This commit is contained in:
Markus Westerlind 2018-08-22 14:49:03 +02:00 committed by Andrew Gallant
parent 1bb38e56bb
commit d51d23642f
2 changed files with 78 additions and 2 deletions

View File

@ -44,9 +44,9 @@ pub use ffi::re2::Regex;
#[cfg(feature = "re-dphobos")]
pub use ffi::d_phobos::Regex;
#[cfg(feature = "re-rust")]
pub use regex::Regex;
pub use regex::{Regex, RegexSet};
#[cfg(feature = "re-rust-bytes")]
pub use regex::bytes::Regex;
pub use regex::bytes::{Regex, RegexSet};
#[cfg(feature = "re-tcl")]
pub use ffi::tcl::Regex;
@ -272,6 +272,58 @@ macro_rules! bench_captures {
}
}
// USAGE: bench_is_match_set!(name, is_match, regex, haystack)
macro_rules! bench_is_match_set {
($name:ident, $is_match:expr, $re:expr, $haystack:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
use std::sync::Mutex;
lazy_static! {
static ref RE: Mutex<RegexSet> = Mutex::new($re);
static ref TEXT: Mutex<Text> = Mutex::new(text!($haystack));
};
let re = RE.lock().unwrap();
let text = TEXT.lock().unwrap();
b.bytes = text.len() as u64;
b.iter(|| {
if re.is_match(&text) != $is_match {
if $is_match {
panic!("expected match, got not match");
} else {
panic!("expected no match, got match");
}
}
});
}
}
}
// USAGE: bench_matches_set!(name, is_match, regex, haystack)
macro_rules! bench_matches_set {
($name:ident, $is_match:expr, $re:expr, $haystack:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
use std::sync::Mutex;
lazy_static! {
static ref RE: Mutex<RegexSet> = Mutex::new($re);
static ref TEXT: Mutex<Text> = Mutex::new(text!($haystack));
};
let re = RE.lock().unwrap();
let text = TEXT.lock().unwrap();
b.bytes = text.len() as u64;
b.iter(|| {
if re.matches(&text).matched_any() != $is_match {
if $is_match {
panic!("expected match, got not match");
} else {
panic!("expected no match, got match");
}
}
});
}
}
}
mod ffi;
mod misc;
mod regexdna;

View File

@ -14,6 +14,8 @@ use std::iter::repeat;
use test::Bencher;
#[cfg(any(feature = "re-rust", feature = "re-rust-bytes"))]
use RegexSet;
use {Regex, Text};
#[cfg(not(feature = "re-onig"))]
@ -278,3 +280,25 @@ bench_captures!(short_haystack_1000000x,
repeat("aaaa").take(1000000).collect::<String>(),
repeat("dddd").take(1000000).collect::<String>(),
));
#[cfg(any(feature = "re-rust", feature = "re-rust-bytes"))]
bench_is_match_set!(is_match_set,
true,
RegexSet::new(vec![
"aaaaaaaaaaaaaaaaaaa", "abc579", "def.+", "e24fg", "a.*2c", "23.",
]).unwrap(),
format!("{}a482c{}",
repeat('a').take(10).collect::<String>(),
repeat('b').take(10).collect::<String>())
);
#[cfg(any(feature = "re-rust", feature = "re-rust-bytes"))]
bench_matches_set!(matches_set,
true,
RegexSet::new(vec![
"aaaaaaaaaaaaaaaaaaa", "abc579", "def.+", "e24fg", "a.*2c", "23.",
]).unwrap(),
format!("{}a482c{}",
repeat('a').take(10).collect::<String>(),
repeat('b').take(10).collect::<String>())
);