Skip to content

Commit

Permalink
bench: add RegexSet benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind authored and BurntSushi committed Dec 1, 2018
1 parent 1bb38e5 commit d51d236
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
56 changes: 54 additions & 2 deletions bench/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions bench/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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>())
);

0 comments on commit d51d236

Please sign in to comment.