|
| 1 | +use bencher::{benchmark_group, benchmark_main}; |
| 2 | + |
| 3 | +use bencher::Bencher; |
| 4 | +use tempdir::TempDir; |
| 5 | +use tempfile::tempfile; |
| 6 | + |
| 7 | +use std::fs; |
| 8 | +use std::path::Path; |
| 9 | +use std::sync::{LazyLock, Mutex}; |
| 10 | + |
| 11 | +use zip::result::ZipResult; |
| 12 | +use zip::write::ZipWriter; |
| 13 | +use zip::ZipArchive; |
| 14 | + |
| 15 | +#[cfg(all(feature = "parallelism", unix))] |
| 16 | +use zip::read::{split_extract, ExtractionParameters}; |
| 17 | + |
| 18 | +/* This archive has a set of entries repeated 20x: |
| 19 | + * - 200K random data, stored uncompressed (CompressionMethod::Stored) |
| 20 | + * - 246K text data (the project gutenberg html version of king lear) |
| 21 | + * (CompressionMethod::Bzip2, compression level 1) (project gutenberg ebooks are public domain) |
| 22 | + * |
| 23 | + * The full archive file is 5.3MB. |
| 24 | + */ |
| 25 | +fn static_test_archive() -> ZipResult<ZipArchive<fs::File>> { |
| 26 | + assert!( |
| 27 | + cfg!(feature = "bzip2"), |
| 28 | + "this test archive requires bzip2 support" |
| 29 | + ); |
| 30 | + let path = |
| 31 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/stored-and-compressed-text.zip"); |
| 32 | + let file = fs::File::open(path)?; |
| 33 | + ZipArchive::new(file) |
| 34 | +} |
| 35 | + |
| 36 | +static STATIC_TEST_ARCHIVE: LazyLock<Mutex<ZipArchive<fs::File>>> = LazyLock::new(|| { |
| 37 | + let archive = static_test_archive().unwrap(); |
| 38 | + Mutex::new(archive) |
| 39 | +}); |
| 40 | + |
| 41 | +/* This archive is generated dynamically, in order to scale with the number of reported CPUs. |
| 42 | + * - We want at least 768 files (4 per VCPU on EC2 *.48xlarge instances) to run in CI. |
| 43 | + * - We want to retain the interspersed random/text entries from static_test_archive(). |
| 44 | + * |
| 45 | + * We will copy over entries from the static archive repeatedly until we reach the desired file |
| 46 | + * count. |
| 47 | + */ |
| 48 | +fn dynamic_test_archive(src_archive: &mut ZipArchive<fs::File>) -> ZipResult<ZipArchive<fs::File>> { |
| 49 | + let desired_num_entries: usize = num_cpus::get() * 4; |
| 50 | + let mut output_archive = ZipWriter::new(tempfile()?); |
| 51 | + |
| 52 | + for (src_index, output_index) in (0..src_archive.len()).cycle().zip(0..desired_num_entries) { |
| 53 | + let src_file = src_archive.by_index_raw(src_index)?; |
| 54 | + let output_name = if src_file.name().starts_with("random-") { |
| 55 | + format!("random-{output_index}.dat") |
| 56 | + } else { |
| 57 | + assert!(src_file.name().starts_with("text-")); |
| 58 | + format!("text-{output_index}.dat") |
| 59 | + }; |
| 60 | + output_archive.raw_copy_file_rename(src_file, output_name)?; |
| 61 | + } |
| 62 | + |
| 63 | + output_archive.finish_into_readable() |
| 64 | +} |
| 65 | + |
| 66 | +static DYNAMIC_TEST_ARCHIVE: LazyLock<Mutex<ZipArchive<fs::File>>> = LazyLock::new(|| { |
| 67 | + let mut src = STATIC_TEST_ARCHIVE.lock().unwrap(); |
| 68 | + let archive = dynamic_test_archive(&mut src).unwrap(); |
| 69 | + Mutex::new(archive) |
| 70 | +}); |
| 71 | + |
| 72 | +fn do_extract_basic(bench: &mut Bencher, archive: &mut ZipArchive<fs::File>) { |
| 73 | + let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap(); |
| 74 | + |
| 75 | + let parent = TempDir::new("zip-extract").unwrap(); |
| 76 | + |
| 77 | + bench.bytes = total_size; |
| 78 | + bench.bench_n(1, |bench| { |
| 79 | + bench.iter(move || { |
| 80 | + let outdir = TempDir::new_in(parent.path(), "bench-subdir") |
| 81 | + .unwrap() |
| 82 | + .into_path(); |
| 83 | + archive.extract(outdir).unwrap(); |
| 84 | + }); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +fn extract_basic_static(bench: &mut Bencher) { |
| 89 | + let mut archive = STATIC_TEST_ARCHIVE.lock().unwrap(); |
| 90 | + do_extract_basic(bench, &mut archive); |
| 91 | +} |
| 92 | + |
| 93 | +fn extract_basic_dynamic(bench: &mut Bencher) { |
| 94 | + let mut archive = DYNAMIC_TEST_ARCHIVE.lock().unwrap(); |
| 95 | + do_extract_basic(bench, &mut archive); |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(all(feature = "parallelism", unix))] |
| 99 | +fn do_extract_split(bench: &mut Bencher, archive: &ZipArchive<fs::File>) { |
| 100 | + let total_size: u64 = archive.decompressed_size().unwrap().try_into().unwrap(); |
| 101 | + |
| 102 | + let params = ExtractionParameters { |
| 103 | + decompression_threads: num_cpus::get() / 3, |
| 104 | + ..Default::default() |
| 105 | + }; |
| 106 | + |
| 107 | + let parent = TempDir::new("zip-extract").unwrap(); |
| 108 | + |
| 109 | + bench.bytes = total_size; |
| 110 | + bench.bench_n(1, |bench| { |
| 111 | + bench.iter(move || { |
| 112 | + let outdir = TempDir::new_in(parent.path(), "bench-subdir") |
| 113 | + .unwrap() |
| 114 | + .into_path(); |
| 115 | + split_extract(archive, &outdir, params.clone()).unwrap(); |
| 116 | + }); |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +#[cfg(all(feature = "parallelism", unix))] |
| 121 | +fn extract_split_static(bench: &mut Bencher) { |
| 122 | + let archive = STATIC_TEST_ARCHIVE.lock().unwrap(); |
| 123 | + do_extract_split(bench, &archive); |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(all(feature = "parallelism", unix))] |
| 127 | +fn extract_split_dynamic(bench: &mut Bencher) { |
| 128 | + let archive = DYNAMIC_TEST_ARCHIVE.lock().unwrap(); |
| 129 | + do_extract_split(bench, &archive); |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(not(all(feature = "parallelism", unix)))] |
| 133 | +benchmark_group!(benches, extract_basic_static, extract_basic_dynamic); |
| 134 | + |
| 135 | +#[cfg(all(feature = "parallelism", unix))] |
| 136 | +benchmark_group!( |
| 137 | + benches, |
| 138 | + extract_basic_static, |
| 139 | + extract_basic_dynamic, |
| 140 | + extract_split_static, |
| 141 | + extract_split_dynamic |
| 142 | +); |
| 143 | + |
| 144 | +benchmark_main!(benches); |
0 commit comments