PageRenderTime 114ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/crates/globset/benches/bench.rs

https://github.com/BurntSushi/ripgrep
Rust | 116 lines | 93 code | 18 blank | 5 comment | 3 complexity | 7b55c85a271deb1f6336a156333d0f5a MD5 | raw file
Possible License(s): MIT, Unlicense
  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. extern crate glob;
  7. extern crate globset;
  8. extern crate regex;
  9. extern crate test;
  10. use globset::{Candidate, Glob, GlobMatcher, GlobSet, GlobSetBuilder};
  11. const EXT: &'static str = "some/a/bigger/path/to/the/crazy/needle.txt";
  12. const EXT_PAT: &'static str = "*.txt";
  13. const SHORT: &'static str = "some/needle.txt";
  14. const SHORT_PAT: &'static str = "some/**/needle.txt";
  15. const LONG: &'static str = "some/a/bigger/path/to/the/crazy/needle.txt";
  16. const LONG_PAT: &'static str = "some/**/needle.txt";
  17. fn new_glob(pat: &str) -> glob::Pattern {
  18. glob::Pattern::new(pat).unwrap()
  19. }
  20. fn new_reglob(pat: &str) -> GlobMatcher {
  21. Glob::new(pat).unwrap().compile_matcher()
  22. }
  23. fn new_reglob_many(pats: &[&str]) -> GlobSet {
  24. let mut builder = GlobSetBuilder::new();
  25. for pat in pats {
  26. builder.add(Glob::new(pat).unwrap());
  27. }
  28. builder.build().unwrap()
  29. }
  30. #[bench]
  31. fn ext_glob(b: &mut test::Bencher) {
  32. let pat = new_glob(EXT_PAT);
  33. b.iter(|| assert!(pat.matches(EXT)));
  34. }
  35. #[bench]
  36. fn ext_regex(b: &mut test::Bencher) {
  37. let set = new_reglob(EXT_PAT);
  38. let cand = Candidate::new(EXT);
  39. b.iter(|| assert!(set.is_match_candidate(&cand)));
  40. }
  41. #[bench]
  42. fn short_glob(b: &mut test::Bencher) {
  43. let pat = new_glob(SHORT_PAT);
  44. b.iter(|| assert!(pat.matches(SHORT)));
  45. }
  46. #[bench]
  47. fn short_regex(b: &mut test::Bencher) {
  48. let set = new_reglob(SHORT_PAT);
  49. let cand = Candidate::new(SHORT);
  50. b.iter(|| assert!(set.is_match_candidate(&cand)));
  51. }
  52. #[bench]
  53. fn long_glob(b: &mut test::Bencher) {
  54. let pat = new_glob(LONG_PAT);
  55. b.iter(|| assert!(pat.matches(LONG)));
  56. }
  57. #[bench]
  58. fn long_regex(b: &mut test::Bencher) {
  59. let set = new_reglob(LONG_PAT);
  60. let cand = Candidate::new(LONG);
  61. b.iter(|| assert!(set.is_match_candidate(&cand)));
  62. }
  63. const MANY_SHORT_GLOBS: &'static [&'static str] = &[
  64. // Taken from a random .gitignore on my system.
  65. ".*.swp",
  66. "tags",
  67. "target",
  68. "*.lock",
  69. "tmp",
  70. "*.csv",
  71. "*.fst",
  72. "*-got",
  73. "*.csv.idx",
  74. "words",
  75. "98m*",
  76. "dict",
  77. "test",
  78. "months",
  79. ];
  80. const MANY_SHORT_SEARCH: &'static str = "98m-blah.csv.idx";
  81. #[bench]
  82. fn many_short_glob(b: &mut test::Bencher) {
  83. let pats: Vec<_> = MANY_SHORT_GLOBS.iter().map(|&s| new_glob(s)).collect();
  84. b.iter(|| {
  85. let mut count = 0;
  86. for pat in &pats {
  87. if pat.matches(MANY_SHORT_SEARCH) {
  88. count += 1;
  89. }
  90. }
  91. assert_eq!(2, count);
  92. })
  93. }
  94. #[bench]
  95. fn many_short_regex_set(b: &mut test::Bencher) {
  96. let set = new_reglob_many(MANY_SHORT_GLOBS);
  97. b.iter(|| assert_eq!(2, set.matches(MANY_SHORT_SEARCH).iter().count()));
  98. }