|
| 1 | +//! Random key-lookup benchmark for Archive. |
| 2 | +
|
| 3 | +use super::utils::{append_random, get_archive, ArchiveType, Key}; |
| 4 | +use commonware_runtime::{ |
| 5 | + benchmarks::{context, tokio}, |
| 6 | + tokio::Config, |
| 7 | + Runner, |
| 8 | +}; |
| 9 | +use commonware_storage::archive::Identifier; |
| 10 | +use criterion::{black_box, criterion_group, Criterion}; |
| 11 | +use futures::future::try_join_all; |
| 12 | +use rand::{rngs::StdRng, Rng, SeedableRng}; |
| 13 | +use std::time::Instant; |
| 14 | + |
| 15 | +/// Items pre-loaded into the archive. |
| 16 | +const ITEMS: u64 = 1_000_000; |
| 17 | + |
| 18 | +fn select_keys(keys: &[Key], reads: usize) -> Vec<Key> { |
| 19 | + let mut rng = StdRng::seed_from_u64(42); |
| 20 | + let mut selected_keys = Vec::with_capacity(reads); |
| 21 | + for _ in 0..reads { |
| 22 | + selected_keys.push(keys[rng.gen_range(0..ITEMS as usize)].clone()); |
| 23 | + } |
| 24 | + selected_keys |
| 25 | +} |
| 26 | + |
| 27 | +fn select_indices(reads: usize) -> Vec<u64> { |
| 28 | + let mut rng = StdRng::seed_from_u64(42); |
| 29 | + let mut selected_indices = Vec::with_capacity(reads); |
| 30 | + for _ in 0..reads { |
| 31 | + selected_indices.push(rng.gen_range(0..ITEMS)); |
| 32 | + } |
| 33 | + selected_indices |
| 34 | +} |
| 35 | + |
| 36 | +async fn read_serial_keys(a: &ArchiveType, reads: &[Key]) { |
| 37 | + for k in reads { |
| 38 | + black_box(a.get(Identifier::Key(k)).await.unwrap().unwrap()); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async fn read_serial_indices(a: &ArchiveType, indices: &[u64]) { |
| 43 | + for idx in indices { |
| 44 | + black_box(a.get(Identifier::Index(*idx)).await.unwrap().unwrap()); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async fn read_concurrent_keys(a: &ArchiveType, reads: Vec<Key>) { |
| 49 | + let futures = reads.iter().map(|k| a.get(Identifier::Key(k))); |
| 50 | + black_box(try_join_all(futures).await.unwrap()); |
| 51 | +} |
| 52 | + |
| 53 | +async fn read_concurrent_indices(a: &ArchiveType, indices: &[u64]) { |
| 54 | + let mut futs = Vec::with_capacity(indices.len()); |
| 55 | + for idx in indices { |
| 56 | + futs.push(a.get(Identifier::Index(*idx))); |
| 57 | + } |
| 58 | + black_box(try_join_all(futs).await.unwrap()); |
| 59 | +} |
| 60 | + |
| 61 | +fn bench_get(c: &mut Criterion) { |
| 62 | + // Create a config we can use across all benchmarks (with a fixed `storage_directory`). |
| 63 | + let cfg = Config::default(); |
| 64 | + for compression in [None, Some(3)] { |
| 65 | + // Create a shared on-disk archive once so later setup is fast. |
| 66 | + let builder = commonware_runtime::tokio::Runner::new(cfg.clone()); |
| 67 | + let keys = builder.start(|ctx| async move { |
| 68 | + let mut a = get_archive(ctx, compression).await; |
| 69 | + let keys = append_random(&mut a, ITEMS).await; |
| 70 | + a.close().await.unwrap(); |
| 71 | + keys |
| 72 | + }); |
| 73 | + |
| 74 | + // Run the benchmarks. |
| 75 | + let runner = tokio::Runner::new(cfg.clone()); |
| 76 | + for mode in ["serial", "concurrent"] { |
| 77 | + for pattern in ["key", "index"] { |
| 78 | + for reads in [1_000, 10_000, 100_000] { |
| 79 | + let label = format!( |
| 80 | + "{}/mode={} pattern={} comp={} reads={}", |
| 81 | + module_path!(), |
| 82 | + mode, |
| 83 | + pattern, |
| 84 | + compression |
| 85 | + .map(|l| l.to_string()) |
| 86 | + .unwrap_or_else(|| "off".into()), |
| 87 | + reads |
| 88 | + ); |
| 89 | + c.bench_function(&label, |b| { |
| 90 | + let keys = keys.clone(); |
| 91 | + b.to_async(&runner).iter_custom(move |iters| { |
| 92 | + let keys = keys.clone(); |
| 93 | + async move { |
| 94 | + let ctx = context::get::<commonware_runtime::tokio::Context>(); |
| 95 | + let archive = get_archive(ctx, compression).await; |
| 96 | + if pattern == "key" { |
| 97 | + let selected_keys = select_keys(&keys, reads); |
| 98 | + let start = Instant::now(); |
| 99 | + for _ in 0..iters { |
| 100 | + match mode { |
| 101 | + "serial" => { |
| 102 | + read_serial_keys(&archive, &selected_keys).await |
| 103 | + } |
| 104 | + "concurrent" => { |
| 105 | + read_concurrent_keys( |
| 106 | + &archive, |
| 107 | + selected_keys.clone(), |
| 108 | + ) |
| 109 | + .await |
| 110 | + } |
| 111 | + _ => unreachable!(), |
| 112 | + } |
| 113 | + } |
| 114 | + start.elapsed() |
| 115 | + } else { |
| 116 | + let selected_indices = select_indices(reads); |
| 117 | + let start = Instant::now(); |
| 118 | + for _ in 0..iters { |
| 119 | + match mode { |
| 120 | + "serial" => { |
| 121 | + read_serial_indices(&archive, &selected_indices) |
| 122 | + .await |
| 123 | + } |
| 124 | + "concurrent" => { |
| 125 | + read_concurrent_indices(&archive, &selected_indices) |
| 126 | + .await |
| 127 | + } |
| 128 | + _ => unreachable!(), |
| 129 | + } |
| 130 | + } |
| 131 | + start.elapsed() |
| 132 | + } |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Clean up shared artifacts. |
| 141 | + let cleaner = commonware_runtime::tokio::Runner::new(cfg.clone()); |
| 142 | + cleaner.start(|ctx| async move { |
| 143 | + let a = get_archive(ctx, compression).await; |
| 144 | + a.destroy().await.unwrap(); |
| 145 | + }); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +criterion_group! { |
| 150 | + name = benches; |
| 151 | + config = Criterion::default().sample_size(10); |
| 152 | + targets = bench_get |
| 153 | +} |
0 commit comments