|
| 1 | +use crate::{append_fixed_random_data, get_variable_journal}; |
| 2 | +use commonware_runtime::{ |
| 3 | + benchmarks::{context, tokio}, |
| 4 | + tokio::{Config, Context, Runner}, |
| 5 | + Runner as _, Supervisor as _, |
| 6 | +}; |
| 7 | +use commonware_storage::journal::contiguous::{variable::Journal, Reader as _}; |
| 8 | +use commonware_utils::{sequence::FixedBytes, NZU64}; |
| 9 | +use criterion::{criterion_group, Criterion}; |
| 10 | +use futures::future::try_join_all; |
| 11 | +use rand::{rngs::StdRng, Rng, SeedableRng}; |
| 12 | +use std::{ |
| 13 | + hint::black_box, |
| 14 | + num::NonZeroU64, |
| 15 | + time::{Duration, Instant}, |
| 16 | +}; |
| 17 | + |
| 18 | +/// Partition name to use in the journal config. |
| 19 | +const PARTITION: &str = "variable-random-test-partition"; |
| 20 | + |
| 21 | +/// Value of items_per_section to use in the journal config. |
| 22 | +const ITEMS_PER_SECTION: NonZeroU64 = NZU64!(10_000); |
| 23 | + |
| 24 | +/// Number of items to write to the journal we will be reading from. |
| 25 | +const ITEMS_TO_WRITE: u64 = 5_000_000; |
| 26 | + |
| 27 | +/// Size of each journal item in bytes. |
| 28 | +const ITEM_SIZE: usize = 32; |
| 29 | + |
| 30 | +/// Read `items_to_read` random items from the given `journal`, awaiting each |
| 31 | +/// result before continuing. |
| 32 | +async fn bench_run_serial(journal: &Journal<Context, FixedBytes<ITEM_SIZE>>, items_to_read: usize) { |
| 33 | + let reader = journal.reader().await; |
| 34 | + let mut rng = StdRng::seed_from_u64(0); |
| 35 | + for _ in 0..items_to_read { |
| 36 | + let pos = rng.gen_range(0..ITEMS_TO_WRITE); |
| 37 | + black_box(reader.read(pos).await.expect("failed to read data")); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Concurrently read (via try_join_all) `items_to_read` random items from the given `journal`. |
| 42 | +async fn bench_run_concurrent( |
| 43 | + journal: &Journal<Context, FixedBytes<ITEM_SIZE>>, |
| 44 | + items_to_read: usize, |
| 45 | +) { |
| 46 | + let reader = journal.reader().await; |
| 47 | + let mut rng = StdRng::seed_from_u64(0); |
| 48 | + let mut futures = Vec::with_capacity(items_to_read); |
| 49 | + for _ in 0..items_to_read { |
| 50 | + let pos = rng.gen_range(0..ITEMS_TO_WRITE); |
| 51 | + futures.push(reader.read(pos)); |
| 52 | + } |
| 53 | + try_join_all(futures).await.expect("failed to read data"); |
| 54 | +} |
| 55 | + |
| 56 | +/// Batch-read `items_to_read` random items via `read_many`. |
| 57 | +async fn bench_run_read_many( |
| 58 | + journal: &Journal<Context, FixedBytes<ITEM_SIZE>>, |
| 59 | + items_to_read: usize, |
| 60 | +) { |
| 61 | + let reader = journal.reader().await; |
| 62 | + let mut rng = StdRng::seed_from_u64(0); |
| 63 | + let mut positions: Vec<u64> = (0..items_to_read) |
| 64 | + .map(|_| rng.gen_range(0..ITEMS_TO_WRITE)) |
| 65 | + .collect(); |
| 66 | + positions.sort_unstable(); |
| 67 | + positions.dedup(); |
| 68 | + black_box( |
| 69 | + reader |
| 70 | + .read_many(&positions) |
| 71 | + .await |
| 72 | + .expect("failed to read data"), |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +fn bench_variable_read_random(c: &mut Criterion) { |
| 77 | + let cfg = Config::default(); |
| 78 | + let mut initialized = false; |
| 79 | + let runner = tokio::Runner::new(cfg.clone()); |
| 80 | + for mode in ["serial", "concurrent", "read_many"] { |
| 81 | + for items_to_read in [100, 1_000, 10_000, 100_000] { |
| 82 | + c.bench_function( |
| 83 | + &format!( |
| 84 | + "{}/mode={} items={} size={}", |
| 85 | + module_path!(), |
| 86 | + mode, |
| 87 | + items_to_read, |
| 88 | + ITEM_SIZE |
| 89 | + ), |
| 90 | + |b| { |
| 91 | + // Setup: populate journal (once, on first sample). |
| 92 | + if !initialized { |
| 93 | + Runner::new(cfg.clone()).start(|ctx| async move { |
| 94 | + let mut j = |
| 95 | + get_variable_journal(ctx, PARTITION, ITEMS_PER_SECTION).await; |
| 96 | + append_fixed_random_data::<_, ITEM_SIZE>(&mut j, ITEMS_TO_WRITE).await; |
| 97 | + j.sync().await.unwrap(); |
| 98 | + }); |
| 99 | + initialized = true; |
| 100 | + } |
| 101 | + |
| 102 | + // Benchmark: measure read time. |
| 103 | + b.to_async(&runner).iter_custom(|iters| async move { |
| 104 | + let ctx = context::get::<commonware_runtime::tokio::Context>(); |
| 105 | + let j = get_variable_journal( |
| 106 | + ctx.child("storage"), |
| 107 | + PARTITION, |
| 108 | + ITEMS_PER_SECTION, |
| 109 | + ) |
| 110 | + .await; |
| 111 | + let mut duration = Duration::ZERO; |
| 112 | + for _ in 0..iters { |
| 113 | + let start = Instant::now(); |
| 114 | + match mode { |
| 115 | + "serial" => bench_run_serial(&j, items_to_read).await, |
| 116 | + "concurrent" => bench_run_concurrent(&j, items_to_read).await, |
| 117 | + "read_many" => bench_run_read_many(&j, items_to_read).await, |
| 118 | + _ => unreachable!(), |
| 119 | + } |
| 120 | + duration += start.elapsed(); |
| 121 | + } |
| 122 | + duration |
| 123 | + }); |
| 124 | + }, |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Cleanup: destroy journal. |
| 130 | + if initialized { |
| 131 | + Runner::new(cfg).start(|context| async move { |
| 132 | + let j = get_variable_journal::<ITEM_SIZE>(context, PARTITION, ITEMS_PER_SECTION).await; |
| 133 | + j.destroy().await.unwrap(); |
| 134 | + }); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +criterion_group! { |
| 139 | + name = benches; |
| 140 | + config = Criterion::default().sample_size(10); |
| 141 | + targets = bench_variable_read_random |
| 142 | +} |
0 commit comments