From d51d23642fc9e919cc030d97d383eebee38b33c1 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Wed, 22 Aug 2018 14:49:03 +0200 Subject: [PATCH] bench: add RegexSet benchmarks --- bench/src/bench.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++-- bench/src/misc.rs | 24 ++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/bench/src/bench.rs b/bench/src/bench.rs index 6cb56db..ac5c3d8 100644 --- a/bench/src/bench.rs +++ b/bench/src/bench.rs @@ -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 = Mutex::new($re); + static ref TEXT: Mutex = 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 = Mutex::new($re); + static ref TEXT: Mutex = 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; diff --git a/bench/src/misc.rs b/bench/src/misc.rs index ad516e2..b5b25b7 100644 --- a/bench/src/misc.rs +++ b/bench/src/misc.rs @@ -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::(), repeat("dddd").take(1000000).collect::(), )); + +#[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::(), + repeat('b').take(10).collect::()) + ); + +#[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::(), + repeat('b').take(10).collect::()) + );