add db benchmarks

This commit is contained in:
hinto.janai 2024-04-16 17:39:53 -04:00
parent 2fa0c1a519
commit 528a1e18da
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 260 additions and 88 deletions

16
Cargo.lock generated
View file

@ -699,6 +699,7 @@ dependencies = [
"criterion",
"cuprate-database",
"cuprate-helper",
"function_name",
"tempfile",
]
@ -1033,6 +1034,21 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "function_name"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1ab577a896d09940b5fe12ec5ae71f9d8211fff62c919c03a3750a9901e98a7"
dependencies = [
"function_name-proc-macro",
]
[[package]]
name = "function_name-proc-macro"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673464e1e314dd67a0fd9544abc99e8eb28d0c7e3b69b033bcff9b2d00b87333"
[[package]]
name = "funty"
version = "2.0.0"

View file

@ -16,6 +16,7 @@ criterion = { workspace = true }
cuprate-database = { path = "../", features = ["default"] }
cuprate-helper = { path = "../../helper", features = ["fs", "thread"] }
# cuprate-types = { path = "../../types", features = ["service"] }
function_name = { version = "0.3.0" }
tempfile = { version = "3.10.0" }
[[bench]]

View file

@ -0,0 +1,145 @@
//! TODO
//---------------------------------------------------------------------------------------------------- Import
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use function_name::named;
use cuprate_database::{
resize::{page_size, ResizeAlgorithm},
tables::Outputs,
types::{Output, PreRctOutputId},
ConcreteEnv, DatabaseIter, DatabaseRo, DatabaseRw, Env, EnvInner, TxRo, TxRw,
};
use cuprate_database_benchmark::tmp_concrete_env;
//---------------------------------------------------------------------------------------------------- Criterion
criterion_group!(benches, put, get, get_range, delete);
criterion_main!(benches);
//---------------------------------------------------------------------------------------------------- Constants
/// The (1st) key.
const KEY: PreRctOutputId = PreRctOutputId {
amount: 1,
amount_index: 123,
};
/// The expected value.
const VALUE: Output = Output {
key: [35; 32],
height: 45_761_798,
output_flags: 0,
tx_idx: 2_353_487,
};
//---------------------------------------------------------------------------------------------------- Env benchmarks
/// [`DatabaseRw::put`]
#[named]
fn put(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw().unwrap();
let mut table = env_inner.open_db_rw::<Outputs>(&tx_rw).unwrap();
let mut key = KEY;
c.bench_function(function_name!(), |b| {
b.iter(|| {
table.put(black_box(&key), black_box(&VALUE)).unwrap();
key.amount += 1;
});
});
}
/// [`DatabaseRo::get`]
#[named]
fn get(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw().unwrap();
let mut table = env_inner.open_db_rw::<Outputs>(&tx_rw).unwrap();
table.put(&KEY, &VALUE).unwrap();
c.bench_function(function_name!(), |b| {
b.iter(|| {
let _value: Output = table.get(black_box(&KEY)).unwrap();
});
});
}
/// [`DatabaseRo::get_range`]
#[named]
fn get_range(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw().unwrap();
let mut table = env_inner.open_db_rw::<Outputs>(&tx_rw).unwrap();
let mut key = KEY;
for _ in 0..100 {
table.put(&key, &VALUE).unwrap();
key.amount += 1;
}
drop(table);
TxRw::commit(tx_rw).unwrap();
let tx_ro = env_inner.tx_ro().unwrap();
let table = env_inner.open_db_ro::<Outputs>(&tx_ro).unwrap();
c.bench_function(function_name!(), |b| {
b.iter(|| {
let range = table.get_range(black_box(..)).unwrap();
for result in range {
let _value: Output = black_box(result.unwrap());
}
});
});
}
/// [`DatabaseRw::delete`]
#[named]
fn delete(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw().unwrap();
let mut table = env_inner.open_db_rw::<Outputs>(&tx_rw).unwrap();
let mut key = KEY;
for _ in 0..100 {
table.put(&key, &VALUE).unwrap();
key.amount += 1;
}
c.bench_function(function_name!(), |b| {
b.iter(|| {
table.put(&KEY, &VALUE).unwrap();
table.delete(&black_box(KEY)).unwrap();
});
});
}
// TODO: waiting on PR 102
// /// [`DatabaseRw::take`]
// #[named]
// fn take(c: &mut Criterion) {
// let (env, _tempdir) = tmp_concrete_env();
// let env_inner = env.env_inner();
// let tx_rw = env_inner.tx_rw().unwrap();
// let mut table = env_inner.open_db_rw::<Outputs>(&tx_rw).unwrap();
// let mut key = KEY;
// for _ in 0..100 {
// table.put(&key, &VALUE).unwrap();
// key.amount += 1;
// }
// c.bench_function(function_name!(), |b| {
// b.iter(|| {
// table.put(&KEY, &VALUE).unwrap();
// let value: Output = black_box(table.take(&black_box(KEY)).unwrap());
// });
// });
// }

View file

@ -3,6 +3,8 @@
//---------------------------------------------------------------------------------------------------- Import
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use function_name::named;
use cuprate_database::{
resize::{page_size, ResizeAlgorithm},
tables::Outputs,
@ -11,94 +13,7 @@ use cuprate_database::{
use cuprate_database_benchmark::tmp_concrete_env;
//---------------------------------------------------------------------------------------------------- Env benchmarks
/// [`Env::open`].
fn open(c: &mut Criterion) {
c.bench_function("tmp_concrete_env", |b| {
b.iter(|| {
// We don't want `tempfile`'s file destruction code
// to overtake the benchmark, so forget the object
// so it doesn't get deconstructed.
//
// FIXME: this might hit file descriptor limits.
std::mem::forget(tmp_concrete_env());
});
});
}
/// Create and commit read-only transactions.
fn tx_ro(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
c.bench_function("tx_ro", |b| {
b.iter(|| {
let tx_ro = env_inner.tx_ro().unwrap();
TxRo::commit(tx_ro).unwrap();
});
});
}
/// Create and commit read/write transactions.
fn tx_rw(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
c.bench_function("tx_rw", |b| {
b.iter(|| {
let tx_rw = env_inner.tx_rw().unwrap();
TxRw::commit(tx_rw).unwrap();
});
});
}
/// Open all database tables in read-only mode.
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();
c.bench_function("open_tables", |b| {
b.iter(|| {
env_inner.open_db_ro::<Outputs>(&tx_ro).unwrap();
// env_inner.open_tables(&tx_ro).unwrap();
// TODO: waiting on PR 102
});
});
}
/// Open all database tables in read/write mode.
fn open_tables_mut(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw().unwrap();
c.bench_function("open_tables_mut", |b| {
b.iter(|| {
env_inner.open_db_rw::<Outputs>(&tx_rw).unwrap();
// env_inner.open_tables_mut(&mut tx_rw).unwrap();
// TODO: waiting on PR 102
});
});
}
/// Test `Env` resizes.
fn resize(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
// Resize by the OS page size.
let page_size = page_size();
c.bench_function("resize", |b| {
b.iter(|| {
// This test is only valid for `Env`'s that need to resize manually.
if ConcreteEnv::MANUAL_RESIZE {
env.resize_map(Some(ResizeAlgorithm::FixedBytes(page_size)));
}
});
});
}
//---------------------------------------------------------------------------------------------------- Criterion
criterion_group!(
benches,
open,
@ -109,3 +24,97 @@ criterion_group!(
resize
);
criterion_main!(benches);
//---------------------------------------------------------------------------------------------------- Env benchmarks
/// [`Env::open`].
#[named]
fn open(c: &mut Criterion) {
c.bench_function(function_name!(), |b| {
b.iter(|| {
// We don't want `tempfile`'s file destruction code
// to overtake the benchmark, so forget the object
// so it doesn't get deconstructed.
//
// FIXME: this might hit file descriptor limits.
std::mem::forget(black_box(tmp_concrete_env()));
});
});
}
/// Create and commit read-only transactions.
#[named]
fn tx_ro(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
c.bench_function(function_name!(), |b| {
b.iter(|| {
let tx_ro = black_box(env_inner.tx_ro()).unwrap();
TxRo::commit(black_box(tx_ro)).unwrap();
});
});
}
/// Create and commit read/write transactions.
#[named]
fn tx_rw(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
c.bench_function(function_name!(), |b| {
b.iter(|| {
let tx_rw = black_box(env_inner.tx_rw()).unwrap();
TxRw::commit(black_box(tx_rw)).unwrap();
});
});
}
/// Open all database tables in read-only mode.
#[named]
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();
c.bench_function(function_name!(), |b| {
b.iter(|| {
black_box(env_inner.open_db_ro::<Outputs>(&tx_ro)).unwrap();
// env_inner.open_tables(&tx_ro).unwrap();
// TODO: waiting on PR 102
});
});
}
/// Open all database tables in read/write mode.
#[named]
fn open_tables_mut(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw().unwrap();
c.bench_function(function_name!(), |b| {
b.iter(|| {
black_box(env_inner.open_db_rw::<Outputs>(&tx_rw)).unwrap();
// env_inner.open_tables_mut(&mut tx_rw).unwrap();
// TODO: waiting on PR 102
});
});
}
/// Test `Env` resizes.
#[named]
fn resize(c: &mut Criterion) {
let (env, _tempdir) = tmp_concrete_env();
// Resize by the OS page size.
let page_size = page_size();
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 {
env.resize_map(black_box(Some(ResizeAlgorithm::FixedBytes(page_size))));
}
});
});
}

View file

@ -86,4 +86,5 @@ mod env;
criterion_main! {
env::benches,
db::benches,
}