diff --git a/Cargo.toml b/Cargo.toml index 2286b58f..1afd00d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ thiserror = "1.0" zstd = { version = "0.8", optional = true } [dev-dependencies] -bencher = "0.1" +criterion = "0.3" rand = "0.7" walkdir = "2" diff --git a/benches/read_entry.rs b/benches/read_entry.rs index 97417312..79288cbd 100644 --- a/benches/read_entry.rs +++ b/benches/read_entry.rs @@ -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 bencher::Bencher; use rand::Rng; -use zip::{ZipArchive, ZipWriter}; +use zip::{CompressionMethod, ZipArchive, ZipWriter}; -fn generate_random_archive(size: usize) -> Vec { +fn generate_random_archive(size: usize, method: Option) -> Vec { let data = Vec::new(); let mut writer = ZipWriter::new(Cursor::new(data)); - let options = - zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored); + let options = zip::write::FileOptions::default() + .compression_method(method.unwrap_or(CompressionMethod::Stored)); writer.start_file("random.dat", options).unwrap(); @@ -23,24 +23,52 @@ fn generate_random_archive(size: usize) -> Vec { writer.finish().unwrap().into_inner() } -fn read_entry(bench: &mut Bencher) { +fn read_entry(bench: &mut Criterion) { let size = 1024 * 1024; - let bytes = generate_random_archive(size); - let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); + let bytes = generate_random_archive(size, None); - bench.iter(|| { - let mut file = archive.by_name("random.dat").unwrap(); - let mut buf = [0u8; 1024]; - loop { - let n = file.read(&mut buf).unwrap(); - if n == 0 { - break; - } - } - }); + // + let mut group = bench.benchmark_group("read_entry"); - 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); -benchmark_main!(benches); +fn write_random_archive(bench: &mut Criterion) { + 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);