use preserves2 as preserves; use criterion::{criterion_group, criterion_main, Criterion}; use preserves::BinarySource; use preserves::BytesBinarySource; use preserves::IOBinarySource; use preserves::IOValue; use preserves::IOValueDomainDecode; use preserves::IOValueDomainEncode; use preserves::IOValues; use preserves::PackedWriter; use preserves::Value; use preserves::ValueImpl; use preserves::ValueReader; use preserves::packed::view::View; use preserves::packed::view::ViewStream; use std::fs::File; use std::io::Read; use std::io::Seek; use std::io; use std::sync::Arc; pub fn bench_decoder_bytes(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); c.bench_function("(oo) decode samples.bin via bytes", |b| b.iter_with_large_drop( || IOValue::from_bytes(&bs[..], true).unwrap())); } pub fn bench_decoder_bytes_view(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let bs = Arc::from(bs); c.bench_function("(oo) view-decode samples.bin via bytes", |b| b.iter_with_large_drop( || View::new::(Arc::clone(&bs), None).unwrap())); } pub fn bench_decoder_bytes_view_iovalue(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let bs = Arc::from(bs); c.bench_function("(oo) iovalue-view-decode samples.bin via bytes", |b| b.iter_with_large_drop( || IOValue(View::new::(Arc::clone(&bs), None).unwrap().owned_value()))); } pub fn bench_decoder_file(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); c.bench_function("(oo) decode samples.bin via file", |b| b.iter_with_large_drop(|| { fh.seek(io::SeekFrom::Start(0)).ok(); IOValue::read(&mut IOBinarySource::new(&mut fh).packed(), true).unwrap() })); } pub fn bench_decoder_buffered_file(c: &mut Criterion) { let mut fh = io::BufReader::new(File::open("./tests/samples.bin").unwrap()); c.bench_function("(oo) decode samples.bin via buffered file", |b| b.iter_with_large_drop(|| { fh.seek(io::SeekFrom::Start(0)).ok(); IOValue::read(&mut IOBinarySource::new(&mut fh).packed(), true).unwrap() })); } pub fn bench_encoder(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); let v = IOValue::read(&mut IOBinarySource::new(&mut fh).packed(), true).unwrap(); c.bench_function("(oo) encode samples.bin", |b| b.iter_with_large_drop( || PackedWriter::encode(&v).unwrap())); } pub fn bench_encoder_view(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let bs = Arc::from(bs); let v = IOValue::new(View::new::(Arc::clone(&bs), None).unwrap()); c.bench_function("(oo) view-encode samples.bin", |b| b.iter_with_large_drop( || PackedWriter::encode(&v).unwrap())); } pub fn large_testdata_decoder_with_ann(c: &mut Criterion) { c.bench_function("(oo) decode testdata.bin with annotations", |b| { let mut fh = File::open("benches/testdata.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); b.iter(|| { let src = BytesBinarySource::new(&bs[..]); let mut r = IOValues::new(src.into_packed()).read_annotations(true); while let Some(Ok(_)) = r.next() {} }) }); } pub fn large_testdata_decoder_without_ann(c: &mut Criterion) { c.bench_function("(oo) decode testdata.bin without annotations", |b| { let mut fh = File::open("benches/testdata.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); b.iter(|| { let src = BytesBinarySource::new(&bs[..]); let mut r = IOValues::new(src.into_packed()).read_annotations(false); while let Some(Ok(_)) = r.next() {} }) }); } pub fn large_testdata_encoder(c: &mut Criterion) { c.bench_function("(oo) encode testdata.bin", |b| { let mut fh = io::BufReader::new(File::open("benches/testdata.bin").unwrap()); let src = IOBinarySource::new(&mut fh); let vs = IOValues::new(src.into_packed()).read_annotations(true).collect::, _>>().unwrap(); b.iter_with_large_drop(|| { let mut bs = vec![]; let mut w = PackedWriter::new(&mut bs); let mut enc = IOValueDomainEncode; for v in &vs { v.write_domain(&mut w, &mut enc).unwrap(); } bs }) }); } pub fn large_testdata_decoder_view(c: &mut Criterion) { c.bench_function("(oo) view-decode testdata.bin", |b| { let mut fh = File::open("benches/testdata.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let bs = Arc::from(bs); b.iter(|| { let mut r = ViewStream::::new(Arc::clone(&bs), None); while let Some(Ok(_)) = r.next() {} }) }); } pub fn large_testdata_decoder_view_clone(c: &mut Criterion) { c.bench_function("(oo) view-decode testdata.bin with cloning", |b| { let mut fh = File::open("benches/testdata.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let bs = Arc::from(bs); b.iter(|| { let mut r = ViewStream::::new(Arc::clone(&bs), None); while let Some(Ok(v)) = r.next() { v.owned_value(); } }) }); } pub fn large_testdata_decoder_view_deepcopy(c: &mut Criterion) { c.bench_function("(oo) view-decode testdata.bin with deep copy", |b| { let mut fh = File::open("benches/testdata.bin").unwrap(); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let bs = Arc::from(bs); b.iter(|| { let mut r = ViewStream::::new(Arc::clone(&bs), None); while let Some(Ok(v)) = r.next() { preserves::value_deepcopy(&Value::new(v)); } }) }); } pub fn large_testdata_encoder_view(c: &mut Criterion) { c.bench_function("(oo) view-encode testdata.bin", |b| { let mut fh = io::BufReader::new(File::open("benches/testdata.bin").unwrap()); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let vs = ViewStream::::new(Arc::from(bs), None).collect::, _>>().unwrap(); b.iter_with_large_drop(|| { let mut bs = vec![]; let mut w = PackedWriter::new(&mut bs); let mut enc = IOValueDomainEncode; for v in &vs { v.write_domain(&mut w, &mut enc).unwrap(); } bs }) }); } pub fn large_testdata_encoder_view_clone(c: &mut Criterion) { c.bench_function("(oo) view-encode testdata.bin with cloning", |b| { let mut fh = io::BufReader::new(File::open("benches/testdata.bin").unwrap()); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let vs = ViewStream::::new(Arc::from(bs), None).map(|v| v.map(|v| v.owned_value())) .collect::, _>>().unwrap(); b.iter_with_large_drop(|| { let mut bs = vec![]; let mut w = PackedWriter::new(&mut bs); let mut enc = IOValueDomainEncode; for v in &vs { v.write_domain(&mut w, &mut enc).unwrap(); } bs }) }); } pub fn large_testdata_encoder_view_deepcopy(c: &mut Criterion) { c.bench_function("(oo) view-encode testdata.bin with deep copy", |b| { let mut fh = io::BufReader::new(File::open("benches/testdata.bin").unwrap()); let mut bs = vec![]; fh.read_to_end(&mut bs).ok(); let vs = ViewStream::::new(Arc::from(bs), None).map(|v| v.map(|v| preserves::value_deepcopy(&Value::new(v)))) .collect::, _>>().unwrap(); b.iter_with_large_drop(|| { let mut bs = vec![]; let mut w = PackedWriter::new(&mut bs); let mut enc = IOValueDomainEncode; for v in &vs { v.write_domain(&mut w, &mut enc).unwrap(); } bs }) }); } criterion_group!(codec, bench_decoder_bytes, bench_decoder_bytes_view, bench_decoder_bytes_view_iovalue, bench_decoder_file, bench_decoder_buffered_file, bench_encoder, bench_encoder_view); criterion_group!{ name = large_testdata; config = Criterion::default().sample_size(10); targets = large_testdata_decoder_with_ann, large_testdata_decoder_without_ann, large_testdata_encoder, large_testdata_decoder_view, large_testdata_decoder_view_clone, large_testdata_decoder_view_deepcopy, large_testdata_encoder_view, large_testdata_encoder_view_clone, large_testdata_encoder_view_deepcopy } criterion_main!(codec, large_testdata);