From 8fab80048cce6f00238c08c428e1ca98395baec0 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Sun, 24 Jun 2018 15:10:13 +0200 Subject: [PATCH] test: Add a raptor-search bench --- raptor-search/benches/main.rs | 31 +++++++++++++++++++++++++++++++ raptor-search/src/main.rs | 11 ++++------- src/lib.rs | 12 ++++++++++++ src/rank.rs | 2 +- 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 raptor-search/benches/main.rs diff --git a/raptor-search/benches/main.rs b/raptor-search/benches/main.rs new file mode 100644 index 000000000..e761252d5 --- /dev/null +++ b/raptor-search/benches/main.rs @@ -0,0 +1,31 @@ +#![feature(test)] + +extern crate test; +extern crate fst; +extern crate raptor; + +use std::path::Path; +use std::{fs, env, io}; +use fst::Streamer; +use raptor::{load_map, DocIndexMap, RankedStream, LevBuilder}; + +#[bench] +fn chauve_souris(b: &mut test::Bencher) { + let lev_builder = LevBuilder::new(); + let map = load_map("map.fst", "values.vecs").unwrap(); + + let query = "chauve souris"; + + b.iter(|| { + let mut automatons = Vec::new(); + for query in query.split_whitespace() { + let lev = lev_builder.build_automaton(query); + automatons.push(lev); + } + + let mut stream = RankedStream::new(&map, &map.values(), automatons); + while let Some(document_id) = stream.next() { + test::black_box(document_id); + } + }) +} diff --git a/raptor-search/src/main.rs b/raptor-search/src/main.rs index 6e987e2f1..ed57b507f 100644 --- a/raptor-search/src/main.rs +++ b/raptor-search/src/main.rs @@ -2,19 +2,16 @@ extern crate env_logger; extern crate fst; extern crate raptor; -use std::{fs, env}; +use std::path::Path; +use std::{fs, env, io}; use fst::Streamer; -use raptor::{DocIndexMap, RankedStream, LevBuilder}; +use raptor::{load_map, DocIndexMap, RankedStream, LevBuilder}; fn main() { drop(env_logger::init()); let lev_builder = LevBuilder::new(); - let map = { - let fst = fs::read("map.fst").unwrap(); - let values = fs::read("values.vecs").unwrap(); - DocIndexMap::from_bytes(fst, &values).unwrap() - }; + let map = load_map("map.fst", "values.vecs").unwrap(); let query = env::args().nth(1).expect("Please enter query words!"); let query = query.to_lowercase(); diff --git a/src/lib.rs b/src/lib.rs index eb1ec0402..f97326611 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,9 @@ pub mod map; pub mod rank; mod levenshtein; +use std::path::Path; +use std::fs; + pub use self::map::{Map, MapBuilder, Values}; pub use self::map::{ OpBuilder, IndexedValues, @@ -101,3 +104,12 @@ impl Match { } } } + + +pub fn load_map(map: P, values: Q) -> fst::Result +where P: AsRef, Q: AsRef, +{ + let fst = fs::read(map)?; + let values = fs::read(values)?; + DocIndexMap::from_bytes(fst, &values) +} diff --git a/src/rank.rs b/src/rank.rs index cc2ab94ae..d1dfb0336 100644 --- a/src/rank.rs +++ b/src/rank.rs @@ -334,7 +334,7 @@ impl<'m, 'v, 'a> fst::Streamer<'a> for RankedStream<'m, 'v> { // TODO remove the Pool system ! // this is an internal Pool rule but // it is more efficient to test that here - if pool.limitation.reached().is_some() && distance != 0 { continue } + if pool.limitation.is_reached() && distance != 0 { continue } let mut matches = HashMap::with_capacity(iv.values.len() / 2); for di in iv.values {