You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.8KB

  1. use tantivy::Index;
  2. use tantivy::schema::{Field, Schema};
  3. use tantivy::query::QueryParser;
  4. use std::path::Path;
  5. use tantivy::TimerTree;
  6. use std::io::BufReader;
  7. use std::io::BufRead;
  8. use std::io;
  9. use std::fs::File;
  10. use tantivy::collector::chain;
  11. use tantivy::collector::TopCollector;
  12. use tantivy::collector::CountCollector;
  13. use clap::ArgMatches;
  14. use std::path::PathBuf;
  15. pub fn run_bench_cli(matches: &ArgMatches) -> Result<(), String> {
  16. let index_path = PathBuf::from(matches.value_of("index").unwrap());
  17. let queries_path = PathBuf::from(matches.value_of("queries").unwrap()); // the unwrap is safe as long as it is comming from the main cli.
  18. let num_repeat = try!(value_t!(matches, "num_repeat", usize).map_err(|e|format!("Failed to read num_repeat argument as an integer. {:?}", e)));
  19. run_bench(&index_path, &queries_path, num_repeat).map_err(From::from)
  20. }
  21. fn extract_search_fields(schema: &Schema) -> Vec<Field> {
  22. schema.fields()
  23. .iter()
  24. .enumerate()
  25. .filter(|&(_, field_entry)| {
  26. field_entry.is_indexed()
  27. })
  28. .map(|(field_id, _)| field_id as u8)
  29. .map(Field)
  30. .collect()
  31. }
  32. fn read_query_file(query_path: &Path) -> io::Result<Vec<String>> {
  33. let query_file: File = try!(File::open(&query_path));
  34. let file = BufReader::new(&query_file);
  35. let mut queries = Vec::new();
  36. for line_res in file.lines() {
  37. let line = try!(line_res);
  38. let query = String::from(line.trim());
  39. queries.push(query);
  40. }
  41. Ok(queries)
  42. }
  43. fn run_bench(index_path: &Path,
  44. query_filepath: &Path,
  45. num_repeat: usize) -> Result<(), String> {
  46. println!("index_path : {:?}", index_path);
  47. println!("Query : {:?}", index_path);
  48. println!("-------------------------------\n\n\n");
  49. let index = try!(Index::open(index_path).map_err(|e| format!("Failed to open index.\n{:?}", e)));
  50. let searcher = index.searcher();
  51. let default_search_fields: Vec<Field> = extract_search_fields(&index.schema());
  52. let queries = try!(read_query_file(query_filepath).map_err(|e| format!("Failed reading the query file: {}", e)));
  53. let query_parser = QueryParser::new(index.schema(), default_search_fields);
  54. println!("SEARCH\n");
  55. println!("{}\t{}\t{}\t{}", "query", "num_terms", "num hits", "time in microsecs");
  56. for _ in 0..num_repeat {
  57. for query_txt in &queries {
  58. let query = query_parser.parse_query(&query_txt).unwrap();
  59. // let num_terms = query.num_terms();
  60. let mut top_collector = TopCollector::with_limit(10);
  61. let mut count_collector = CountCollector::default();
  62. let timing;
  63. {
  64. let mut collector = chain().push(&mut top_collector).push(&mut count_collector);
  65. timing = try!(query.search(&searcher, &mut collector).map_err(|e| format!("Failed while searching query {:?}.\n\n{:?}", query_txt, e)));
  66. }
  67. println!("{}\t{}\t{}", query_txt, count_collector.count(), timing.total_time());
  68. }
  69. }
  70. println!("\n\nFETCH STORE\n");
  71. println!("{}\t{}", "query", "time in microsecs");
  72. for _ in 0..num_repeat {
  73. for query_txt in &queries {
  74. let query = query_parser.parse_query(&query_txt).unwrap();
  75. let mut top_collector = TopCollector::with_limit(10);
  76. try!(query.search(&searcher, &mut top_collector).map_err(|e| format!("Failed while retrieving document for query {:?}.\n{:?}", query, e)));
  77. let mut timer = TimerTree::default();
  78. {
  79. let _scoped_timer_ = timer.open("total");
  80. for doc_address in top_collector.docs() {
  81. searcher.doc(&doc_address).unwrap();
  82. }
  83. }
  84. println!("{}\t{}", query_txt, timer.total_time());
  85. }
  86. }
  87. Ok(())
  88. }