use preserves2 as preserves; use criterion::{criterion_group, criterion_main, Criterion}; use preserves::IOValue; use preserves::IOValueDomainDecode; use preserves::PackedWriter; use preserves::Value; use preserves::ValueReader; use preserves::packed::view::View; use std::fs::File; use std::io::Read; use std::sync::Arc; #[path = "../tests/samples/mod.rs"] mod samples; use samples::TestCases; pub fn bench_de(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("deserialize samples.bin", |b| b.iter_with_large_drop( || preserves::serde::from_bytes::(&bs[..]).unwrap())); } pub fn bench_ser(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); let v: TestCases = preserves::serde::from_read(&mut fh).unwrap(); c.bench_function("serialize samples.bin", |b| b.iter_with_large_drop(|| { let mut bs = vec![]; preserves::serde::to_writer(&mut PackedWriter::new(&mut bs), &v).unwrap(); bs })); } pub fn bench_decoder_de(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("decode-then-deserialize samples.bin", |b| b.iter_with_large_drop( || preserves::serde::from_value::( &IOValue::iovalue_from_bytes(&bs[..]).unwrap()).unwrap())); } pub fn bench_ser_encoder(c: &mut Criterion) { let mut fh = File::open("./tests/samples.bin").unwrap(); let v: TestCases = preserves::serde::from_read(&mut fh).unwrap(); c.bench_function("serialize-then-encode samples.bin", |b| b.iter_with_large_drop( || PackedWriter::encode(&preserves::serde::to_value(&v)).unwrap())); } pub fn bench_view_de(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("view-decode-then-deserialize samples.bin", |b| b.iter_with_large_drop( || preserves::serde::from_value::( &IOValue(Value::new(View::new::(Arc::clone(&bs), None).unwrap()))))); } criterion_group!(serde, bench_de, bench_ser); criterion_group!(codec_then_serde, bench_decoder_de, bench_ser_encoder, bench_view_de); criterion_main!(serde, codec_then_serde);