cuprate-hinto-janai/database/benchmark/benches/env.rs

162 lines
4.2 KiB
Rust
Raw Normal View History

2024-04-16 20:53:24 +00:00
//! TODO
//---------------------------------------------------------------------------------------------------- Import
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2024-04-16 21:39:53 +00:00
use function_name::named;
2024-04-16 21:13:04 +00:00
use cuprate_database::{
2024-04-20 00:30:43 +00:00
config::Config,
2024-04-16 21:13:04 +00:00
resize::{page_size, ResizeAlgorithm},
tables::Outputs,
ConcreteEnv, Env, EnvInner, TxRo, TxRw,
};
2024-04-16 20:53:24 +00:00
2024-04-16 21:13:04 +00:00
use cuprate_database_benchmark::tmp_concrete_env;
2024-04-16 20:53:24 +00:00
2024-04-16 21:39:53 +00:00
//---------------------------------------------------------------------------------------------------- Criterion
criterion_group!(
benches,
open,
2024-04-20 00:30:43 +00:00
env_inner,
2024-04-16 21:39:53 +00:00
tx_ro,
tx_rw,
open_tables,
open_tables_mut,
2024-04-20 00:30:43 +00:00
resize,
current_map_size,
disk_size_bytes,
2024-04-16 21:39:53 +00:00
);
criterion_main!(benches);
2024-04-16 21:13:04 +00:00
//---------------------------------------------------------------------------------------------------- Env benchmarks
2024-04-16 20:53:24 +00:00
/// [`Env::open`].
2024-04-16 21:39:53 +00:00
#[named]
2024-04-16 20:53:24 +00:00
fn open(c: &mut Criterion) {
2024-04-20 00:30:43 +00:00
let tempdir = tempfile::tempdir().unwrap();
let config = Config::low_power(Some(tempdir.path().into()));
c.bench_function(function_name!(), |b| {
b.iter_with_large_drop(|| {
ConcreteEnv::open(config.clone()).unwrap();
});
});
}
/// [`Env::env_inner`].
#[named]
fn env_inner(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
2024-04-16 21:39:53 +00:00
c.bench_function(function_name!(), |b| {
2024-04-16 21:13:04 +00:00
b.iter(|| {
2024-04-20 00:30:43 +00:00
black_box(env.env_inner());
2024-04-16 21:13:04 +00:00
});
});
2024-04-16 20:53:24 +00:00
}
/// Create and commit read-only transactions.
2024-04-16 21:39:53 +00:00
#[named]
2024-04-16 20:53:24 +00:00
fn tx_ro(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
2024-04-16 21:39:53 +00:00
c.bench_function(function_name!(), |b| {
2024-04-16 20:53:24 +00:00
b.iter(|| {
2024-04-16 21:39:53 +00:00
let tx_ro = black_box(env_inner.tx_ro()).unwrap();
TxRo::commit(black_box(tx_ro)).unwrap();
2024-04-16 21:13:04 +00:00
});
2024-04-16 20:53:24 +00:00
});
}
/// Create and commit read/write transactions.
2024-04-16 21:39:53 +00:00
#[named]
2024-04-16 20:53:24 +00:00
fn tx_rw(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
2024-04-16 21:39:53 +00:00
c.bench_function(function_name!(), |b| {
2024-04-16 20:53:24 +00:00
b.iter(|| {
2024-04-16 21:39:53 +00:00
let tx_rw = black_box(env_inner.tx_rw()).unwrap();
TxRw::commit(black_box(tx_rw)).unwrap();
2024-04-16 21:13:04 +00:00
});
2024-04-16 20:53:24 +00:00
});
}
/// Open all database tables in read-only mode.
2024-04-16 21:39:53 +00:00
#[named]
2024-04-16 20:53:24 +00:00
fn open_tables(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_ro = env_inner.tx_ro().unwrap();
2024-04-16 21:39:53 +00:00
c.bench_function(function_name!(), |b| {
2024-04-16 20:53:24 +00:00
b.iter(|| {
2024-04-16 21:39:53 +00:00
black_box(env_inner.open_db_ro::<Outputs>(&tx_ro)).unwrap();
2024-04-16 21:13:04 +00:00
// env_inner.open_tables(&tx_ro).unwrap();
// TODO: waiting on PR 102
});
2024-04-16 20:53:24 +00:00
});
}
/// Open all database tables in read/write mode.
2024-04-16 21:39:53 +00:00
#[named]
2024-04-16 20:53:24 +00:00
fn open_tables_mut(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
2024-04-16 21:13:04 +00:00
let tx_rw = env_inner.tx_rw().unwrap();
2024-04-16 20:53:24 +00:00
2024-04-16 21:39:53 +00:00
c.bench_function(function_name!(), |b| {
2024-04-16 20:53:24 +00:00
b.iter(|| {
2024-04-16 21:39:53 +00:00
black_box(env_inner.open_db_rw::<Outputs>(&tx_rw)).unwrap();
2024-04-16 21:13:04 +00:00
// env_inner.open_tables_mut(&mut tx_rw).unwrap();
// TODO: waiting on PR 102
});
2024-04-16 20:53:24 +00:00
});
}
2024-04-20 00:30:43 +00:00
/// `Env` memory map resizes.
2024-04-16 21:39:53 +00:00
#[named]
2024-04-16 21:13:04 +00:00
fn resize(c: &mut Criterion) {
2024-04-16 20:53:24 +00:00
let (env, _tempdir) = tmp_concrete_env();
// Resize by the OS page size.
2024-04-16 21:13:04 +00:00
let page_size = page_size();
2024-04-16 20:53:24 +00:00
2024-04-16 21:39:53 +00:00
c.bench_function(function_name!(), |b| {
2024-04-16 21:13:04 +00:00
b.iter(|| {
// This test is only valid for `Env`'s that need to resize manually.
if ConcreteEnv::MANUAL_RESIZE {
2024-04-16 21:39:53 +00:00
env.resize_map(black_box(Some(ResizeAlgorithm::FixedBytes(page_size))));
2024-04-16 21:13:04 +00:00
}
});
});
2024-04-16 20:53:24 +00:00
}
2024-04-20 00:30:43 +00:00
/// Access current memory map size of the database.
#[named]
fn current_map_size(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
c.bench_function(function_name!(), |b| {
b.iter(|| {
// This test is only valid for `Env`'s that need to resize manually.
if ConcreteEnv::MANUAL_RESIZE {
black_box(env.current_map_size());
}
});
});
}
/// Access on-disk size of the database.
#[named]
fn disk_size_bytes(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
c.bench_function(function_name!(), |b| {
b.iter(|| {
black_box(env.disk_size_bytes()).unwrap();
});
});
}