]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | /*! |
2 | This module benchmarks the glob implementation. For benchmarks on the ripgrep | |
3 | tool itself, see the benchsuite directory. | |
4 | */ | |
5 | #![feature(test)] | |
6 | ||
dfeec247 XL |
7 | extern crate test; |
8 | ||
dfeec247 XL |
9 | use globset::{Candidate, Glob, GlobMatcher, GlobSet, GlobSetBuilder}; |
10 | ||
11 | const EXT: &'static str = "some/a/bigger/path/to/the/crazy/needle.txt"; | |
12 | const EXT_PAT: &'static str = "*.txt"; | |
13 | ||
14 | const SHORT: &'static str = "some/needle.txt"; | |
15 | const SHORT_PAT: &'static str = "some/**/needle.txt"; | |
16 | ||
17 | const LONG: &'static str = "some/a/bigger/path/to/the/crazy/needle.txt"; | |
18 | const LONG_PAT: &'static str = "some/**/needle.txt"; | |
19 | ||
20 | fn new_glob(pat: &str) -> glob::Pattern { | |
21 | glob::Pattern::new(pat).unwrap() | |
22 | } | |
23 | ||
24 | fn new_reglob(pat: &str) -> GlobMatcher { | |
25 | Glob::new(pat).unwrap().compile_matcher() | |
26 | } | |
27 | ||
28 | fn new_reglob_many(pats: &[&str]) -> GlobSet { | |
29 | let mut builder = GlobSetBuilder::new(); | |
30 | for pat in pats { | |
31 | builder.add(Glob::new(pat).unwrap()); | |
32 | } | |
33 | builder.build().unwrap() | |
34 | } | |
35 | ||
36 | #[bench] | |
37 | fn ext_glob(b: &mut test::Bencher) { | |
38 | let pat = new_glob(EXT_PAT); | |
39 | b.iter(|| assert!(pat.matches(EXT))); | |
40 | } | |
41 | ||
42 | #[bench] | |
43 | fn ext_regex(b: &mut test::Bencher) { | |
44 | let set = new_reglob(EXT_PAT); | |
45 | let cand = Candidate::new(EXT); | |
46 | b.iter(|| assert!(set.is_match_candidate(&cand))); | |
47 | } | |
48 | ||
49 | #[bench] | |
50 | fn short_glob(b: &mut test::Bencher) { | |
51 | let pat = new_glob(SHORT_PAT); | |
52 | b.iter(|| assert!(pat.matches(SHORT))); | |
53 | } | |
54 | ||
55 | #[bench] | |
56 | fn short_regex(b: &mut test::Bencher) { | |
57 | let set = new_reglob(SHORT_PAT); | |
58 | let cand = Candidate::new(SHORT); | |
59 | b.iter(|| assert!(set.is_match_candidate(&cand))); | |
60 | } | |
61 | ||
62 | #[bench] | |
63 | fn long_glob(b: &mut test::Bencher) { | |
64 | let pat = new_glob(LONG_PAT); | |
65 | b.iter(|| assert!(pat.matches(LONG))); | |
66 | } | |
67 | ||
68 | #[bench] | |
69 | fn long_regex(b: &mut test::Bencher) { | |
70 | let set = new_reglob(LONG_PAT); | |
71 | let cand = Candidate::new(LONG); | |
72 | b.iter(|| assert!(set.is_match_candidate(&cand))); | |
73 | } | |
74 | ||
75 | const MANY_SHORT_GLOBS: &'static [&'static str] = &[ | |
76 | // Taken from a random .gitignore on my system. | |
77 | ".*.swp", | |
78 | "tags", | |
79 | "target", | |
80 | "*.lock", | |
81 | "tmp", | |
82 | "*.csv", | |
83 | "*.fst", | |
84 | "*-got", | |
85 | "*.csv.idx", | |
86 | "words", | |
87 | "98m*", | |
88 | "dict", | |
89 | "test", | |
90 | "months", | |
91 | ]; | |
92 | ||
93 | const MANY_SHORT_SEARCH: &'static str = "98m-blah.csv.idx"; | |
94 | ||
95 | #[bench] | |
96 | fn many_short_glob(b: &mut test::Bencher) { | |
97 | let pats: Vec<_> = MANY_SHORT_GLOBS.iter().map(|&s| new_glob(s)).collect(); | |
98 | b.iter(|| { | |
99 | let mut count = 0; | |
100 | for pat in &pats { | |
101 | if pat.matches(MANY_SHORT_SEARCH) { | |
102 | count += 1; | |
103 | } | |
104 | } | |
105 | assert_eq!(2, count); | |
106 | }) | |
107 | } | |
108 | ||
109 | #[bench] | |
110 | fn many_short_regex_set(b: &mut test::Bencher) { | |
111 | let set = new_reglob_many(MANY_SHORT_GLOBS); | |
112 | b.iter(|| assert_eq!(2, set.matches(MANY_SHORT_SEARCH).iter().count())); | |
113 | } |