|
| 1 | +// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE.md for licensing terms. |
| 3 | + |
| 4 | +use criterion::{Criterion, criterion_group, criterion_main}; |
| 5 | +use firewood::db::{BatchOp, Db, DbConfig}; |
| 6 | +use firewood::v2::api::{Db as _, Proposal as _}; |
| 7 | +use firewood_storage::NodeHashAlgorithm; |
| 8 | +use rand::{Rng, distr::Alphanumeric}; |
| 9 | +use std::iter::repeat_with; |
| 10 | +use std::num::NonZeroU64; |
| 11 | + |
| 12 | +#[expect(clippy::unwrap_used)] |
| 13 | +fn bench_deferred_persistence<const N: usize, const COMMIT_COUNT: u64>(criterion: &mut Criterion) { |
| 14 | + const KEY_LEN: usize = 4; |
| 15 | + let rng = &firewood_storage::SeededRng::from_option(Some(1234)); |
| 16 | + let commit_count = NonZeroU64::new(COMMIT_COUNT).unwrap(); |
| 17 | + |
| 18 | + criterion |
| 19 | + .benchmark_group("deferred_persistence") |
| 20 | + .sample_size(20) |
| 21 | + .bench_function(format!("commit_count_{COMMIT_COUNT}"), |b| { |
| 22 | + b.iter_batched( |
| 23 | + || { |
| 24 | + let batch_ops: Vec<_> = |
| 25 | + repeat_with(|| rng.sample_iter(&Alphanumeric).take(KEY_LEN).collect()) |
| 26 | + .map(|key: Vec<_>| BatchOp::Put { |
| 27 | + key, |
| 28 | + value: vec![b'v'], |
| 29 | + }) |
| 30 | + .take(N) |
| 31 | + .collect(); |
| 32 | + batch_ops |
| 33 | + }, |
| 34 | + |batch_ops| { |
| 35 | + let tmpdir = tempfile::tempdir().unwrap(); |
| 36 | + let dbcfg = DbConfig::builder() |
| 37 | + .node_hash_algorithm(NodeHashAlgorithm::compile_option()) |
| 38 | + .deferred_persistence_commit_count(commit_count) |
| 39 | + .build(); |
| 40 | + let db = Db::new(tmpdir, dbcfg).unwrap(); |
| 41 | + |
| 42 | + for op in batch_ops { |
| 43 | + let proposal = db.propose(vec![op]).unwrap(); |
| 44 | + proposal.commit().unwrap(); |
| 45 | + } |
| 46 | + |
| 47 | + db.close().unwrap(); |
| 48 | + }, |
| 49 | + criterion::BatchSize::SmallInput, |
| 50 | + ); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +criterion_group! { |
| 55 | + name = benches; |
| 56 | + config = Criterion::default(); |
| 57 | + targets = bench_deferred_persistence::<1_000, 1>, bench_deferred_persistence::<1_000, 100> |
| 58 | +} |
| 59 | + |
| 60 | +criterion_main!(benches); |
0 commit comments