| 1 | use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 2 | use glob::Pattern; |
| 3 | use regex::Regex; |
| 4 | use regex_lite::Regex as RegexLite; |
| 5 | use wildmatch::WildMatch; |
| 6 | |
| 7 | const TEXT: &str = "Lorem ipsum dolor sit amet, \ |
| 8 | consetetur sadipscing elitr, sed diam nonumy eirmod tempor \ |
| 9 | invidunt ut labore et dolore magna aliquyam erat, sed diam \ |
| 10 | voluptua. At vero eos et accusam et justo duo dolores et ea \ |
| 11 | rebum. Stet clita kasd gubergren, no sea takimata sanctus est \ |
| 12 | Lorem ipsum dolor sit amet."; |
| 13 | |
| 14 | const FULL_TEXT_PATTERN: &str = TEXT; |
| 15 | const FULL_TEXT_REGEX: &str = "^Lorem ipsum dolor sit amet, \ |
| 16 | consetetur sadipscing elitr, sed diam nonumy eirmod tempor \ |
| 17 | invidunt ut labore et dolore magna aliquyam erat, sed diam \ |
| 18 | voluptua\\. At vero eos et accusam et justo duo dolores et ea \ |
| 19 | rebum\\. Stet clita kasd gubergren, no sea takimata sanctus est \ |
| 20 | Lorem ipsum dolor sit amet\\.$"; |
| 21 | |
| 22 | const COMPLEX_PATTERN: &str = "Lorem?ipsum*dolore*ea* ?????ata*."; |
| 23 | const COMPLEX_REGEX: &str = "^Lorem.ipsum.*dolore.*ea.* .....ata.*\\.$"; |
| 24 | |
| 25 | const MOST_COMPLEX_PATTERN: &str = "?a*b*?**c?d****?e*f*g*?*h?i*?*?**j*******k"; |
| 26 | const MOST_COMPLEX_REGEX: &str = |
| 27 | "^.a.*b.*..*.*c.d.*.*.*.*.e.*f.*g.*..*h.i.*..*..*.*j.*.*.*.*.*.*.*k$"; |
| 28 | |
| 29 | pub fn compiling(c: &mut Criterion) { |
| 30 | let mut group = c.benchmark_group("compiling"); |
| 31 | |
| 32 | group.bench_function("compile text (wildmatch)", |b| { |
| 33 | b.iter(|| WildMatch::new(black_box(FULL_TEXT_PATTERN))) |
| 34 | }); |
| 35 | group.bench_function("compile complex (wildmatch)", |b| { |
| 36 | b.iter(|| WildMatch::new(black_box(MOST_COMPLEX_PATTERN))) |
| 37 | }); |
| 38 | |
| 39 | group.bench_function("compile text (regex)", |b| { |
| 40 | b.iter(|| Regex::new(black_box(FULL_TEXT_REGEX)).unwrap()) |
| 41 | }); |
| 42 | group.bench_function("compile complex (regex)", |b| { |
| 43 | b.iter(|| Regex::new(black_box(MOST_COMPLEX_REGEX)).unwrap()) |
| 44 | }); |
| 45 | |
| 46 | group.bench_function("compile text (glob)", |b| { |
| 47 | b.iter(|| Pattern::new(black_box(FULL_TEXT_PATTERN))) |
| 48 | }); |
| 49 | group.bench_function("compile complex (glob)", |b| { |
| 50 | b.iter(|| Pattern::new(black_box(MOST_COMPLEX_PATTERN))) |
| 51 | }); |
| 52 | |
| 53 | group.bench_function("compile text (regex_lite)", |b| { |
| 54 | b.iter(|| RegexLite::new(black_box(FULL_TEXT_PATTERN))) |
| 55 | }); |
| 56 | group.bench_function("compile complex (regex_lite)", |b| { |
| 57 | b.iter(|| RegexLite::new(black_box(MOST_COMPLEX_PATTERN))) |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | pub fn matching(c: &mut Criterion) { |
| 62 | let pattern1 = WildMatch::new(FULL_TEXT_PATTERN); |
| 63 | let pattern2 = WildMatch::new(COMPLEX_PATTERN); |
| 64 | let regex1 = Regex::new(FULL_TEXT_REGEX).unwrap(); |
| 65 | let regex2 = Regex::new(COMPLEX_REGEX).unwrap(); |
| 66 | let glob1 = Pattern::new(FULL_TEXT_PATTERN).unwrap(); |
| 67 | let glob2 = Pattern::new(COMPLEX_PATTERN).unwrap(); |
| 68 | let regex_lite1 = RegexLite::new(FULL_TEXT_PATTERN).unwrap(); |
| 69 | let regex_lite2 = RegexLite::new(COMPLEX_PATTERN).unwrap(); |
| 70 | |
| 71 | let mut group = c.benchmark_group("matching"); |
| 72 | |
| 73 | group.bench_function("match text (wildmatch)", |b| { |
| 74 | b.iter(|| pattern1 == black_box(TEXT)) |
| 75 | }); |
| 76 | group.bench_function("match complex (wildmatch)", |b| { |
| 77 | b.iter(|| pattern2 == black_box(TEXT)) |
| 78 | }); |
| 79 | |
| 80 | group.bench_function("match text (regex)", |b| { |
| 81 | b.iter(|| regex1.is_match(black_box(TEXT))) |
| 82 | }); |
| 83 | group.bench_function("match complex (regex)", |b| { |
| 84 | b.iter(|| regex2.is_match(black_box(TEXT))) |
| 85 | }); |
| 86 | |
| 87 | group.bench_function("match text (glob)", |b| { |
| 88 | b.iter(|| glob1.matches(black_box(TEXT))) |
| 89 | }); |
| 90 | group.bench_function("match complex (glob)", |b| { |
| 91 | b.iter(|| glob2.matches(black_box(TEXT))) |
| 92 | }); |
| 93 | |
| 94 | group.bench_function("match text (regex_lite)", |b| { |
| 95 | b.iter(|| regex_lite1.is_match(black_box(TEXT))) |
| 96 | }); |
| 97 | group.bench_function("match complex (regex_lite)", |b| { |
| 98 | b.iter(|| regex_lite2.is_match(black_box(TEXT))) |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | criterion_group!(benches, compiling, matching); |
| 103 | criterion_main!(benches); |