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.

102 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(field_id as u32))
  29. .collect()
  30. }
  31. fn read_query_file(query_path: &Path) -> io::Result<Vec<String>> {
  32. let query_file: File = try!(File::open(&query_path));
  33. let file = BufReader::new(&query_file);
  34. let mut queries = Vec::new();
  35. for line_res in file.lines() {
  36. let line = try!(line_res);
  37. let query = String::from(line.trim());
  38. queries.push(query);
  39. }
  40. Ok(queries)
  41. }
  42. fn run_bench(index_path: &Path,
  43. query_filepath: &Path,
  44. num_repeat: usize) -> Result<(), String> {
  45. println!("index_path : {:?}", index_path);
  46. println!("Query : {:?}", index_path);
  47. println!("-------------------------------\n\n\n");
  48. let index = try!(Index::open(index_path).map_err(|e| format!("Failed to open index.\n{:?}", e)));
  49. let searcher = index.searcher();
  50. let default_search_fields: Vec<Field> = extract_search_fields(&index.schema());
  51. let queries = try!(read_query_file(query_filepath).map_err(|e| format!("Failed reading the query file: {}", e)));
  52. let query_parser = QueryParser::new(index.schema(), default_search_fields);
  53. println!("SEARCH\n");
  54. println!("{}\t{}\t{}\t{}", "query", "num_terms", "num hits", "time in microsecs");
  55. for _ in 0..num_repeat {
  56. for query_txt in &queries {
  57. let query = query_parser.parse_query(&query_txt).unwrap();
  58. // let num_terms = query.num_terms();
  59. let mut top_collector = TopCollector::with_limit(10);
  60. let mut count_collector = CountCollector::default();
  61. let timing;
  62. {
  63. let mut collector = chain().push(&mut top_collector).push(&mut count_collector);
  64. timing = try!(query.search(&searcher, &mut collector).map_err(|e| format!("Failed while searching query {:?}.\n\n{:?}", query_txt, e)));
  65. }
  66. println!("{}\t{}\t{}", query_txt, count_collector.count(), timing.total_time());
  67. }
  68. }
  69. println!("\n\nFETCH STORE\n");
  70. println!("{}\t{}", "query", "time in microsecs");
  71. for _ in 0..num_repeat {
  72. for query_txt in &queries {
  73. let query = query_parser.parse_query(&query_txt).unwrap();
  74. let mut top_collector = TopCollector::with_limit(10);
  75. try!(query.search(&searcher, &mut top_collector).map_err(|e| format!("Failed while retrieving document for query {:?}.\n{:?}", query, e)));
  76. let mut timer = TimerTree::default();
  77. {
  78. let _scoped_timer_ = timer.open("total");
  79. for doc_address in top_collector.docs() {
  80. searcher.doc(&doc_address).unwrap();
  81. }
  82. }
  83. println!("{}\t{}", query_txt, timer.total_time());
  84. }
  85. }
  86. Ok(())
  87. }