forked from a16z/helios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_db.rs
51 lines (45 loc) · 1.49 KB
/
file_db.rs
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
use config::Config;
use consensus::database::{Database, FileDB};
use criterion::{criterion_group, criterion_main, Criterion};
use tempfile::tempdir;
mod harness;
criterion_group! {
name = file_db;
config = Criterion::default();
targets = save_checkpoint, load_checkpoint
}
criterion_main!(file_db);
/// Benchmark saving/writing a checkpoint to the file db.
pub fn save_checkpoint(c: &mut Criterion) {
c.bench_function("save_checkpoint", |b| {
let checkpoint: &[u8] = &[1, 2, 3];
b.iter(|| {
let data_dir = Some(tempdir().unwrap().into_path());
let config = Config {
data_dir,
..Default::default()
};
let db = FileDB::new(&config).unwrap();
db.save_checkpoint(checkpoint).unwrap();
})
});
}
/// Benchmark loading a checkpoint from the file db.
pub fn load_checkpoint(c: &mut Criterion) {
c.bench_function("load_checkpoint", |b| {
// First write to the db
let data_dir = Some(tempdir().unwrap().into_path());
let config = Config {
data_dir,
..Default::default()
};
let db = FileDB::new(&config).unwrap();
let written_checkpoint = vec![1; 32];
db.save_checkpoint(&written_checkpoint.clone()).unwrap();
// Then read from the db
b.iter(|| {
let checkpoint = db.load_checkpoint().unwrap();
assert_eq!(checkpoint, written_checkpoint.clone());
})
});
}