Skip to content

Commit 558e548

Browse files
committed
chore: add deferred persistence benchmark
1 parent f044ec9 commit 558e548

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

.github/workflows/benchmarks.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ jobs:
2424
run: cargo fetch --locked --verbose
2525
- name: Build
2626
run: cargo build --frozen --profile maxperf --features ethhash,logger --workspace --benches
27-
- name: Run firewood crate benchmarks
27+
- name: Run firewood crate hashop benchmark
2828
run: cargo bench --frozen --profile maxperf --features ethhash,logger -p firewood --bench hashops -- --noplot
29+
- name: Run firewood crate defer_persist benchmark
30+
run: cargo bench --frozen --profile maxperf --features ethhash,logger -p firewood --bench defer_persist -- --noplot
2931
- name: Run storage crate benchmarks
3032
run: cargo bench --frozen --profile maxperf --features ethhash,logger -p firewood-storage --bench serializer -- --noplot
3133
- name: Upload benchmark results

firewood/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ sha3 = "0.10.8"
7777
name = "hashops"
7878
harness = false
7979

80+
[[bench]]
81+
name = "defer_persist"
82+
harness = false
83+
8084
[lints]
8185
workspace = true
8286

firewood/benches/defer_persist.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)