From dfa19582a24cd3b87ec145833694e9fce07f4856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sun, 30 Dec 2018 13:04:22 +0100 Subject: [PATCH] test: Add benchmarks to mesure the words proximity criterion --- src/rank/criterion/words_proximity.rs | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/rank/criterion/words_proximity.rs b/src/rank/criterion/words_proximity.rs index f4b3aa0cd..fc80dfaec 100644 --- a/src/rank/criterion/words_proximity.rs +++ b/src/rank/criterion/words_proximity.rs @@ -121,3 +121,42 @@ mod tests { assert_eq!(matches_proximity(matches), 3); } } + +#[cfg(all(feature = "nightly", test))] +mod bench { + extern crate test; + + use super::*; + use std::error::Error; + use self::test::Bencher; + + use rand_xorshift::XorShiftRng; + use rand::{Rng, SeedableRng}; + + use crate::Attribute; + + #[bench] + fn evaluate_proximity(bench: &mut Bencher) -> Result<(), Box> { + let number_matches = 30_000; + let mut matches = Vec::with_capacity(number_matches); + let mut rng = XorShiftRng::seed_from_u64(42); + + for _ in 0..number_matches { + let query_index = rng.gen_range(0, 4); + + let attribute = rng.gen_range(0, 5); + let word_index = rng.gen_range(0, 15); + let attribute = Attribute::new_faillible(attribute, word_index); + + let match_ = Match { query_index, attribute, ..Match::zero() }; + matches.push(match_); + } + + bench.iter(|| { + let proximity = matches_proximity(&matches); + test::black_box(move || proximity) + }); + + Ok(()) + } +}