2021-09-22 12:10:21 +02:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2021-04-14 16:26:21 +02:00
|
|
|
use std::fs::{create_dir_all, remove_dir_all, File};
|
2022-07-12 15:22:09 +02:00
|
|
|
use std::io::{self, BufRead, BufReader, Cursor, Read, Seek};
|
2021-09-28 15:58:36 +02:00
|
|
|
use std::num::ParseFloatError;
|
2021-06-03 15:59:43 +02:00
|
|
|
use std::path::Path;
|
2023-01-11 12:14:17 +01:00
|
|
|
use std::str::FromStr;
|
2021-04-01 18:54:14 +02:00
|
|
|
|
2021-04-07 11:50:38 +02:00
|
|
|
use criterion::BenchmarkId;
|
2022-06-14 18:17:48 +02:00
|
|
|
use milli::documents::{DocumentsBatchBuilder, DocumentsBatchReader};
|
2022-08-11 11:15:46 +02:00
|
|
|
use milli::heed::EnvOpenOptions;
|
2021-12-08 14:12:07 +01:00
|
|
|
use milli::update::{
|
|
|
|
IndexDocuments, IndexDocumentsConfig, IndexDocumentsMethod, IndexerConfig, Settings,
|
|
|
|
};
|
2023-01-11 12:14:17 +01:00
|
|
|
use milli::{Criterion, Filter, Index, Object, TermsMatchingStrategy};
|
2022-06-15 15:36:27 +02:00
|
|
|
use serde_json::Value;
|
2021-04-01 18:54:14 +02:00
|
|
|
|
2021-04-07 11:50:38 +02:00
|
|
|
pub struct Conf<'a> {
|
2021-04-13 10:44:27 +02:00
|
|
|
/// where we are going to create our database.mmdb directory
|
|
|
|
/// each benchmark will first try to delete it and then recreate it
|
|
|
|
pub database_name: &'a str,
|
|
|
|
/// the dataset to be used, it must be an uncompressed csv
|
|
|
|
pub dataset: &'a str,
|
2021-09-13 18:08:28 +02:00
|
|
|
/// The format of the dataset
|
2021-09-22 12:10:21 +02:00
|
|
|
pub dataset_format: &'a str,
|
2021-04-07 11:50:38 +02:00
|
|
|
pub group_name: &'a str,
|
2021-04-14 13:13:33 +02:00
|
|
|
pub queries: &'a [&'a str],
|
2021-04-13 11:40:16 +02:00
|
|
|
/// here you can change which criterion are used and in which order.
|
|
|
|
/// - if you specify something all the base configuration will be thrown out
|
|
|
|
/// - if you don't specify anything (None) the default configuration will be kept
|
2021-04-07 11:50:38 +02:00
|
|
|
pub criterion: Option<&'a [&'a str]>,
|
2021-04-13 11:40:16 +02:00
|
|
|
/// the last chance to configure your database as you want
|
|
|
|
pub configure: fn(&mut Settings),
|
2021-06-03 10:33:42 +02:00
|
|
|
pub filter: Option<&'a str>,
|
2021-09-13 18:08:28 +02:00
|
|
|
pub sort: Option<Vec<&'a str>>,
|
2021-04-13 11:40:16 +02:00
|
|
|
/// enable or disable the optional words on the query
|
2021-04-07 11:50:38 +02:00
|
|
|
pub optional_words: bool,
|
2021-05-25 17:09:14 +02:00
|
|
|
/// primary key, if there is None we'll auto-generate docids for every documents
|
|
|
|
pub primary_key: Option<&'a str>,
|
2021-04-07 11:50:38 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 10:44:27 +02:00
|
|
|
impl Conf<'_> {
|
|
|
|
pub const BASE: Self = Conf {
|
|
|
|
database_name: "benches.mmdb",
|
2021-09-22 12:10:21 +02:00
|
|
|
dataset_format: "csv",
|
2021-04-13 10:44:27 +02:00
|
|
|
dataset: "",
|
|
|
|
group_name: "",
|
|
|
|
queries: &[],
|
|
|
|
criterion: None,
|
2021-06-02 17:09:21 +02:00
|
|
|
configure: |_| (),
|
2021-06-03 10:33:42 +02:00
|
|
|
filter: None,
|
2021-09-13 18:08:28 +02:00
|
|
|
sort: None,
|
2021-04-13 10:44:27 +02:00
|
|
|
optional_words: true,
|
2021-05-25 17:09:14 +02:00
|
|
|
primary_key: None,
|
2021-04-13 10:44:27 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:40:16 +02:00
|
|
|
pub fn base_setup(conf: &Conf) -> Index {
|
2023-01-17 18:01:26 +01:00
|
|
|
match remove_dir_all(conf.database_name) {
|
2021-04-13 10:44:27 +02:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => (),
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
}
|
2023-01-17 18:01:26 +01:00
|
|
|
create_dir_all(conf.database_name).unwrap();
|
2021-04-01 18:54:14 +02:00
|
|
|
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
options.max_readers(10);
|
2021-04-13 11:40:16 +02:00
|
|
|
let index = Index::new(options, conf.database_name).unwrap();
|
2021-04-01 18:54:14 +02:00
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
2021-04-01 18:54:14 +02:00
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
2021-04-01 18:54:14 +02:00
|
|
|
|
2021-06-15 13:45:20 +02:00
|
|
|
if let Some(primary_key) = conf.primary_key {
|
|
|
|
builder.set_primary_key(primary_key.to_string());
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:40:16 +02:00
|
|
|
if let Some(criterion) = conf.criterion {
|
2021-06-03 10:33:42 +02:00
|
|
|
builder.reset_filterable_fields();
|
2021-04-01 18:54:14 +02:00
|
|
|
builder.reset_criteria();
|
|
|
|
builder.reset_stop_words();
|
|
|
|
|
2023-01-11 12:14:17 +01:00
|
|
|
let criterion = criterion.iter().map(|s| Criterion::from_str(s).unwrap()).collect();
|
2021-04-07 11:50:38 +02:00
|
|
|
builder.set_criteria(criterion);
|
2021-04-01 18:54:14 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 11:40:16 +02:00
|
|
|
(conf.configure)(&mut builder);
|
|
|
|
|
2022-10-05 17:41:07 +02:00
|
|
|
builder.execute(|_| (), || false).unwrap();
|
2021-04-01 18:54:14 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
2021-04-01 18:54:14 +02:00
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
let indexing_config = IndexDocumentsConfig {
|
|
|
|
autogenerate_docids: conf.primary_key.is_none(),
|
|
|
|
update_method: IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-10-05 17:41:07 +02:00
|
|
|
let builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| (), || false).unwrap();
|
2021-09-22 12:10:21 +02:00
|
|
|
let documents = documents_from(conf.dataset, conf.dataset_format);
|
2022-06-15 15:36:27 +02:00
|
|
|
let (builder, user_error) = builder.add_documents(documents).unwrap();
|
|
|
|
user_error.unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
builder.execute().unwrap();
|
2021-04-01 18:54:14 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
index
|
|
|
|
}
|
2021-04-07 11:50:38 +02:00
|
|
|
|
|
|
|
pub fn run_benches(c: &mut criterion::Criterion, confs: &[Conf]) {
|
|
|
|
for conf in confs {
|
2021-04-13 11:40:16 +02:00
|
|
|
let index = base_setup(conf);
|
2021-04-07 11:50:38 +02:00
|
|
|
|
2021-06-03 15:59:43 +02:00
|
|
|
let file_name = Path::new(conf.dataset).file_name().and_then(|f| f.to_str()).unwrap();
|
|
|
|
let name = format!("{}: {}", file_name, conf.group_name);
|
|
|
|
let mut group = c.benchmark_group(&name);
|
2021-04-07 11:50:38 +02:00
|
|
|
|
|
|
|
for &query in conf.queries {
|
|
|
|
group.bench_with_input(BenchmarkId::from_parameter(query), &query, |b, &query| {
|
|
|
|
b.iter(|| {
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
2021-04-13 10:44:27 +02:00
|
|
|
let mut search = index.search(&rtxn);
|
2022-08-22 17:37:36 +02:00
|
|
|
search.query(query).terms_matching_strategy(TermsMatchingStrategy::default());
|
2021-06-03 10:33:42 +02:00
|
|
|
if let Some(filter) = conf.filter {
|
2021-12-09 12:14:16 +01:00
|
|
|
let filter = Filter::from_str(filter).unwrap().unwrap();
|
2021-06-03 10:33:42 +02:00
|
|
|
search.filter(filter);
|
2021-04-13 10:44:27 +02:00
|
|
|
}
|
2021-09-13 18:08:28 +02:00
|
|
|
if let Some(sort) = &conf.sort {
|
|
|
|
let sort = sort.iter().map(|sort| sort.parse().unwrap()).collect();
|
|
|
|
search.sort_criteria(sort);
|
|
|
|
}
|
2021-04-13 10:44:27 +02:00
|
|
|
let _ids = search.execute().unwrap();
|
2021-04-07 11:50:38 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
group.finish();
|
2021-07-29 14:31:00 +02:00
|
|
|
|
|
|
|
index.prepare_for_closing().wait();
|
2021-04-07 11:50:38 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-22 12:10:21 +02:00
|
|
|
|
2022-07-12 15:22:09 +02:00
|
|
|
pub fn documents_from(filename: &str, filetype: &str) -> DocumentsBatchReader<impl BufRead + Seek> {
|
2023-01-17 18:02:55 +01:00
|
|
|
let reader = File::open(filename)
|
|
|
|
.unwrap_or_else(|_| panic!("could not find the dataset in: {}", filename));
|
2022-07-06 18:20:15 +02:00
|
|
|
let reader = BufReader::new(reader);
|
2021-09-22 12:10:21 +02:00
|
|
|
let documents = match filetype {
|
|
|
|
"csv" => documents_from_csv(reader).unwrap(),
|
|
|
|
"json" => documents_from_json(reader).unwrap(),
|
|
|
|
"jsonl" => documents_from_jsonl(reader).unwrap(),
|
|
|
|
otherwise => panic!("invalid update format {:?}", otherwise),
|
|
|
|
};
|
2022-06-14 18:17:48 +02:00
|
|
|
DocumentsBatchReader::from_reader(Cursor::new(documents)).unwrap()
|
2021-09-22 12:10:21 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 18:17:48 +02:00
|
|
|
fn documents_from_jsonl(reader: impl BufRead) -> anyhow::Result<Vec<u8>> {
|
|
|
|
let mut documents = DocumentsBatchBuilder::new(Vec::new());
|
2021-09-22 12:10:21 +02:00
|
|
|
|
2022-06-15 15:36:27 +02:00
|
|
|
for result in serde_json::Deserializer::from_reader(reader).into_iter::<Object>() {
|
2022-06-14 18:17:48 +02:00
|
|
|
let object = result?;
|
|
|
|
documents.append_json_object(&object)?;
|
2021-09-22 12:10:21 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 18:17:48 +02:00
|
|
|
documents.into_inner().map_err(Into::into)
|
2021-09-22 12:10:21 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 18:20:15 +02:00
|
|
|
fn documents_from_json(reader: impl BufRead) -> anyhow::Result<Vec<u8>> {
|
2022-06-14 18:17:48 +02:00
|
|
|
let mut documents = DocumentsBatchBuilder::new(Vec::new());
|
2021-09-22 12:10:21 +02:00
|
|
|
|
2022-07-11 18:38:50 +02:00
|
|
|
documents.append_json_array(reader)?;
|
2021-09-22 12:10:21 +02:00
|
|
|
|
2022-06-14 18:17:48 +02:00
|
|
|
documents.into_inner().map_err(Into::into)
|
2021-09-22 12:10:21 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 18:20:15 +02:00
|
|
|
fn documents_from_csv(reader: impl BufRead) -> anyhow::Result<Vec<u8>> {
|
2022-06-14 18:17:48 +02:00
|
|
|
let csv = csv::Reader::from_reader(reader);
|
|
|
|
|
|
|
|
let mut documents = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
documents.append_csv(csv)?;
|
2021-09-22 12:10:21 +02:00
|
|
|
|
2022-06-14 18:17:48 +02:00
|
|
|
documents.into_inner().map_err(Into::into)
|
2021-09-22 12:10:21 +02:00
|
|
|
}
|
2021-09-28 15:58:36 +02:00
|
|
|
|
|
|
|
enum AllowedType {
|
|
|
|
String,
|
|
|
|
Number,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_csv_header(header: &str) -> (String, AllowedType) {
|
|
|
|
// if there are several separators we only split on the last one.
|
|
|
|
match header.rsplit_once(':') {
|
|
|
|
Some((field_name, field_type)) => match field_type {
|
|
|
|
"string" => (field_name.to_string(), AllowedType::String),
|
|
|
|
"number" => (field_name.to_string(), AllowedType::Number),
|
|
|
|
// we may return an error in this case.
|
|
|
|
_otherwise => (header.to_string(), AllowedType::String),
|
|
|
|
},
|
|
|
|
None => (header.to_string(), AllowedType::String),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CSVDocumentDeserializer<R>
|
|
|
|
where
|
|
|
|
R: Read,
|
|
|
|
{
|
|
|
|
documents: csv::StringRecordsIntoIter<R>,
|
|
|
|
headers: Vec<(String, AllowedType)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Read> CSVDocumentDeserializer<R> {
|
|
|
|
fn from_reader(reader: R) -> io::Result<Self> {
|
|
|
|
let mut records = csv::Reader::from_reader(reader);
|
|
|
|
|
|
|
|
let headers = records.headers()?.into_iter().map(parse_csv_header).collect();
|
|
|
|
|
|
|
|
Ok(Self { documents: records.into_records(), headers })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Read> Iterator for CSVDocumentDeserializer<R> {
|
2022-06-15 15:36:27 +02:00
|
|
|
type Item = anyhow::Result<Object>;
|
2021-09-28 15:58:36 +02:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let csv_document = self.documents.next()?;
|
|
|
|
|
|
|
|
match csv_document {
|
|
|
|
Ok(csv_document) => {
|
2022-06-15 15:36:27 +02:00
|
|
|
let mut document = Object::new();
|
2021-09-28 15:58:36 +02:00
|
|
|
|
|
|
|
for ((field_name, field_type), value) in
|
|
|
|
self.headers.iter().zip(csv_document.into_iter())
|
|
|
|
{
|
|
|
|
let parsed_value: Result<Value, ParseFloatError> = match field_type {
|
|
|
|
AllowedType::Number => {
|
|
|
|
value.parse::<f64>().map(Value::from).map_err(Into::into)
|
|
|
|
}
|
|
|
|
AllowedType::String => Ok(Value::String(value.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
match parsed_value {
|
|
|
|
Ok(value) => drop(document.insert(field_name.to_string(), value)),
|
|
|
|
Err(_e) => {
|
|
|
|
return Some(Err(anyhow::anyhow!(
|
|
|
|
"Value '{}' is not a valid number",
|
|
|
|
value
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Ok(document))
|
|
|
|
}
|
|
|
|
Err(e) => Some(Err(anyhow::anyhow!("Error parsing csv document: {}", e))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|