add storable benchmarks

This commit is contained in:
hinto.janai 2024-04-16 19:53:59 -04:00
parent 4b4f25c8a4
commit 92ae8d7b65
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
3 changed files with 87 additions and 5 deletions

View file

@ -7,7 +7,8 @@ use function_name::named;
use cuprate_database::{
tables::Outputs,
types::{Output, PreRctOutputId}, DatabaseIter, DatabaseRo, DatabaseRw, Env, EnvInner, TxRw,
types::{Output, PreRctOutputId},
DatabaseIter, DatabaseRo, DatabaseRw, Env, EnvInner, TxRw,
};
use cuprate_database_benchmark::tmp_concrete_env;

View file

@ -79,12 +79,12 @@
// Allow some lints when running in debug mode.
#![cfg_attr(debug_assertions, allow(clippy::todo, clippy::multiple_crate_versions))]
use criterion::criterion_main;
mod db;
mod env;
mod storable;
criterion_main! {
env::benches,
criterion::criterion_main! {
db::benches,
env::benches,
storable::benches,
}

View file

@ -0,0 +1,81 @@
//! TODO
//---------------------------------------------------------------------------------------------------- Import
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use function_name::named;
use cuprate_database::{
types::{Output, PreRctOutputId},
Storable,
};
//---------------------------------------------------------------------------------------------------- Criterion
criterion_group!(
benches,
pre_rct_output_id_as_bytes,
pre_rct_output_id_from_bytes,
output_as_bytes,
output_from_bytes
);
criterion_main!(benches);
//---------------------------------------------------------------------------------------------------- Constants
/// 16 bytes.
const PRE_RCT_OUTPUT_ID: PreRctOutputId = PreRctOutputId {
amount: 1,
amount_index: 123,
};
/// 48 bytes.
const OUTPUT: Output = Output {
key: [35; 32],
height: 45_761_798,
output_flags: 0,
tx_idx: 2_353_487,
};
//---------------------------------------------------------------------------------------------------- Storable benchmarks
/// [`PreRctOutputId`] cast as bytes.
#[named]
fn pre_rct_output_id_as_bytes(c: &mut Criterion) {
c.bench_function(function_name!(), |b| {
b.iter(|| {
black_box(Storable::as_bytes(black_box(&PRE_RCT_OUTPUT_ID)));
});
});
}
/// [`PreRctOutputId`] cast from bytes.
#[named]
fn pre_rct_output_id_from_bytes(c: &mut Criterion) {
let bytes = Storable::as_bytes(&PRE_RCT_OUTPUT_ID);
c.bench_function(function_name!(), |b| {
b.iter(|| {
let _: PreRctOutputId = black_box(Storable::from_bytes(black_box(bytes)));
});
});
}
/// [`Output`] cast as bytes.
#[named]
fn output_as_bytes(c: &mut Criterion) {
c.bench_function(function_name!(), |b| {
b.iter(|| {
black_box(Storable::as_bytes(black_box(&OUTPUT)));
});
});
}
/// [`Output`] cast from bytes.
#[named]
fn output_from_bytes(c: &mut Criterion) {
let bytes = Storable::as_bytes(&OUTPUT);
c.bench_function(function_name!(), |b| {
b.iter(|| {
let _: Output = black_box(Storable::from_bytes(black_box(bytes)));
});
});
}