1 use criterion::Criterion;
2
3 use crate::input::*;
4 use crate::{define_aho_corasick, define_aho_corasick_dfa};
5
6 /// These benchmarks test various words on random text.
all(c: &mut Criterion)7 pub fn all(c: &mut Criterion) {
8 memchr_optimizations(c);
9 misc(c);
10 many_patterns(c);
11 }
12
13 /// These benchmarks test the prefix byte optimization, and the impact that
14 /// match-vs-non-match has.
15 ///
16 /// More specifically, Aho-Corasick will use highly optimized vectorized
17 /// routines (on x86) if it determines that all matches start with 1, 2 or 3
18 /// distinct bytes. Beyond that, no prefix optimizations are used.
19 ///
20 /// For match-vs-non-match, we keep the match counts fixed across the different
21 /// prefix optimizations as a way to control what we measure.
memchr_optimizations(c: &mut Criterion)22 fn memchr_optimizations(c: &mut Criterion) {
23 define_random(c, "onebyte/match", 352, vec!["a"]);
24 define_random(c, "onebyte/nomatch", 0, vec!["\x00"]);
25 define_random(c, "twobytes/match", 352, vec!["a", "\x00"]);
26 define_random(c, "twobytes/nomatch", 0, vec!["\x00", "\x01"]);
27 define_random(c, "threebytes/match", 352, vec!["a", "\x00", "\x01"]);
28 define_random(c, "threebytes/nomatch", 0, vec!["\x00", "\x01", "\x02"]);
29 define_random(
30 c,
31 "fourbytes/match",
32 352,
33 vec!["a", "\x00", "\x01", "\x02"],
34 );
35 define_random(
36 c,
37 "fourbytes/nomatch",
38 0,
39 vec!["\x00", "\x01", "\x02", "\x03"],
40 );
41 define_random(
42 c,
43 "fivebytes/match",
44 352,
45 vec!["a", "\x00", "\x01", "\x02", "\x03"],
46 );
47 define_random(
48 c,
49 "fivebytes/nomatch",
50 0,
51 vec!["\x00", "\x01", "\x02", "\x03", "\x04"],
52 );
53 }
54
55 /// Some miscellaneous benchmarks on random data.
misc(c: &mut Criterion)56 fn misc(c: &mut Criterion) {
57 define_random(
58 c,
59 "ten-one-prefix",
60 0,
61 vec![
62 "zacdef", "zbcdef", "zccdef", "zdcdef", "zecdef", "zfcdef",
63 "zgcdef", "zhcdef", "zicdef", "zjcdef",
64 ],
65 );
66 define_random(
67 c,
68 "ten-diff-prefix",
69 0,
70 vec![
71 "abcdef", "bcdefg", "cdefgh", "defghi", "efghij", "fghijk",
72 "ghijkl", "hijklm", "ijklmn", "jklmno",
73 ],
74 );
75 }
76
77 /// Various benchmarks using a large pattern set.
many_patterns(c: &mut Criterion)78 fn many_patterns(c: &mut Criterion) {
79 use aho_corasick::MatchKind::*;
80
81 let name = "5000words";
82
83 let group = "random10x/standard";
84 define_aho_corasick_dfa(
85 c,
86 group,
87 name,
88 RANDOM10X,
89 Standard,
90 0,
91 words_5000(),
92 |ac, haystack| ac.find_iter(haystack).count(),
93 );
94 let group = "random10x/leftmost-first";
95 define_aho_corasick_dfa(
96 c,
97 group,
98 name,
99 RANDOM10X,
100 LeftmostFirst,
101 0,
102 words_5000(),
103 |ac, haystack| ac.find_iter(haystack).count(),
104 );
105 let group = "random10x/leftmost-longest";
106 define_aho_corasick_dfa(
107 c,
108 group,
109 name,
110 RANDOM10X,
111 LeftmostLongest,
112 0,
113 words_5000(),
114 |ac, haystack| ac.find_iter(haystack).count(),
115 );
116
117 let group = "random10x/overlapping";
118 define_aho_corasick_dfa(
119 c,
120 group,
121 name,
122 RANDOM10X,
123 Standard,
124 0,
125 words_5000(),
126 |ac, haystack| ac.find_overlapping_iter(haystack).count(),
127 );
128
129 let name = "100words";
130
131 let group = "random10x/standard";
132 define_aho_corasick_dfa(
133 c,
134 group,
135 name,
136 RANDOM10X,
137 Standard,
138 0,
139 words_100(),
140 |ac, haystack| ac.find_iter(haystack).count(),
141 );
142 let group = "random10x/leftmost-first";
143 define_aho_corasick_dfa(
144 c,
145 group,
146 name,
147 RANDOM10X,
148 LeftmostFirst,
149 0,
150 words_100(),
151 |ac, haystack| ac.find_iter(haystack).count(),
152 );
153 let group = "random10x/leftmost-longest";
154 define_aho_corasick_dfa(
155 c,
156 group,
157 name,
158 RANDOM10X,
159 LeftmostLongest,
160 0,
161 words_100(),
162 |ac, haystack| ac.find_iter(haystack).count(),
163 );
164
165 let group = "random10x/overlapping";
166 define_aho_corasick_dfa(
167 c,
168 group,
169 name,
170 RANDOM10X,
171 Standard,
172 0,
173 words_100(),
174 |ac, haystack| ac.find_overlapping_iter(haystack).count(),
175 );
176 }
177
define_random<B: AsRef<[u8]>>( c: &mut Criterion, bench_name: &str, count: usize, patterns: Vec<B>, )178 fn define_random<B: AsRef<[u8]>>(
179 c: &mut Criterion,
180 bench_name: &str,
181 count: usize,
182 patterns: Vec<B>,
183 ) {
184 define_aho_corasick(c, "random", bench_name, RANDOM, count, patterns);
185 }
186