use crate::{append_fixed_random_data, get_fixed_journal};
use commonware_runtime::{
benchmarks::{context, tokio},
tokio::Context,
};
use commonware_storage::journal::contiguous::{fixed::Journal, Reader as _};
use commonware_utils::{sequence::FixedBytes, NZU64};
use criterion::{criterion_group, Criterion};
use std::{
hint::black_box,
num::NonZeroU64,
time::{Duration, Instant},
};
const PARTITION: &str = "test-partition";
const ITEMS_PER_BLOB: NonZeroU64 = NZU64!(100_000);
const ITEM_SIZE: usize = 32;
async fn bench_run(journal: &Journal<Context, FixedBytes<ITEM_SIZE>>, items_to_read: u64) {
let reader = journal.reader().await;
for pos in 0..items_to_read {
black_box(reader.read(pos).await.expect("failed to read data"));
}
}
fn bench_fixed_read_sequential(c: &mut Criterion) {
let runner = tokio::Runner::default();
for items in [1_000, 10_000, 100_000, 500_000] {
c.bench_function(
&format!("{}/items={} size={}", module_path!(), items, ITEM_SIZE),
|b| {
b.to_async(&runner).iter_custom(|iters| async move {
let ctx = context::get::<commonware_runtime::tokio::Context>();
let mut j =
get_fixed_journal::<ITEM_SIZE>(ctx, PARTITION, ITEMS_PER_BLOB).await;
append_fixed_random_data::<_, ITEM_SIZE>(&mut j, items).await;
let sz = j.size().await;
assert_eq!(sz, items);
let mut duration = Duration::ZERO;
for _ in 0..iters {
let start = Instant::now();
bench_run(&j, items).await;
duration += start.elapsed();
}
j.destroy().await.unwrap();
duration
});
},
);
}
}
criterion_group! {
name = benches;
config = Criterion::default().sample_size(10);
targets = bench_fixed_read_sequential
}