From e01181f29fd67af00eb9a625d62adcd432d2d2cf Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Tue, 15 Oct 2024 13:40:16 +0800 Subject: [PATCH] switch to binggan from criterion --- Cargo.toml | 8 +- benches/binggan_bench.rs | 77 ++++--- benches/crit_bench.rs | 431 --------------------------------------- 3 files changed, 51 insertions(+), 465 deletions(-) delete mode 100644 benches/crit_bench.rs diff --git a/Cargo.toml b/Cargo.toml index 16489f71..efbc7be4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,14 +16,13 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dev-dependencies] -criterion = { git = "https://github.com/PSeitz/criterion.rs/", rev = "cf60ffc"} lzzzz = "1.0.4" lz4-compress = "0.1.1" more-asserts = "0.3.1" snap = "1.1.0" serde_json = "1.0.91" proptest = "1.0.0" -binggan = { version = "0.8"} +binggan = { version = "0.12.0"} jemallocator = "0.5.4" [dev-dependencies.lz-fear] @@ -66,11 +65,6 @@ harness = false name = "binggan_bench" path = "benches/binggan_bench.rs" -#[[bench]] -#harness = false -#name = "crit_bench" -#path = "benches/crit_bench.rs" - # [[bench]] # harness = false # name = "quickbench" diff --git a/benches/binggan_bench.rs b/benches/binggan_bench.rs index 8ebb77b0..b15ba54f 100644 --- a/benches/binggan_bench.rs +++ b/benches/binggan_bench.rs @@ -1,13 +1,11 @@ #![allow(dead_code)] -extern crate criterion; #[allow(unused)] use std::io::{Read, Write}; -use binggan::black_box; -use binggan::BenchRunner; -use binggan::InputGroup; -use binggan::PeakMemAlloc; +use binggan::plugins::*; +use binggan::*; + use lz_fear::raw::compress2; use lz_fear::raw::decompress_raw; use lz_fear::raw::U16Table; @@ -52,26 +50,36 @@ fn main() { #[cfg(feature = "frame")] fn frame_decompress(data_sets: &[(String, Vec)]) { let mut runner = BenchRunner::with_name("frame_decompress"); - runner.set_alloc(&GLOBAL); + runner + .add_plugin(PerfCounterPlugin::default()) + .add_plugin(PeakMemAllocPlugin::new(&GLOBAL)); for (name, data_set) in data_sets { - let compressed_independent = lz4_cpp_frame_compress(&data_set, true).unwrap(); - let compressed_linked = lz4_cpp_frame_compress(&data_set, false).unwrap(); + let compressed_independent = lz4_cpp_frame_compress(data_set, true).unwrap(); + let compressed_linked = lz4_cpp_frame_compress(data_set, false).unwrap(); + let comp_snap = compress_snap_frame(data_set); let mut group = runner.new_group(); group.set_name(name); group.set_input_size(data_set.len()); - //runner.enable_perf(); group.register_with_input("lz4 flex independent", &compressed_independent, move |i| { black_box(lz4_flex_frame_decompress(i).unwrap()); + Some(()) }); group.register_with_input("lz4 c90 independent", &compressed_independent, move |i| { black_box(lz4_cpp_frame_decompress(i).unwrap()); + Some(()) }); group.register_with_input("lz4 flex linked", &compressed_linked, move |i| { black_box(lz4_flex_frame_decompress(i).unwrap()); + Some(()) }); group.register_with_input("lz4 c90 linked", &compressed_linked, move |i| { black_box(lz4_cpp_frame_decompress(i).unwrap()); + Some(()) + }); + group.register_with_input("snap", &comp_snap, move |i| { + black_box(decompress_snap_frame(i)); + Some(()) }); group.run(); @@ -79,46 +87,58 @@ fn frame_decompress(data_sets: &[(String, Vec)]) { } #[cfg(feature = "frame")] -fn frame_compress(mut runner: InputGroup>) { - runner.set_name("frame_compress".to_string()); - runner.set_alloc(&GLOBAL); +fn frame_compress(mut runner: InputGroup, usize>) { + runner.set_name("frame_compress"); + runner.add_plugin(PeakMemAllocPlugin::new(&GLOBAL)); runner.throughput(|data| data.len()); runner.register("lz4 flex independent", move |i| { let mut frame_info = lz4_flex::frame::FrameInfo::new(); frame_info.block_size = lz4_flex::frame::BlockSize::Max256KB; frame_info.block_mode = lz4_flex::frame::BlockMode::Independent; - black_box(lz4_flex_frame_compress_with(frame_info, i).unwrap()); + let out = black_box(lz4_flex_frame_compress_with(frame_info, i).unwrap()); + Some(out.len()) }); runner.register("lz4 c90 indep", move |i| { - black_box(lz4_cpp_frame_compress(i, true).unwrap()); + let out = black_box(lz4_cpp_frame_compress(i, true).unwrap()); + Some(out.len()) }); runner.register("lz4 flex linked", move |i| { let mut frame_info = lz4_flex::frame::FrameInfo::new(); frame_info.block_size = lz4_flex::frame::BlockSize::Max256KB; frame_info.block_mode = lz4_flex::frame::BlockMode::Linked; - black_box(lz4_flex_frame_compress_with(frame_info, i).unwrap()); + let out = black_box(lz4_flex_frame_compress_with(frame_info, i).unwrap()); + Some(out.len()) }); runner.register("lz4 c90 linked", move |i| { - black_box(lz4_cpp_frame_compress(i, false).unwrap()); + let out = black_box(lz4_cpp_frame_compress(i, false).unwrap()); + Some(out.len()) + }); + runner.register("snap", move |i| { + let out = compress_snap_frame(i); + Some(out.len()) }); runner.run(); } -fn block_compress(mut runner: InputGroup>) { - runner.set_name("block_compress".to_string()); - runner.set_alloc(&GLOBAL); // Set the peak mem allocator. This will enable peak memory reporting. - //runner.enable_perf(); +fn block_compress(mut runner: InputGroup, usize>) { + runner.set_name("block_compress"); + // Set the peak mem allocator. This will enable peak memory reporting. + runner.add_plugin(PeakMemAllocPlugin::new(&GLOBAL)); + runner.throughput(|data| data.len()); runner.register("lz4 flex", move |i| { - black_box(lz4_flex::compress(i)); + let out = black_box(lz4_flex::compress(i)); + Some(out.len()) }); runner.register("lz4 c90", move |i| { - black_box(lz4_cpp_block_compress(i).unwrap()); + let out = black_box(lz4_cpp_block_compress(i).unwrap()); + Some(out.len()) }); runner.register("snap", move |i| { - black_box(compress_snap(i)); + let out = black_box(compress_snap(i)); + Some(out.len()) }); runner.run(); @@ -126,7 +146,8 @@ fn block_compress(mut runner: InputGroup>) { fn block_decompress() { let mut runner = BenchRunner::with_name("block_decompress"); - runner.set_alloc(&GLOBAL); + // Set the peak mem allocator. This will enable peak memory reporting. + runner.add_plugin(PeakMemAllocPlugin::new(&GLOBAL)); for data_uncomp in ALL { let comp_lz4 = lz4_cpp_block_compress(data_uncomp).unwrap(); let bundle = (comp_lz4, data_uncomp.len()); @@ -137,10 +158,12 @@ fn block_decompress() { group.set_input_size(data_uncomp.len()); group.register_with_input("lz4 flex", &bundle, move |i| { - black_box(lz4_flex::decompress(&i.0, i.1).unwrap()); + let size = black_box(lz4_flex::decompress(&i.0, i.1).unwrap()); + Some(size.len()) }); group.register_with_input("lz4 c90", &bundle, move |i| { - black_box(lz4_cpp_block_decompress(&i.0, i.1).unwrap()); + let size = black_box(lz4_cpp_block_decompress(&i.0, i.1).unwrap()); + Some(size.len()) }); group.run(); @@ -179,7 +202,7 @@ fn compress_lz4_fear(input: &[u8]) -> Vec { fn decompress_lz4_fear(input: &[u8]) -> Vec { let mut vec = Vec::new(); - decompress_raw(input, &[], &mut vec, std::usize::MAX).unwrap(); + decompress_raw(input, &[], &mut vec, usize::MAX).unwrap(); vec } diff --git a/benches/crit_bench.rs b/benches/crit_bench.rs deleted file mode 100644 index 470e4cd0..00000000 --- a/benches/crit_bench.rs +++ /dev/null @@ -1,431 +0,0 @@ -#![allow(dead_code)] -extern crate criterion; - -#[allow(unused)] -use std::io::{Read, Write}; - -use self::criterion::*; -use lz_fear::raw::compress2; -use lz_fear::raw::decompress_raw; -use lz_fear::raw::U16Table; -use lz_fear::raw::U32Table; - -const COMPRESSION1K: &[u8] = include_bytes!("compression_1k.txt"); -const COMPRESSION34K: &[u8] = include_bytes!("compression_34k.txt"); -const COMPRESSION65K: &[u8] = include_bytes!("compression_65k.txt"); -const COMPRESSION66K: &[u8] = include_bytes!("compression_66k_JSON.txt"); -const COMPRESSION10MB: &[u8] = include_bytes!("dickens.txt"); -const COMPRESSION95K_VERY_GOOD_LOGO: &[u8] = include_bytes!("../logo.jpg"); - -const ALL: &[&[u8]] = &[ - COMPRESSION1K as &[u8], - COMPRESSION34K as &[u8], - COMPRESSION65K as &[u8], - COMPRESSION66K as &[u8], - COMPRESSION10MB as &[u8], - COMPRESSION95K_VERY_GOOD_LOGO as &[u8], -]; - -fn get_frame_datasets() -> Vec> { - let paths = [ - "compression_1k.txt", - "dickens.txt", - "hdfs.json", - "reymont.pdf", - "xml_collection.xml", - ]; - paths - .iter() - .map(|path| { - let path = std::path::Path::new("benches").join(path); - let mut file = std::fs::File::open(path).unwrap(); - let mut buf = Vec::new(); - file.read_to_end(&mut buf).unwrap(); - buf - }) - .collect() -} - -fn compress_lz4_fear(input: &[u8]) -> Vec { - let mut buf = Vec::new(); - if input.len() <= 0xFFFF { - compress2(input, 0, &mut U16Table::default(), &mut buf).unwrap(); - } else { - compress2(input, 0, &mut U32Table::default(), &mut buf).unwrap(); - } - buf -} - -fn decompress_lz4_fear(input: &[u8]) -> Vec { - let mut vec = Vec::new(); - decompress_raw(input, &[], &mut vec, std::usize::MAX).unwrap(); - vec -} - -fn compress_snap(input: &[u8]) -> Vec { - snap::raw::Encoder::new().compress_vec(input).unwrap() -} - -fn decompress_snap(input: &[u8]) -> Vec { - snap::raw::Decoder::new().decompress_vec(input).unwrap() -} - -#[cfg(feature = "frame")] -fn compress_snap_frame(input: &[u8]) -> Vec { - let mut fe = snap::write::FrameEncoder::new(Vec::new()); - fe.write_all(input).unwrap(); - fe.into_inner().unwrap() -} - -#[cfg(feature = "frame")] -fn decompress_snap_frame(input: &[u8]) -> Vec { - let mut out = Vec::new(); - let mut fe = snap::read::FrameDecoder::new(input); - fe.read_to_end(&mut out).unwrap(); - out -} - -fn lz4_cpp_block_decompress(input: &[u8], decomp_len: usize) -> Result, lzzzz::Error> { - let mut out = vec![0u8; decomp_len]; - lzzzz::lz4::decompress(input, &mut out)?; - Ok(out) -} - -fn lz4_cpp_block_compress(input: &[u8]) -> Result, lzzzz::Error> { - let mut out = Vec::new(); - lzzzz::lz4::compress_to_vec(input, &mut out, lzzzz::lz4::ACC_LEVEL_DEFAULT).unwrap(); - Ok(out) -} - -#[cfg(feature = "frame")] -fn lz4_cpp_frame_compress(input: &[u8], independent: bool) -> Result, lzzzz::Error> { - let pref = lzzzz::lz4f::PreferencesBuilder::new() - .block_mode(if independent { - lzzzz::lz4f::BlockMode::Independent - } else { - lzzzz::lz4f::BlockMode::Linked - }) - .block_size(lzzzz::lz4f::BlockSize::Max64KB) - .build(); - let mut comp = lzzzz::lz4f::WriteCompressor::new(Vec::new(), pref).unwrap(); - comp.write_all(input).unwrap(); - let out = comp.into_inner(); - - Ok(out) -} - -#[cfg(feature = "frame")] -fn lz4_cpp_frame_decompress(mut input: &[u8]) -> Result, lzzzz::lz4f::Error> { - let mut r = lzzzz::lz4f::ReadDecompressor::new(&mut input)?; - let mut buf = Vec::new(); - r.read_to_end(&mut buf).unwrap(); - - Ok(buf) -} - -#[cfg(feature = "frame")] -pub fn lz4_flex_frame_compress_with( - frame_info: lz4_flex::frame::FrameInfo, - input: &[u8], -) -> Result, lz4_flex::frame::Error> { - let buffer = Vec::new(); - let mut enc = lz4_flex::frame::FrameEncoder::with_frame_info(frame_info, buffer); - enc.write_all(input)?; - enc.finish() -} - -#[cfg(feature = "frame")] -pub fn lz4_flex_frame_decompress(input: &[u8]) -> Result, lz4_flex::frame::Error> { - let mut de = lz4_flex::frame::FrameDecoder::new(input); - let mut out = Vec::new(); - de.read_to_end(&mut out)?; - Ok(out) -} - -//pub fn lz4_flex_master_frame_compress_with( -//frame_info: lz4_flex_master::frame::FrameInfo, -//input: &[u8], -//) -> Result, lz4_flex_master::frame::Error> { -//let buffer = Vec::new(); -//let mut enc = lz4_flex_master::frame::FrameEncoder::with_frame_info(frame_info, buffer); -//enc.write_all(input)?; -//Ok(enc.finish()?) -//} - -//pub fn lz4_flex_master_frame_decompress( -//input: &[u8], -//) -> Result, lz4_flex_master::frame::Error> { -//let mut de = lz4_flex_master::frame::FrameDecoder::new(input); -//let mut out = Vec::new(); -//de.read_to_end(&mut out)?; -//Ok(out) -//} - -fn bench_block_compression_throughput(c: &mut Criterion) { - let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Linear); - - let mut group = c.benchmark_group("BlockCompress"); - group.plot_config(plot_config); - - for input in ALL.iter() { - let input_bytes = input.len() as u64; - group.throughput(Throughput::Bytes(input_bytes)); - - group.bench_with_input( - BenchmarkId::new("lz4_flex_rust", input_bytes), - &input, - |b, i| b.iter(|| lz4_flex::compress(i)), - ); - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_v10_rust", input_bytes), - //&input, - //|b, i| b.iter(|| lz4_flex_master::compress(i)), - //); - - // an empty slice that the compiler can't infer the size - //let empty_vec = std::env::args() - //.nth(1000000) - //.unwrap_or_default() - //.into_bytes(); - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_rust_with_dict", input_bytes), - //&input, - //|b, i| b.iter(|| lz4_flex::block::compress_with_dict(i, &empty_vec)), - //); - //group.bench_with_input( - //BenchmarkId::new("lz4_redox_rust", input_bytes), - //&input, - //|b, i| b.iter(|| lz4_compress::compress(i)), - //); - //group.bench_with_input( - //BenchmarkId::new("lz4_fear_rust", input_bytes), - //&input, - //|b, i| b.iter(|| compress_lz4_fear(i)), - //); - - group.bench_with_input(BenchmarkId::new("lz4_cpp", input_bytes), &input, |b, i| { - b.iter(|| lz4_cpp_block_compress(i)) - }); - - group.bench_with_input(BenchmarkId::new("snap", input_bytes), &input, |b, i| { - b.iter(|| compress_snap(i)) - }); - } - - group.finish(); -} - -fn bench_block_decompression_throughput(c: &mut Criterion) { - let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Linear); - - let mut group = c.benchmark_group("BlockDecompress"); - group.plot_config(plot_config); - - for input in ALL.iter() { - let input_bytes = input.len() as u64; - - let comp_lz4 = lz4_cpp_block_compress(input).unwrap(); - group.throughput(Throughput::Bytes(input.len() as _)); - - group.bench_with_input( - BenchmarkId::new("lz4_flex_rust", input_bytes), - &comp_lz4, - |b, i| b.iter(|| lz4_flex::decompress(i, input.len())), - ); - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_v10_rust", input_bytes), - //&comp_lz4, - //|b, i| b.iter(|| lz4_flex_master::decompress(i, input.len())), - //); - - // an empty slice that the compiler can't infer the size - //let empty_vec = std::env::args() - //.nth(1000000) - //.unwrap_or_default() - //.into_bytes(); - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_rust_with_dict", input_bytes), - //&comp_lz4, - //|b, i| b.iter(|| lz4_flex::block::decompress_with_dict(i, input.len(), &empty_vec)), - //); - //group.bench_with_input( - //BenchmarkId::new("lz4_redox_rust", input_bytes), - //&comp_lz4, - //|b, i| b.iter(|| lz4_compress::decompress(i)), - //); - //group.bench_with_input( - //BenchmarkId::new("lz4_fear_rust", input_bytes), - //&comp_lz4, - //|b, i| b.iter(|| decompress_lz4_fear(i)), - //); - - group.bench_with_input( - BenchmarkId::new("lz4_cpp", input_bytes), - &comp_lz4, - |b, i| b.iter(|| lz4_cpp_block_decompress(i, input.len())), - ); - - //let comp_snap = compress_snap(input); - //group.bench_with_input(BenchmarkId::new("snap", input_bytes), &comp_snap, |b, i| { - //b.iter(|| decompress_snap(i)) - //}); - } - - group.finish(); -} - -#[cfg(feature = "frame")] -fn bench_frame_decompression_throughput(c: &mut Criterion) { - let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Linear); - - let mut group = c.benchmark_group("FrameDecompress"); - group.plot_config(plot_config); - - let all_frame = get_frame_datasets(); - for input in all_frame.iter() { - let input_bytes = input.len() as u64; - - let comp_lz4_indep = lz4_cpp_frame_compress(input, true).unwrap(); - let comp_lz4_linked = lz4_cpp_frame_compress(input, false).unwrap(); - group.throughput(Throughput::Bytes(input.len() as _)); - - group.bench_with_input( - BenchmarkId::new("lz4_flex_rust_indep", input_bytes), - &comp_lz4_indep, - |b, i| b.iter(|| lz4_flex_frame_decompress(i)), - ); - group.bench_with_input( - BenchmarkId::new("lz4_flex_rust_linked", input_bytes), - &comp_lz4_linked, - |b, i| b.iter(|| lz4_flex_frame_decompress(i)), - ); - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_v10_rust_indep", input_bytes), - //&comp_lz4_indep, - //|b, i| b.iter(|| lz4_flex_master_frame_decompress(&i)), - //); - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_v10_rust_linked", input_bytes), - //&comp_lz4_linked, - //|b, i| b.iter(|| lz4_flex_master_frame_decompress(&i)), - //); - - group.bench_with_input( - BenchmarkId::new("lz4_cpp_indep", input_bytes), - &comp_lz4_indep, - |b, i| b.iter(|| lz4_cpp_frame_decompress(i)), - ); - group.bench_with_input( - BenchmarkId::new("lz4_cpp_linked", input_bytes), - &comp_lz4_linked, - |b, i| b.iter(|| lz4_cpp_frame_decompress(i)), - ); - - let comp_snap = compress_snap_frame(input); - group.bench_with_input(BenchmarkId::new("snap", input_bytes), &comp_snap, |b, i| { - b.iter(|| decompress_snap_frame(i)) - }); - } - - group.finish(); -} - -#[cfg(feature = "frame")] -fn bench_frame_compression_throughput(c: &mut Criterion) { - let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Linear); - - let mut group = c.benchmark_group("FrameCompress"); - group.plot_config(plot_config); - - let all_frame = get_frame_datasets(); - for input in all_frame.iter() { - let input_bytes = input.len() as u64; - group.throughput(Throughput::Bytes(input_bytes)); - - group.bench_with_input( - BenchmarkId::new("lz4_flex_rust_indep", input_bytes), - &input, - |b, i| { - b.iter(|| { - let mut frame_info = lz4_flex::frame::FrameInfo::new(); - frame_info.block_size = lz4_flex::frame::BlockSize::Max64KB; - frame_info.block_mode = lz4_flex::frame::BlockMode::Independent; - lz4_flex_frame_compress_with(frame_info, i) - }) - }, - ); - group.bench_with_input( - BenchmarkId::new("lz4_flex_rust_linked", input_bytes), - &input, - |b, i| { - b.iter(|| { - let mut frame_info = lz4_flex::frame::FrameInfo::new(); - frame_info.block_size = lz4_flex::frame::BlockSize::Max64KB; - frame_info.block_mode = lz4_flex::frame::BlockMode::Linked; - lz4_flex_frame_compress_with(frame_info, i) - }) - }, - ); - - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_v10_rust_indep", input_bytes), - //&input, - //|b, i| { - //b.iter(|| { - //let mut frame_info = lz4_flex_master::frame::FrameInfo::new(); - //frame_info.block_size = lz4_flex_master::frame::BlockSize::Max64KB; - //frame_info.block_mode = lz4_flex_master::frame::BlockMode::Independent; - //lz4_flex_master_frame_compress_with(frame_info, i) - //}) - //}, - //); - //group.bench_with_input( - //BenchmarkId::new("lz4_flex_v10_rust_linked", input_bytes), - //&input, - //|b, i| { - //b.iter(|| { - //let mut frame_info = lz4_flex_master::frame::FrameInfo::new(); - //frame_info.block_size = lz4_flex_master::frame::BlockSize::Max64KB; - //frame_info.block_mode = lz4_flex_master::frame::BlockMode::Linked; - //lz4_flex_master_frame_compress_with(frame_info, i) - //}) - //}, - //); - - group.bench_with_input( - BenchmarkId::new("lz4_cpp_indep", input_bytes), - &input, - |b, i| b.iter(|| lz4_cpp_frame_compress(i, true)), - ); - group.bench_with_input( - BenchmarkId::new("lz4_cpp_linked", input_bytes), - &input, - |b, i| b.iter(|| lz4_cpp_frame_compress(i, false)), - ); - - group.bench_with_input(BenchmarkId::new("snap", input_bytes), &input, |b, i| { - b.iter(|| compress_snap_frame(i)) - }); - } - - group.finish(); -} - -criterion_group!( - block_benches, - bench_block_decompression_throughput, - bench_block_compression_throughput, -); - -#[cfg(feature = "frame")] -criterion_group!( - frame_benches, - bench_frame_decompression_throughput, - bench_frame_compression_throughput, -); - -#[cfg(not(feature = "frame"))] -criterion_main!(block_benches); - -#[cfg(feature = "frame")] -criterion_main!(block_benches, frame_benches);