//! Benchmark the generation of a large database with values of varying sizes for each (a)db variant //! that supports variable-size values. use crate::variable::{ gen_random_kv, get_any_ordered, get_any_unordered, get_current_ordered, get_current_unordered, Digest, Variant, VARIANTS, }; use commonware_runtime::{ benchmarks::{context, tokio}, tokio::{Config, Context}, }; use commonware_storage::qmdb::{any::traits::DbAny, store::LogStore, Error}; use criterion::{criterion_group, Criterion}; use std::time::{Duration, Instant}; const NUM_ELEMENTS: u64 = 1_000; const NUM_OPERATIONS: u64 = 10_000; const COMMITS_PER_ITERATION: u64 = 100; /// Benchmark the generation of a large randomly generated any db. fn bench_variable_generate(c: &mut Criterion) { let cfg = Config::default(); let runner = tokio::Runner::new(cfg); for elements in [NUM_ELEMENTS, NUM_ELEMENTS * 10] { for operations in [NUM_OPERATIONS, NUM_OPERATIONS * 10] { for variant in VARIANTS { c.bench_function( &format!( "{}/variant={} elements={} operations={}", module_path!(), variant.name(), elements, operations, ), |b| { b.to_async(&runner).iter_custom(|iters| async move { let ctx = context::get::(); let mut total_elapsed = Duration::ZERO; for _ in 0..iters { let commit_frequency = (operations / COMMITS_PER_ITERATION) as u32; let elapsed = match variant { Variant::AnyUnordered => { let db = get_any_unordered(ctx.clone()).await; test_db(db, elements, operations, commit_frequency) .await .unwrap() } Variant::AnyOrdered => { let db = get_any_ordered(ctx.clone()).await; test_db(db, elements, operations, commit_frequency) .await .unwrap() } Variant::CurrentUnordered => { let db = get_current_unordered(ctx.clone()).await; test_db(db, elements, operations, commit_frequency) .await .unwrap() } Variant::CurrentOrdered => { let db = get_current_ordered(ctx.clone()).await; test_db(db, elements, operations, commit_frequency) .await .unwrap() } }; total_elapsed += elapsed; } total_elapsed }); }, ); } } } } /// Test the database generation and cleanup. /// /// Generates data, prunes, and destroys. async fn test_db( mut db: C, elements: u64, operations: u64, commit_frequency: u32, ) -> Result where C: DbAny + LogStore>, { let start = Instant::now(); // Generate random operations gen_random_kv(&mut db, elements, operations, commit_frequency).await; db.prune(db.inactivity_floor_loc().await).await?; db.sync().await?; let elapsed = start.elapsed(); db.destroy().await?; // don't time destroy Ok(elapsed) } criterion_group! { name = benches; config = Criterion::default().sample_size(10); targets = bench_variable_generate }