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 = 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 = 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. queries.push(line_res?);
  37. }
  38. Ok(queries)
  39. }
  40. fn run_bench(index_path: &Path,
  41. query_filepath: &Path,
  42. num_repeat: usize) -> Result<(), String> {
  43. println!("index_path : {:?}", index_path);
  44. println!("Query : {:?}", index_path);
  45. println!("-------------------------------\n\n\n");
  46. let index = Index::open(index_path).map_err(|e| format!("Failed to open index.\n{:?}", e))?;
  47. let searcher = index.searcher();
  48. let default_search_fields: Vec<Field> = extract_search_fields(&index.schema());
  49. let queries = read_query_file(query_filepath).map_err(|e| format!("Failed reading the query file: {}", e))?;
  50. let query_parser = QueryParser::new(index.schema(), default_search_fields, index.tokenizers().clone());
  51. println!("SEARCH\n");
  52. println!("{}\t{}\t{}\t{}", "query", "num_terms", "num hits", "time in microsecs");
  53. for _ in 0..num_repeat {
  54. for query_txt in &queries {
  55. let query = query_parser.parse_query(&query_txt).unwrap();
  56. // let num_terms = query.num_terms();
  57. let mut top_collector = TopCollector::with_limit(10);
  58. let mut count_collector = CountCollector::default();
  59. let timing;
  60. {
  61. let mut collector = chain().push(&mut top_collector).push(&mut count_collector);
  62. timing = query.search(&searcher, &mut collector)
  63. .map_err(|e| format!("Failed while searching query {:?}.\n\n{:?}", query_txt, e))?;
  64. }
  65. println!("{}\t{}\t{}", query_txt, count_collector.count(), timing.total_time());
  66. }
  67. }
  68. println!("\n\nFETCH STORE\n");
  69. println!("{}\t{}", "query", "time in microsecs");
  70. for _ in 0..num_repeat {
  71. for query_txt in &queries {
  72. let query = query_parser.parse_query(&query_txt).unwrap();
  73. let mut top_collector = TopCollector::with_limit(10);
  74. query.search(&searcher, &mut top_collector)
  75. .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. }