2019-11-29 12:12:55 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate assert_matches;
|
|
|
|
|
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::path::Path;
|
2020-04-02 10:51:56 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufReader;
|
2019-11-29 12:12:55 +01:00
|
|
|
use std::iter;
|
|
|
|
|
2020-05-18 10:56:24 +02:00
|
|
|
use meilisearch_core::{Database, DatabaseOptions};
|
2019-11-29 12:12:55 +01:00
|
|
|
use meilisearch_core::{ProcessedUpdateResult, UpdateStatus};
|
2020-04-02 10:51:56 +01:00
|
|
|
use meilisearch_core::settings::{Settings, SettingsUpdate};
|
2020-03-31 23:06:39 +01:00
|
|
|
use meilisearch_schema::Schema;
|
2019-11-29 12:12:55 +01:00
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId};
|
|
|
|
|
|
|
|
fn prepare_database(path: &Path) -> Database {
|
2020-05-18 10:56:24 +02:00
|
|
|
let database = Database::open_or_create(path, DatabaseOptions::default()).unwrap();
|
2019-11-29 12:12:55 +01:00
|
|
|
let db = &database;
|
|
|
|
|
|
|
|
let (sender, receiver) = mpsc::sync_channel(100);
|
|
|
|
let update_fn = move |_name: &str, update: ProcessedUpdateResult| {
|
|
|
|
sender.send(update.update_id).unwrap()
|
|
|
|
};
|
|
|
|
let index = database.create_index("bench").unwrap();
|
|
|
|
|
|
|
|
database.set_update_callback(Box::new(update_fn));
|
2020-05-18 10:56:24 +02:00
|
|
|
|
2020-03-31 23:06:39 +01:00
|
|
|
let mut writer = db.main_write_txn().unwrap();
|
|
|
|
index.main.put_schema(&mut writer, &Schema::with_primary_key("id")).unwrap();
|
|
|
|
writer.commit().unwrap();
|
2019-11-29 12:12:55 +01:00
|
|
|
|
2020-03-31 23:06:39 +01:00
|
|
|
let settings_update: SettingsUpdate = {
|
|
|
|
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../datasets/movies/settings.json");
|
|
|
|
let file = File::open(path).unwrap();
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
let settings: Settings = serde_json::from_reader(reader).unwrap();
|
|
|
|
settings.into_update().unwrap()
|
2019-11-29 12:12:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut update_writer = db.update_write_txn().unwrap();
|
2020-03-31 23:06:39 +01:00
|
|
|
let _update_id = index.settings_update(&mut update_writer, settings_update).unwrap();
|
2019-11-29 12:12:55 +01:00
|
|
|
update_writer.commit().unwrap();
|
|
|
|
|
|
|
|
let mut additions = index.documents_addition();
|
|
|
|
|
|
|
|
let json: Value = {
|
|
|
|
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../datasets/movies/movies.json");
|
2020-04-02 10:51:56 +01:00
|
|
|
let movies_file = File::open(path).expect("find movies");
|
2019-11-29 12:12:55 +01:00
|
|
|
serde_json::from_reader(movies_file).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let documents = json.as_array().unwrap();
|
|
|
|
|
|
|
|
for document in documents {
|
|
|
|
additions.update_document(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut update_writer = db.update_write_txn().unwrap();
|
|
|
|
let update_id = additions.finalize(&mut update_writer).unwrap();
|
|
|
|
update_writer.commit().unwrap();
|
|
|
|
|
|
|
|
// block until the transaction is processed
|
|
|
|
let _ = receiver.into_iter().find(|id| *id == update_id);
|
|
|
|
|
|
|
|
let update_reader = db.update_read_txn().unwrap();
|
|
|
|
let result = index.update_status(&update_reader, update_id).unwrap();
|
|
|
|
assert_matches!(result, Some(UpdateStatus::Processed { content }) if content.error.is_none());
|
|
|
|
|
|
|
|
database
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
let database = prepare_database(dir.path());
|
|
|
|
|
|
|
|
let reader = database.main_read_txn().unwrap();
|
|
|
|
let index = database.open_index("bench").unwrap();
|
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
let query = "I love paris ";
|
|
|
|
|
|
|
|
let iter = iter::from_fn(|| {
|
|
|
|
count += 1;
|
|
|
|
query.get(0..count)
|
|
|
|
});
|
|
|
|
|
2019-11-29 12:28:46 +01:00
|
|
|
let mut group = c.benchmark_group("searching in movies (19654 docs)");
|
2019-11-29 12:12:55 +01:00
|
|
|
group.sample_size(10);
|
|
|
|
|
|
|
|
for query in iter {
|
2019-11-29 12:28:46 +01:00
|
|
|
let bench_name = BenchmarkId::from_parameter(format!("{:?}", query));
|
2019-11-29 12:12:55 +01:00
|
|
|
group.bench_with_input(bench_name, &query, |b, query| b.iter(|| {
|
|
|
|
let builder = index.query_builder();
|
|
|
|
builder.query(&reader, query, 0..20).unwrap();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
group.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|