/
bench_archive.rs
360 lines (323 loc) · 12.6 KB
/
bench_archive.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::BenchmarkId;
use criterion::Throughput;
use mla::config::{ArchiveReaderConfig, ArchiveWriterConfig};
use mla::helpers::linear_extract;
use mla::Layers;
use mla::{ArchiveReader, ArchiveWriter};
use rand::distributions::{Alphanumeric, Distribution};
use rand::seq::index::sample;
use rand::RngCore;
use rand::SeedableRng;
use rand_chacha::ChaChaRng;
use std::collections::HashMap;
use std::io::{self, Cursor, Read};
use std::time::{Duration, Instant};
use x25519_dalek::{PublicKey, StaticSecret};
const KB: usize = 1024;
const MB: usize = 1024 * KB;
const SIZE_LIST: [usize; 5] = [KB, 16 * KB, 128 * KB, MB, 4 * MB];
const SAMPLE_SIZE_SMALL: usize = 10;
/// Benchmark with all layers' permutations different block size
///
/// The archive is not reset between iterations, only between benchs.
/// Data is only appended as block for the same file.
/// As a result, some addition might take longer (on layer boundaries), but with
/// enough samples it ends as outliers
///
/// Big blocks (> 4MB) are also use to force the use of several blocks inside boundaries
pub fn multiple_layers_multiple_block_size(c: &mut Criterion) {
// Setup
// Use a deterministic RNG in tests, for reproductability. DO NOT DO THIS IS IN ANY RELEASED BINARY!
let mut rng = ChaChaRng::seed_from_u64(0);
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);
let key = StaticSecret::from(bytes);
let mut group = c.benchmark_group("multiple_layers_multiple_block_size");
group.measurement_time(Duration::from_secs(10));
group.sample_size(SAMPLE_SIZE_SMALL);
for size in SIZE_LIST.iter() {
group.throughput(Throughput::Bytes(*size as u64));
let data: Vec<u8> = Alphanumeric
.sample_iter(&mut rng)
.take(*size)
.map(|c| c as u8)
.collect();
for layers in &[
Layers::EMPTY,
Layers::COMPRESS,
Layers::ENCRYPT,
Layers::COMPRESS | Layers::ENCRYPT,
] {
// Create an archive
let file = Vec::new();
let mut config = ArchiveWriterConfig::new();
config
.enable_layer(*layers)
.add_public_keys(&[PublicKey::from(&key)]);
let mut mla = ArchiveWriter::from_config(file, config).expect("Writer init failed");
let id = mla.start_file("file").unwrap();
group.bench_with_input(
BenchmarkId::new(format!("Layers {:?}", layers), size),
size,
|b, &_size| {
b.iter(|| mla.append_file_content(id, data.len() as u64, data.as_slice()));
},
);
}
}
group.finish();
}
/// Benchmark the layer Compress only, using different block size
///
/// It should be noted the Throughput obtained depends on the data received and
/// do not represent the size of the emitted data.
/// Additionnals tests should be done to measure the compression gain between
/// the different levels
pub fn multiple_compression_quality(c: &mut Criterion) {
let size = 256 * KB;
// Setup
// Use a deterministic RNG in tests, for reproductability. DO NOT DO THIS IS IN ANY RELEASED BINARY!
let mut rng = ChaChaRng::seed_from_u64(0);
let mut group = c.benchmark_group("multiple_compression_quality");
group.measurement_time(Duration::from_secs(10));
for quality in 1..=11 {
group.throughput(Throughput::Bytes(size as u64));
let data: Vec<u8> = Alphanumeric
.sample_iter(&mut rng)
.take(size)
.map(|c| c as u8)
.collect();
// Create an archive
let file = Vec::new();
let mut config = ArchiveWriterConfig::new();
config
.enable_layer(Layers::COMPRESS)
.with_compression_level(quality)
.unwrap();
let mut mla = ArchiveWriter::from_config(file, config).expect("Writer init failed");
let id = mla.start_file("file").unwrap();
group.bench_with_input(
BenchmarkId::new(format!("CompressionLevel {}", quality), size),
&size,
|b, &_size| {
b.iter(|| mla.append_file_content(id, data.len() as u64, data.as_slice()));
},
);
}
group.finish();
}
/// Create an archive with a file of `iters`*`size` bytes using `layers` and
/// measure the time needed to read it
///
/// This function is used to measure only the read time without the cost of
/// creation nor file getting
fn iter_decompress(iters: u64, size: u64, layers: Layers) -> Duration {
// Prepare data
let mut rng = ChaChaRng::seed_from_u64(0);
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);
let key = StaticSecret::from(bytes);
let data: Vec<u8> = Alphanumeric
.sample_iter(&mut rng)
.take((size * iters) as usize)
.map(|c| c as u8)
.collect();
// Create an archive with one file
let file = Vec::new();
let mut config = ArchiveWriterConfig::new();
config
.enable_layer(layers)
.add_public_keys(&[PublicKey::from(&key)]);
let mut mla = ArchiveWriter::from_config(file, config).expect("Writer init failed");
let id = mla.start_file("file").unwrap();
mla.append_file_content(id, data.len() as u64, data.as_slice())
.unwrap();
mla.end_file(id).unwrap();
mla.finalize().unwrap();
// Prepare the reader
let dest = mla.into_raw();
let buf = Cursor::new(dest.as_slice());
let mut config = ArchiveReaderConfig::new();
config.add_private_keys(std::slice::from_ref(&key));
let mut mla_read = ArchiveReader::from_config(buf, config).unwrap();
// Get the file (costly as `seek` are implied)
let subfile = mla_read.get_file("file".to_string()).unwrap().unwrap();
// Read iters * size bytes
let start = Instant::now();
let mut src = subfile.data;
for _i in 0..iters {
io::copy(&mut (&mut src).take(size), &mut io::sink()).unwrap();
}
start.elapsed()
}
/// Benchmark the read speed depending on layers enabled and read size
pub fn multiple_layers_multiple_block_size_decompress(c: &mut Criterion) {
let mut group = c.benchmark_group("multiple_layers_multiple_block_size_decompress");
// Reduce the number of sample to avoid taking too much time
group.sample_size(SAMPLE_SIZE_SMALL);
for size in SIZE_LIST.iter() {
group.throughput(Throughput::Bytes(*size as u64));
for layers in &[
Layers::EMPTY,
Layers::COMPRESS,
Layers::ENCRYPT,
Layers::COMPRESS | Layers::ENCRYPT,
] {
group.bench_function(
BenchmarkId::new(format!("Layers {:?}", layers), size),
move |b| b.iter_custom(|iters| iter_decompress(iters, *size as u64, *layers)),
);
}
}
group.finish();
}
fn build_archive<'a>(
iters: u64,
size: u64,
layers: Layers,
) -> ArchiveReader<'a, io::Cursor<Vec<u8>>> {
// Setup
let mut rng = ChaChaRng::seed_from_u64(0);
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);
let key = StaticSecret::from(bytes);
let file = Vec::new();
// Create the initial archive with `iters` files of `size` bytes
let mut config = ArchiveWriterConfig::new();
config
.enable_layer(layers)
.add_public_keys(&[PublicKey::from(&key)]);
let mut mla = ArchiveWriter::from_config(file, config).expect("Writer init failed");
for i in 0..iters {
let data: Vec<u8> = Alphanumeric
.sample_iter(&mut rng)
.take(size as usize)
.map(|c| c as u8)
.collect();
let id = mla.start_file(&format!("file_{}", i)).unwrap();
mla.append_file_content(id, data.len() as u64, data.as_slice())
.unwrap();
mla.end_file(id).unwrap();
}
mla.finalize().unwrap();
// Instantiate the reader
let dest = mla.into_raw();
let buf = Cursor::new(dest);
let mut config = ArchiveReaderConfig::new();
config.add_private_keys(std::slice::from_ref(&key));
ArchiveReader::from_config(buf, config).unwrap()
}
/// Create an archive with a `iters` files of `size` bytes using `layers` and
/// measure the time needed to read them (in a random order)
///
/// This function is used to measure only the get_file + read time without the
/// cost of archive creation
fn iter_decompress_multifiles_random(iters: u64, size: u64, layers: Layers) -> Duration {
let mut mla_read = build_archive(iters, size, layers);
let mut rng = ChaChaRng::seed_from_u64(0);
// Measure the time needed to get and read a file
let start = Instant::now();
for i in sample(&mut rng, iters as usize, iters as usize).iter() {
let subfile = mla_read
.get_file(format!("file_{}", i).to_string())
.unwrap()
.unwrap();
let mut src = subfile.data;
io::copy(&mut (&mut src).take(size), &mut io::sink()).unwrap();
}
start.elapsed()
}
/// This benchmark measures the time needed to randomly pick a file and read it
///
/// This pattern should represent one of the common use of the library
pub fn multiple_layers_multiple_block_size_decompress_multifiles_random(c: &mut Criterion) {
static KB: usize = 1024;
static MB: usize = 1024 * KB;
let mut group = c.benchmark_group("chunk_size_decompress_mutilfiles_random");
// Reduce the number of sample to avoid taking too much time
group.sample_size(SAMPLE_SIZE_SMALL);
for size in [MB, 2 * MB, 4 * MB, 16 * MB].iter() {
group.throughput(Throughput::Bytes(*size as u64));
for layers in &[
Layers::EMPTY,
Layers::COMPRESS,
Layers::ENCRYPT,
Layers::COMPRESS | Layers::ENCRYPT,
] {
group.bench_function(
BenchmarkId::new(format!("Layers {:?}", layers), size),
move |b| {
b.iter_custom(|iters| {
iter_decompress_multifiles_random(iters, *size as u64, *layers)
})
},
);
}
}
group.finish();
}
/// Create an archive with a `iters` files of `size` bytes using `layers` and
/// measure the time needed to read them (linearyly)
///
/// This function is used to measure only `linear_extract` time without the cost
/// of archive creation
fn iter_decompress_multifiles_linear(iters: u64, size: u64, layers: Layers) -> Duration {
let mut mla_read = build_archive(iters, size, layers);
let fnames: Vec<String> = mla_read.list_files().unwrap().cloned().collect();
// Measure the time needed to get and read a file
// Prepare output
let mut export: HashMap<&String, io::Sink> =
fnames.iter().map(|fname| (fname, io::sink())).collect();
let start = Instant::now();
linear_extract(&mut mla_read, &mut export).unwrap();
start.elapsed()
}
/// This benchmark measures the time needed to compare the extraction time
/// between the "randomly pick" and "linear extraction"
///
/// The full extraction is a common pattern of use of the library. This
/// benchmark helps measuring the gain of using `linear_extract`.
pub fn linear_vs_normal_extract(c: &mut Criterion) {
static KB: usize = 1024;
static MB: usize = 1024 * KB;
let mut group = c.benchmark_group("linear_vs_normal_extract");
// Reduce the number of sample to avoid taking too much time
group.sample_size(SAMPLE_SIZE_SMALL);
for size in [MB, 2 * MB, 4 * MB, 16 * MB].iter() {
group.throughput(Throughput::Bytes(*size as u64));
for layers in &[
Layers::EMPTY,
Layers::COMPRESS,
Layers::ENCRYPT,
Layers::COMPRESS | Layers::ENCRYPT,
] {
group.bench_function(
BenchmarkId::new(format!("NORMAL / Layers {:?}", layers), size),
move |b| {
b.iter_custom(|iters| {
iter_decompress_multifiles_random(iters, *size as u64, *layers)
})
},
);
group.bench_function(
BenchmarkId::new(format!("LINEAR / Layers {:?}", layers), size),
move |b| {
b.iter_custom(|iters| {
iter_decompress_multifiles_linear(iters, *size as u64, *layers)
})
},
);
}
}
group.finish();
}
criterion_group!(
benches,
multiple_layers_multiple_block_size,
multiple_compression_quality,
multiple_layers_multiple_block_size_decompress,
multiple_layers_multiple_block_size_decompress_multifiles_random,
linear_vs_normal_extract,
);
criterion_main!(benches);