Skip to content

Commit

Permalink
add benchmarks for iterating through entries
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Aug 11, 2024
1 parent 568fcab commit 2731e7e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ default = [
name = "read_entry"
harness = false

[[bench]]
name = "decompress_sink"
harness = false

[[bench]]
name = "read_metadata"
harness = false
Expand Down
112 changes: 112 additions & 0 deletions benches/decompress_sink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#![cfg(all(feature = "_deflate-any", feature = "bzip2"))]

use bencher::{benchmark_group, benchmark_main};

use std::fs;
use std::io::prelude::*;
use std::path::Path;

use bencher::Bencher;

use zip::read::stream::{ZipStreamFileMetadata, ZipStreamReader, ZipStreamVisitor};
use zip::read::ZipFile;
use zip::unstable::read::streaming::StreamingArchive;
use zip::{result::ZipResult, ZipArchive};

/* This contains the compressed text of King Lear from Project Gutenberg, in the public domain. */
fn get_test_data() -> ZipResult<ZipArchive<fs::File>> {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/king-lear-compressed.zip");
let file = fs::File::open(path)?;
ZipArchive::new(file)
}

fn write_entry_to_sink_generic(bench: &mut Bencher) {
let mut archive = get_test_data().unwrap();
let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap();

bench.bytes = total_size;
bench.bench_n(1, |bench| {
bench.iter(|| {
for i in 0..archive.len() {
let mut f = archive.by_index_generic(i).unwrap();
std::io::copy(&mut f, &mut std::io::sink()).unwrap();
}
})
});
}

fn write_entry_to_sink_standard(bench: &mut Bencher) {
let mut archive = get_test_data().unwrap();
let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap();

bench.bytes = total_size;
bench.bench_n(1, |bench| {
bench.iter(|| {
for i in 0..archive.len() {
let mut f = archive.by_index(i).unwrap();
std::io::copy(&mut f, &mut std::io::sink()).unwrap();
}
})
});
}

fn write_stream_to_sink_generic(bench: &mut Bencher) {
let archive = get_test_data().unwrap();
let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap();

let mut reader = archive.into_inner();

bench.bytes = total_size;
bench.bench_n(1, |bench| {
bench.iter(|| {
reader.rewind().unwrap();
let mut stream_zip = StreamingArchive::new(&mut reader);

while let Some(mut file) = stream_zip.next_entry().unwrap() {
std::io::copy(&mut file, &mut std::io::sink()).unwrap();
}
while stream_zip.next_metadata_entry().unwrap().is_some() {}
})
});
}

fn write_stream_to_sink_standard(bench: &mut Bencher) {
let archive = get_test_data().unwrap();
let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap();

struct V;
impl ZipStreamVisitor for V {
fn visit_file(&mut self, file: &mut ZipFile) -> ZipResult<()> {
std::io::copy(file, &mut std::io::sink())?;
Ok(())
}
fn visit_additional_metadata(
&mut self,
_metadata: &ZipStreamFileMetadata,
) -> ZipResult<()> {
Ok(())
}
}

let mut reader = archive.into_inner();

bench.bytes = total_size;
bench.bench_n(1, |bench| {
bench.iter(|| {
reader.rewind().unwrap();
let stream_zip = ZipStreamReader::new(&mut reader);

stream_zip.visit(&mut V).unwrap();
})
});
}

benchmark_group!(
benches,
write_entry_to_sink_generic,
write_entry_to_sink_standard,
write_stream_to_sink_generic,
write_stream_to_sink_standard,
);

benchmark_main!(benches);

Check failure on line 112 in benches/decompress_sink.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--no-default-features)

`main` function not found in crate `decompress_sink`
8 changes: 5 additions & 3 deletions benches/read_entry.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use bencher::{benchmark_group, benchmark_main};

use std::io::{Cursor, Read, Write};
use std::io::{prelude::*, Cursor};

use bencher::Bencher;
use getrandom::getrandom;
use zip::{write::SimpleFileOptions, ZipArchive, ZipWriter};

use zip::{write::SimpleFileOptions, CompressionMethod, ZipArchive, ZipWriter};

fn generate_random_archive(size: usize) -> Vec<u8> {
let data = Vec::new();
let mut writer = ZipWriter::new(Cursor::new(data));
let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
let options = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);

writer.start_file("random.dat", options).unwrap();
let mut bytes = vec![0u8; size];
Expand Down Expand Up @@ -39,4 +40,5 @@ fn read_entry(bench: &mut Bencher) {
}

benchmark_group!(benches, read_entry);

benchmark_main!(benches);
2 changes: 1 addition & 1 deletion src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod config;
pub use config::*;

/// Provides high level API for reading from a stream.
pub(crate) mod stream;
pub mod stream;

#[cfg(feature = "lzma")]
pub(crate) mod lzma;
Expand Down
Binary file added tests/data/king-lear-compressed.zip
Binary file not shown.

0 comments on commit 2731e7e

Please sign in to comment.