Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/src/snapshot/benches/version_map.rs
2529 views
1
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: Apache-2.0
3
use criterion::{black_box, criterion_group, criterion_main, Criterion};
4
use snapshot::Snapshot;
5
use versionize::{VersionMap, Versionize, VersionizeResult};
6
use versionize_derive::Versionize;
7
8
#[derive(Clone, Debug, Default, Versionize)]
9
struct Test {
10
a: Vec<Dummy>,
11
#[version(start = 1)]
12
b: u64,
13
#[version(start = 2)]
14
c: u64,
15
#[version(start = 3)]
16
d: u32,
17
#[version(start = 4)]
18
e: Vec<u64>,
19
}
20
21
#[derive(Clone, Debug, Default, Versionize)]
22
struct Dummy {
23
a: String,
24
#[version(start = 2)]
25
b: [u64; 32],
26
}
27
28
#[inline]
29
fn restore(mut snapshot_mem: &[u8], vm: VersionMap) {
30
Snapshot::unchecked_load::<&[u8], Test>(&mut snapshot_mem, vm).unwrap();
31
}
32
33
#[inline]
34
fn save<W: std::io::Write>(mut snapshot_mem: &mut W, vm: VersionMap) {
35
let state = Test {
36
a: vec![
37
Dummy {
38
a: "a string".to_owned(),
39
b: [0x1234u64; 32]
40
};
41
200
42
],
43
b: 0,
44
c: 1,
45
d: 2,
46
e: vec![0x4321; 100],
47
};
48
49
let mut snapshot = Snapshot::new(vm.clone(), vm.latest_version());
50
snapshot
51
.save_without_crc(&mut snapshot_mem, &state)
52
.unwrap();
53
}
54
55
pub fn criterion_benchmark(c: &mut Criterion) {
56
let mut snapshot_mem = vec![0u8; 1024 * 1024 * 128];
57
let mut vm = VersionMap::new();
58
59
vm.new_version()
60
.set_type_version(Test::type_id(), 2)
61
.new_version()
62
.set_type_version(Test::type_id(), 3)
63
.new_version()
64
.set_type_version(Test::type_id(), 4)
65
.set_type_version(Dummy::type_id(), 2);
66
67
let mut slice = &mut snapshot_mem.as_mut_slice();
68
save(&mut slice, vm.clone());
69
let snapshot_len = slice.as_ptr() as usize - snapshot_mem.as_slice().as_ptr() as usize;
70
println!("Snapshot length: {} bytes", snapshot_len);
71
72
c.bench_function("Serialize in vspace=4", |b| {
73
b.iter(|| {
74
save(
75
black_box(&mut snapshot_mem.as_mut_slice()),
76
black_box(vm.clone()),
77
)
78
})
79
});
80
81
c.bench_function("Deserialize in vspace=4", |b| {
82
b.iter(|| {
83
restore(
84
black_box(&mut snapshot_mem.as_slice()),
85
black_box(vm.clone()),
86
)
87
})
88
});
89
90
// Extend vspace to 100.
91
for _ in 0..96 {
92
vm.new_version();
93
}
94
95
save(&mut snapshot_mem.as_mut_slice(), vm.clone());
96
97
c.bench_function("Serialize in vspace=100", |b| {
98
b.iter(|| {
99
save(
100
black_box(&mut snapshot_mem.as_mut_slice()),
101
black_box(vm.clone()),
102
)
103
})
104
});
105
c.bench_function("Deserialize in vspace=100", |b| {
106
b.iter(|| {
107
restore(
108
black_box(&mut snapshot_mem.as_slice()),
109
black_box(vm.clone()),
110
)
111
})
112
});
113
114
// Extend vspace to 1001.
115
for _ in 0..900 {
116
vm.new_version();
117
}
118
119
// Save the snapshot at version 1001.
120
save(&mut snapshot_mem.as_mut_slice(), vm.clone());
121
122
c.bench_function("Serialize in vspace=1000", |b| {
123
b.iter(|| {
124
save(
125
black_box(&mut snapshot_mem.as_mut_slice()),
126
black_box(vm.clone()),
127
)
128
})
129
});
130
c.bench_function("Deserialize in vspace=1000", |b| {
131
b.iter(|| {
132
restore(
133
black_box(&mut snapshot_mem.as_slice()),
134
black_box(vm.clone()),
135
)
136
})
137
});
138
}
139
140
criterion_group! {
141
name = benches;
142
// TODO: Fix the duration below to match the required time to collect enough samples.
143
// A warning message is generated by criterion while the accuracy of the test will be lower.
144
config = Criterion::default().measurement_time(std::time::Duration::from_secs(10));
145
targets = criterion_benchmark
146
}
147
148
criterion_main! {
149
benches
150
}
151
152