Use Criterion for benchmarks

This commit is contained in:
Jack Fletcher 2021-06-07 02:51:06 +01:00
parent e43ac72f7d
commit 6c1bd78a6b
2 changed files with 51 additions and 23 deletions

View file

@ -20,7 +20,7 @@ thiserror = "1.0"
zstd = { version = "0.8", optional = true } zstd = { version = "0.8", optional = true }
[dev-dependencies] [dev-dependencies]
bencher = "0.1" criterion = "0.3"
rand = "0.7" rand = "0.7"
walkdir = "2" walkdir = "2"

View file

@ -1,16 +1,16 @@
use bencher::{benchmark_group, benchmark_main}; use criterion::{BenchmarkId, Criterion, Throughput};
use criterion::{criterion_group, criterion_main};
use std::io::{Cursor, Read, Write}; use std::io::{Cursor, Read, Write};
use bencher::Bencher;
use rand::Rng; use rand::Rng;
use zip::{ZipArchive, ZipWriter}; use zip::{CompressionMethod, ZipArchive, ZipWriter};
fn generate_random_archive(size: usize) -> Vec<u8> { fn generate_random_archive(size: usize, method: Option<CompressionMethod>) -> Vec<u8> {
let data = Vec::new(); let data = Vec::new();
let mut writer = ZipWriter::new(Cursor::new(data)); let mut writer = ZipWriter::new(Cursor::new(data));
let options = let options = zip::write::FileOptions::default()
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored); .compression_method(method.unwrap_or(CompressionMethod::Stored));
writer.start_file("random.dat", options).unwrap(); writer.start_file("random.dat", options).unwrap();
@ -23,24 +23,52 @@ fn generate_random_archive(size: usize) -> Vec<u8> {
writer.finish().unwrap().into_inner() writer.finish().unwrap().into_inner()
} }
fn read_entry(bench: &mut Bencher) { fn read_entry(bench: &mut Criterion) {
let size = 1024 * 1024; let size = 1024 * 1024;
let bytes = generate_random_archive(size); let bytes = generate_random_archive(size, None);
let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap();
bench.iter(|| { //
let mut file = archive.by_name("random.dat").unwrap(); let mut group = bench.benchmark_group("read_entry");
let mut buf = [0u8; 1024];
loop {
let n = file.read(&mut buf).unwrap();
if n == 0 {
break;
}
}
});
bench.bytes = size as u64; //
for method in CompressionMethod::supported_methods().iter() {
group.bench_with_input(BenchmarkId::from_parameter(method), method, |b, method| {
b.iter(|| {
let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap();
let mut file = archive.by_name("random.dat").unwrap();
let mut buf = [0u8; 1024];
let mut total_bytes = 0;
loop {
let n = file.read(&mut buf).unwrap();
total_bytes += n;
if n == 0 {
return total_bytes
}
}
});
});
}
} }
benchmark_group!(benches, read_entry); fn write_random_archive(bench: &mut Criterion) {
benchmark_main!(benches); let size = 1024 * 1024;
//
let mut group = bench.benchmark_group("write_random_archive");
//
for method in CompressionMethod::supported_methods().iter() {
group.bench_with_input(BenchmarkId::from_parameter(method), method, |b, method| {
b.iter(|| {
generate_random_archive(size, Some(method.clone()));
})
});
}
group.finish();
}
criterion_group!(benches, read_entry, write_random_archive);
criterion_main!(benches);