1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Benchmarks for QMDB startup initialization performance.
//!
//! These benchmarks have expensive setup (generating large random databases) that runs lazily
//! inside `bench_function` so criterion's name filter can skip them entirely.
use crate::common::{
Digest, StoreDb, define_fixed_variants, define_vec_variants, gen_random_kv,
gen_store_random_kv, make_fixed_value, make_var_value, open_store_db, store_cfg,
};
use commonware_macros::boxed;
use commonware_runtime::{
Runner as _, Supervisor as _,
benchmarks::{context, tokio},
tokio::{Config, Context},
};
use commonware_storage::{merkle::Family, qmdb::any::traits::DbAny};
use commonware_utils::{NZUsize, TestRng};
use core::num::NonZeroUsize;
use criterion::{Criterion, criterion_group};
const NUM_ELEMENTS: u64 = 100_000;
const NUM_OPERATIONS: u64 = 1_000_000;
const COMMIT_FREQUENCY: u32 = 10_000;
/// Init-time `(location -> key)` cache sizes to compare: `None` disables the cache (the no-cache
/// baseline), and a reasonably sized cache that covers the bench's working set.
const CACHE_SIZES: [Option<NonZeroUsize>; 2] = [None, Some(NZUsize!(1 << 18))];
cfg_if::cfg_if! {
if #[cfg(not(full_bench))] {
const CASES: [(u64, u64); 1] = [(NUM_ELEMENTS, NUM_OPERATIONS)];
} else {
const CASES: [(u64, u64); 2] = [
(NUM_ELEMENTS, NUM_OPERATIONS),
(NUM_ELEMENTS * 3, NUM_OPERATIONS * 3),
];
}
}
/// Populate, prune, and sync a database (used in setup phase).
#[boxed]
async fn populate_and_sync<F: Family, C: DbAny<F, Key = Digest>>(
db: C,
elements: u64,
operations: u64,
make_value: impl Fn(&mut TestRng) -> C::Value,
) -> C {
let db = gen_random_kv::<F, _>(
db,
elements,
operations,
Some(COMMIT_FREQUENCY),
None, // seed_batch
None, // prune_frequency
None, // key_zipf_exponent (uniform churn)
None, // keyspace (all keys seeded)
make_value,
)
.await;
let boundary = db.sync_boundary();
let db = db.prune(boundary).await.unwrap();
db.sync().await.unwrap()
}
define_fixed_variants! {
enum FixedVariant;
const FIXED_VARIANTS;
dispatch dispatch_fixed;
timed_dispatch dispatch_fixed_timed_init;
}
fn bench_fixed_value_init(c: &mut Criterion) {
let cfg = Config::default();
for (elements, operations) in CASES {
for &variant in FIXED_VARIANTS {
// Populated lazily on the first sample of the first matched cache size, then reused by
// every cache size for this variant (all read the same on-disk database).
let mut initialized = false;
for &cache_size in &CACHE_SIZES {
let cache = cache_size.map_or(0, NonZeroUsize::get);
let runner = tokio::Runner::new(cfg.clone());
c.bench_function(
&format!(
"{}/variant={} cache={cache} elements={elements}",
module_path!(),
variant.name(),
),
|b| {
// Setup: populate database (once, on first matched sample).
if !initialized {
commonware_runtime::tokio::Runner::new(cfg.clone()).start(
|ctx| async move {
dispatch_fixed!(ctx, variant, |db| {
populate_and_sync(
db,
elements,
operations,
make_fixed_value,
)
.await;
});
},
);
initialized = true;
}
// Benchmark: measure init time at this cache size.
b.to_async(&runner).iter_custom(move |iters| async move {
let ctx = context::get::<Context>();
dispatch_fixed_timed_init!(ctx, variant, iters, cache_size, |db| {
assert_ne!(db.bounds().end, 0);
})
});
},
);
}
// Cleanup: destroy database.
if initialized {
commonware_runtime::tokio::Runner::new(cfg.clone()).start(|ctx| async move {
dispatch_fixed!(ctx, variant, |db| {
db.destroy().await.unwrap();
});
});
}
}
}
}
define_vec_variants! {
enum VarVariant;
const VEC_VARIANTS;
dispatch dispatch_var;
timed_dispatch dispatch_var_timed_init;
}
fn bench_var_value_init(c: &mut Criterion) {
let cfg = Config::default();
for (elements, operations) in CASES {
for &variant in VEC_VARIANTS {
// Populated lazily on the first sample of the first matched cache size, then reused by
// every cache size for this variant (all read the same on-disk database).
let mut initialized = false;
for &cache_size in &CACHE_SIZES {
let cache = cache_size.map_or(0, NonZeroUsize::get);
let runner = tokio::Runner::new(cfg.clone());
c.bench_function(
&format!(
"{}/variant={} cache={cache} elements={elements}",
module_path!(),
variant.name(),
),
|b| {
// Setup: populate database (once, on first matched sample).
if !initialized {
commonware_runtime::tokio::Runner::new(cfg.clone()).start(
|ctx| async move {
dispatch_var!(ctx, variant, |db| {
populate_and_sync(db, elements, operations, make_var_value)
.await;
});
},
);
initialized = true;
}
// Benchmark: measure init time at this cache size.
b.to_async(&runner).iter_custom(move |iters| async move {
let ctx = context::get::<Context>();
dispatch_var_timed_init!(ctx, variant, iters, cache_size, |db| {
assert_ne!(db.bounds().end, 0);
})
});
},
);
}
// Cleanup: destroy database.
if initialized {
commonware_runtime::tokio::Runner::new(cfg.clone()).start(|ctx| async move {
dispatch_var!(ctx, variant, |db| {
db.destroy().await.unwrap();
});
});
}
}
}
}
/// Benchmark reopening a populated unauthenticated store at each init cache size.
fn bench_store_init(c: &mut Criterion) {
let cfg = Config::default();
for (elements, operations) in CASES {
// Populated lazily on the first sample of the first matched cache size, then reused by
// every cache size (all read the same on-disk database).
let mut initialized = false;
for &cache_size in &CACHE_SIZES {
let cache = cache_size.map_or(0, NonZeroUsize::get);
let runner = tokio::Runner::new(cfg.clone());
c.bench_function(
&format!(
"{}/variant=store::variable cache={cache} elements={elements}",
module_path!(),
),
|b| {
// Populate the database once, on the first matched sample.
if !initialized {
commonware_runtime::tokio::Runner::new(cfg.clone()).start(
|ctx| async move {
let db = open_store_db(ctx.child("storage")).await;
let db = gen_store_random_kv(
db,
elements,
operations,
COMMIT_FREQUENCY,
make_var_value,
)
.await;
let floor = db.inactivity_floor_loc();
let db = db.prune(floor).await.unwrap();
db.sync().await.unwrap();
},
);
initialized = true;
}
// Measure init time at this cache size.
b.to_async(&runner).iter_custom(move |iters| async move {
let ctx = context::get::<Context>();
let mut cfg = store_cfg();
cfg.init_cache = cache_size;
let start = std::time::Instant::now();
for _ in 0..iters {
let db = StoreDb::init(ctx.child("storage"), cfg.clone(), None)
.await
.unwrap();
assert_ne!(db.bounds().end, 0);
}
start.elapsed()
});
},
);
}
// Destroy the populated database.
if initialized {
commonware_runtime::tokio::Runner::new(cfg.clone()).start(|ctx| async move {
open_store_db(ctx.child("storage"))
.await
.destroy()
.await
.unwrap();
});
}
}
}
criterion_group! {
name = benches;
config = Criterion::default().sample_size(10);
targets = bench_fixed_value_init, bench_var_value_init, bench_store_init
}