Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed May 24, 2024
1 parent 94765a4 commit 7a55945
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
72 changes: 64 additions & 8 deletions benches/read_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,92 @@ use bencher::{benchmark_group, benchmark_main};
use std::io::{Cursor, Write};

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

const FILE_COUNT: usize = 15_000;
const FILE_SIZE: usize = 1024;

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

let bytes = vec![0u8; file_size];
let mut bytes = vec![0u8; file_size];

for i in 0..count_files {
let name = format!("file_deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_{i}.dat");
writer.start_file(name, options).unwrap();
writer.write_all(&bytes).unwrap();
writer.start_file(name, options)?;
getrandom(&mut bytes).unwrap();
writer.write_all(&bytes)?;
}

writer.finish().unwrap().into_inner()
Ok(writer.finish()?.into_inner())
}

fn read_metadata(bench: &mut Bencher) {
let bytes = generate_random_archive(FILE_COUNT, FILE_SIZE);
let bytes = generate_random_archive(FILE_COUNT, FILE_SIZE).unwrap();

bench.iter(|| {
let archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap();
archive.len()
});
}

benchmark_group!(benches, read_metadata);
const COMMENT_SIZE: usize = 50_000;

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

let mut bytes = vec![0u8; comment_length];
getrandom(&mut bytes).unwrap();
writer.set_raw_comment(bytes);

writer.start_file("asdf.txt", options)?;
writer.write_all(b"asdf")?;

Ok(writer.finish()?.into_inner())
}

fn parse_comment(bench: &mut Bencher) {
let bytes = generate_random_zip32_archive_with_comment(COMMENT_SIZE).unwrap();

bench.iter(|| {
let archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap();
archive.len()
});
}

const COMMENT_SIZE_64: usize = 500_000;

fn generate_random_zip64_archive_with_comment(comment_length: usize) -> ZipResult<Vec<u8>> {
let data = Vec::new();
let mut writer = ZipWriter::new(Cursor::new(data));
let options = SimpleFileOptions::default()
.compression_method(CompressionMethod::Stored)
.large_file(true);

let mut bytes = vec![0u8; comment_length];
getrandom(&mut bytes).unwrap();
writer.set_raw_comment(bytes);

writer.start_file("asdf.txt", options)?;
writer.write_all(b"asdf")?;

Ok(writer.finish()?.into_inner())
}

fn parse_zip64_comment(bench: &mut Bencher) {
let bytes = generate_random_zip64_archive_with_comment(COMMENT_SIZE_64).unwrap();

bench.iter(|| {
let archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap();
archive.len()
});
}

benchmark_group!(benches, read_metadata, parse_comment, parse_zip64_comment);
benchmark_main!(benches);
1 change: 1 addition & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ pub(crate) fn make_reader(
}
}

#[derive(Debug)]
pub(crate) struct CentralDirectoryInfo {
pub(crate) archive_offset: u64,
pub(crate) directory_start: u64,
Expand Down
2 changes: 1 addition & 1 deletion src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl CentralDirectoryEnd {
const BYTES_BETWEEN_MAGIC_AND_COMMENT_SIZE: u64 = HEADER_SIZE - 6;
let file_length = reader.seek(io::SeekFrom::End(0))?;

let search_upper_bound = file_length.saturating_sub(MAX_HEADER_AND_COMMENT_SIZE);
let search_upper_bound = 0;

if file_length < HEADER_SIZE {
return Err(ZipError::InvalidArchive("Invalid zip header"));
Expand Down

0 comments on commit 7a55945

Please sign in to comment.