2023-07-11 18:22:31 +00:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
|
|
|
|
use rand::prelude::SliceRandom;
|
|
|
|
use rand::thread_rng;
|
2023-10-26 21:33:06 +00:00
|
|
|
use std::fmt::Debug;
|
2023-07-11 18:22:31 +00:00
|
|
|
use std::time::Duration;
|
2023-08-01 09:57:05 +00:00
|
|
|
use surrealdb::idx::trees::bkeys::{BKeys, FstKeys, TrieKeys};
|
|
|
|
use surrealdb::idx::trees::btree::{BState, BTree, Payload};
|
2023-12-13 13:37:24 +00:00
|
|
|
use surrealdb::idx::trees::store::cache::TreeCache;
|
|
|
|
use surrealdb::idx::trees::store::{TreeNodeProvider, TreeStore};
|
2023-09-26 13:02:53 +00:00
|
|
|
use surrealdb::kvs::{Datastore, Key, LockType::*, TransactionType::*};
|
2023-11-13 17:19:47 +00:00
|
|
|
use tokio::runtime::Runtime;
|
2023-07-11 18:22:31 +00:00
|
|
|
macro_rules! get_key_value {
|
|
|
|
($idx:expr) => {{
|
|
|
|
(format!("{}", $idx).into(), ($idx * 10) as Payload)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bench_index_btree(c: &mut Criterion) {
|
|
|
|
let (samples_len, samples) = setup();
|
|
|
|
|
|
|
|
let mut group = c.benchmark_group("index_btree");
|
2024-04-30 18:09:54 +00:00
|
|
|
group.throughput(Throughput::Elements(samples_len as u64));
|
2023-07-11 18:22:31 +00:00
|
|
|
group.sample_size(10);
|
|
|
|
group.measurement_time(Duration::from_secs(30));
|
|
|
|
|
2023-08-01 09:57:05 +00:00
|
|
|
group.bench_function("trees-insertion-fst", |b| {
|
2023-11-13 17:19:47 +00:00
|
|
|
b.to_async(Runtime::new().unwrap())
|
2023-12-13 13:37:24 +00:00
|
|
|
.iter(|| bench::<_, FstKeys>(samples_len, 100, |i| get_key_value!(samples[i])))
|
2023-07-11 18:22:31 +00:00
|
|
|
});
|
|
|
|
|
2023-08-01 09:57:05 +00:00
|
|
|
group.bench_function("trees-insertion-trie", |b| {
|
2023-11-13 17:19:47 +00:00
|
|
|
b.to_async(Runtime::new().unwrap())
|
2023-12-13 13:37:24 +00:00
|
|
|
.iter(|| bench::<_, TrieKeys>(samples_len, 100, |i| get_key_value!(samples[i])))
|
|
|
|
});
|
|
|
|
|
|
|
|
group.bench_function("trees-insertion-fst-fullcache", |b| {
|
|
|
|
b.to_async(Runtime::new().unwrap())
|
|
|
|
.iter(|| bench::<_, FstKeys>(samples_len, 0, |i| get_key_value!(samples[i])))
|
|
|
|
});
|
|
|
|
|
|
|
|
group.bench_function("trees-insertion-trie-fullcache", |b| {
|
|
|
|
b.to_async(Runtime::new().unwrap())
|
|
|
|
.iter(|| bench::<_, TrieKeys>(samples_len, 0, |i| get_key_value!(samples[i])))
|
2023-07-11 18:22:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup() -> (usize, Vec<usize>) {
|
|
|
|
let samples_len = if cfg!(debug_assertions) {
|
|
|
|
1000 // debug is much slower!
|
|
|
|
} else {
|
|
|
|
100_000
|
|
|
|
};
|
|
|
|
let mut samples: Vec<usize> = (0..samples_len).collect();
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
samples.shuffle(&mut rng);
|
|
|
|
(samples_len, samples)
|
|
|
|
}
|
|
|
|
|
2023-12-13 13:37:24 +00:00
|
|
|
async fn bench<F, BK>(samples_size: usize, cache_size: usize, sample_provider: F)
|
2023-07-11 18:22:31 +00:00
|
|
|
where
|
|
|
|
F: Fn(usize) -> (Key, Payload),
|
2023-12-13 13:37:24 +00:00
|
|
|
BK: BKeys + Clone + Default + Debug,
|
2023-07-11 18:22:31 +00:00
|
|
|
{
|
|
|
|
let ds = Datastore::new("memory").await.unwrap();
|
2023-09-26 13:02:53 +00:00
|
|
|
let mut tx = ds.transaction(Write, Optimistic).await.unwrap();
|
2023-08-01 09:57:05 +00:00
|
|
|
let mut t = BTree::<BK>::new(BState::new(100));
|
2024-04-30 18:09:54 +00:00
|
|
|
let np = TreeNodeProvider::Debug;
|
|
|
|
let c = TreeCache::new(0, np.get_key(0), np.clone(), cache_size);
|
|
|
|
let mut s = TreeStore::new(np, c.into(), Write).await;
|
2023-07-11 18:22:31 +00:00
|
|
|
for i in 0..samples_size {
|
|
|
|
let (key, payload) = sample_provider(i);
|
|
|
|
// Insert the sample
|
|
|
|
t.insert(&mut tx, &mut s, key.clone(), payload).await.unwrap();
|
|
|
|
// Search for it
|
2023-12-13 13:37:24 +00:00
|
|
|
black_box(t.search_mut(&mut tx, &mut s, &key).await.unwrap());
|
2023-07-11 18:22:31 +00:00
|
|
|
}
|
2023-12-13 13:37:24 +00:00
|
|
|
s.finish(&mut tx).await.unwrap();
|
2023-08-16 17:56:17 +00:00
|
|
|
tx.commit().await.unwrap();
|
2023-07-11 18:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, bench_index_btree);
|
|
|
|
criterion_main!(benches);
|