mirror of
https://gitee.com/openharmony/third_party_rust_regex
synced 2025-04-15 09:00:00 +00:00
bench: add RegexSet benchmarks
This commit is contained in:
parent
1bb38e56bb
commit
d51d23642f
@ -44,9 +44,9 @@ pub use ffi::re2::Regex;
|
|||||||
#[cfg(feature = "re-dphobos")]
|
#[cfg(feature = "re-dphobos")]
|
||||||
pub use ffi::d_phobos::Regex;
|
pub use ffi::d_phobos::Regex;
|
||||||
#[cfg(feature = "re-rust")]
|
#[cfg(feature = "re-rust")]
|
||||||
pub use regex::Regex;
|
pub use regex::{Regex, RegexSet};
|
||||||
#[cfg(feature = "re-rust-bytes")]
|
#[cfg(feature = "re-rust-bytes")]
|
||||||
pub use regex::bytes::Regex;
|
pub use regex::bytes::{Regex, RegexSet};
|
||||||
#[cfg(feature = "re-tcl")]
|
#[cfg(feature = "re-tcl")]
|
||||||
pub use ffi::tcl::Regex;
|
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 ffi;
|
||||||
mod misc;
|
mod misc;
|
||||||
mod regexdna;
|
mod regexdna;
|
||||||
|
@ -14,6 +14,8 @@ use std::iter::repeat;
|
|||||||
|
|
||||||
use test::Bencher;
|
use test::Bencher;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "re-rust", feature = "re-rust-bytes"))]
|
||||||
|
use RegexSet;
|
||||||
use {Regex, Text};
|
use {Regex, Text};
|
||||||
|
|
||||||
#[cfg(not(feature = "re-onig"))]
|
#[cfg(not(feature = "re-onig"))]
|
||||||
@ -278,3 +280,25 @@ bench_captures!(short_haystack_1000000x,
|
|||||||
repeat("aaaa").take(1000000).collect::<String>(),
|
repeat("aaaa").take(1000000).collect::<String>(),
|
||||||
repeat("dddd").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>())
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user