-
Notifications
You must be signed in to change notification settings - Fork 86
pipelined extraction #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
pipelined extraction #236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use bencher::{benchmark_group, benchmark_main}; | ||
|
||
use bencher::Bencher; | ||
use tempdir::TempDir; | ||
use tempfile::tempfile; | ||
|
||
use std::fs; | ||
use std::path::Path; | ||
use std::sync::{LazyLock, Mutex}; | ||
|
||
use zip::result::ZipResult; | ||
use zip::write::ZipWriter; | ||
use zip::ZipArchive; | ||
|
||
#[cfg(all(feature = "parallelism", unix))] | ||
use zip::read::{split_extract, ExtractionParameters}; | ||
|
||
/* This archive has a set of entries repeated 20x: | ||
* - 200K random data, stored uncompressed (CompressionMethod::Stored) | ||
* - 246K text data (the project gutenberg html version of king lear) | ||
* (CompressionMethod::Bzip2, compression level 1) (project gutenberg ebooks are public domain) | ||
* | ||
* The full archive file is 5.3MB. | ||
*/ | ||
fn static_test_archive() -> ZipResult<ZipArchive<fs::File>> { | ||
assert!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a |
||
cfg!(feature = "bzip2"), | ||
"this test archive requires bzip2 support" | ||
); | ||
let path = | ||
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/stored-and-compressed-text.zip"); | ||
let file = fs::File::open(path)?; | ||
ZipArchive::new(file) | ||
} | ||
|
||
static STATIC_TEST_ARCHIVE: LazyLock<Mutex<ZipArchive<fs::File>>> = LazyLock::new(|| { | ||
let archive = static_test_archive().unwrap(); | ||
Mutex::new(archive) | ||
}); | ||
|
||
/* This archive is generated dynamically, in order to scale with the number of reported CPUs. | ||
* - We want at least 768 files (4 per VCPU on EC2 *.48xlarge instances) to run in CI. | ||
* - We want to retain the interspersed random/text entries from static_test_archive(). | ||
* | ||
* We will copy over entries from the static archive repeatedly until we reach the desired file | ||
* count. | ||
*/ | ||
fn dynamic_test_archive(src_archive: &mut ZipArchive<fs::File>) -> ZipResult<ZipArchive<fs::File>> { | ||
let desired_num_entries: usize = num_cpus::get() * 4; | ||
let mut output_archive = ZipWriter::new(tempfile()?); | ||
|
||
for (src_index, output_index) in (0..src_archive.len()).cycle().zip(0..desired_num_entries) { | ||
let src_file = src_archive.by_index_raw(src_index)?; | ||
let output_name = if src_file.name().starts_with("random-") { | ||
format!("random-{output_index}.dat") | ||
} else { | ||
assert!(src_file.name().starts_with("text-")); | ||
format!("text-{output_index}.dat") | ||
}; | ||
output_archive.raw_copy_file_rename(src_file, output_name)?; | ||
} | ||
|
||
output_archive.finish_into_readable() | ||
} | ||
|
||
static DYNAMIC_TEST_ARCHIVE: LazyLock<Mutex<ZipArchive<fs::File>>> = LazyLock::new(|| { | ||
let mut src = STATIC_TEST_ARCHIVE.lock().unwrap(); | ||
let archive = dynamic_test_archive(&mut src).unwrap(); | ||
Mutex::new(archive) | ||
}); | ||
|
||
fn do_extract_basic(bench: &mut Bencher, archive: &mut ZipArchive<fs::File>) { | ||
let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap(); | ||
|
||
let parent = TempDir::new("zip-extract").unwrap(); | ||
|
||
bench.bytes = total_size; | ||
bench.bench_n(1, |bench| { | ||
bench.iter(move || { | ||
let outdir = TempDir::new_in(parent.path(), "bench-subdir") | ||
.unwrap() | ||
.into_path(); | ||
archive.extract(outdir).unwrap(); | ||
}); | ||
}); | ||
} | ||
|
||
fn extract_basic_static(bench: &mut Bencher) { | ||
let mut archive = STATIC_TEST_ARCHIVE.lock().unwrap(); | ||
do_extract_basic(bench, &mut archive); | ||
} | ||
|
||
fn extract_basic_dynamic(bench: &mut Bencher) { | ||
let mut archive = DYNAMIC_TEST_ARCHIVE.lock().unwrap(); | ||
do_extract_basic(bench, &mut archive); | ||
} | ||
|
||
#[cfg(all(feature = "parallelism", unix))] | ||
fn do_extract_split(bench: &mut Bencher, archive: &ZipArchive<fs::File>) { | ||
let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap(); | ||
|
||
let params = ExtractionParameters { | ||
decompression_threads: num_cpus::get() / 3, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will the other 2/3 of the CPUs be doing? Also, does this need to be clamped to at least 1? |
||
..Default::default() | ||
}; | ||
|
||
let parent = TempDir::new("zip-extract").unwrap(); | ||
|
||
bench.bytes = total_size; | ||
bench.bench_n(1, |bench| { | ||
bench.iter(move || { | ||
let outdir = TempDir::new_in(parent.path(), "bench-subdir") | ||
.unwrap() | ||
.into_path(); | ||
split_extract(archive, &outdir, params.clone()).unwrap(); | ||
}); | ||
}); | ||
} | ||
|
||
#[cfg(all(feature = "parallelism", unix))] | ||
fn extract_split_static(bench: &mut Bencher) { | ||
let archive = STATIC_TEST_ARCHIVE.lock().unwrap(); | ||
do_extract_split(bench, &archive); | ||
} | ||
|
||
#[cfg(all(feature = "parallelism", unix))] | ||
fn extract_split_dynamic(bench: &mut Bencher) { | ||
let archive = DYNAMIC_TEST_ARCHIVE.lock().unwrap(); | ||
do_extract_split(bench, &archive); | ||
} | ||
|
||
#[cfg(not(all(feature = "parallelism", unix)))] | ||
benchmark_group!(benches, extract_basic_static, extract_basic_dynamic); | ||
|
||
#[cfg(all(feature = "parallelism", unix))] | ||
benchmark_group!( | ||
benches, | ||
extract_basic_static, | ||
extract_basic_dynamic, | ||
extract_split_static, | ||
extract_split_dynamic | ||
); | ||
|
||
benchmark_main!(benches); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we include some compressed files that contain both text and random bytes, to reflect the fact that real files tend to have sections with different entropy rates (e.g. image content vs metadata)?