diff --git a/implementations/rust/oo/Cargo.toml b/implementations/rust/oo/Cargo.toml index 82ce8ce..f79194e 100644 --- a/implementations/rust/oo/Cargo.toml +++ b/implementations/rust/oo/Cargo.toml @@ -14,5 +14,12 @@ num-bigint = "0.4" num-traits = "0.2" regex = "1.5" +[dev-dependencies] +criterion = "0.3" + +[[bench]] +name = "codec" +harness = false + [package.metadata.workspaces] independent = true diff --git a/implementations/rust/oo/benches/.gitignore b/implementations/rust/oo/benches/.gitignore new file mode 100644 index 0000000..800aca2 --- /dev/null +++ b/implementations/rust/oo/benches/.gitignore @@ -0,0 +1 @@ +testdata.bin diff --git a/implementations/rust/oo/benches/codec.rs b/implementations/rust/oo/benches/codec.rs new file mode 100644 index 0000000..f1055ae --- /dev/null +++ b/implementations/rust/oo/benches/codec.rs @@ -0,0 +1,98 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use oo::BinarySource; +use oo::BytesBinarySource; +use oo::IOBinarySource; +use oo::IOValueDomainCodec; +use oo::IOValues; +use oo::PackedWriter; +use oo::Reader; +use oo::Value; +use oo::packed::annotated_iovalue_from_bytes; +use std::fs::File; +use std::io::Read; +use std::io::Seek; +use std::io; + +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( + || annotated_iovalue_from_bytes(&bs[..]).unwrap())); +} + +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(); + IOBinarySource::new(&mut fh).packed().next_iovalue(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(); + IOBinarySource::new(&mut fh).packed().next_iovalue(true).unwrap() + })); +} + +pub fn bench_encoder(c: &mut Criterion) { + let mut fh = File::open("../../../tests/samples.bin").unwrap(); + let v = IOBinarySource::new(&mut fh).packed().next_iovalue(true).unwrap(); + c.bench_function("(oo) encode samples.bin", |b| b.iter_with_large_drop( + || PackedWriter::encode_iovalue(&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 mut src = BytesBinarySource::new(&bs[..]); + let mut r = IOValues::new(src.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 mut src = BytesBinarySource::new(&bs[..]); + let mut r = IOValues::new(src.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 mut src = IOBinarySource::new(&mut fh); + let vs = IOValues::new(src.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 = IOValueDomainCodec; + for v in &vs { + v.write(&mut w, &mut enc).unwrap(); + } + bs + }) + }); +} + +criterion_group!(codec, + bench_decoder_bytes, bench_decoder_file, bench_decoder_buffered_file, + bench_encoder); +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 +} +criterion_main!(codec, large_testdata); diff --git a/implementations/rust/oo/benches/testdata.bin.gz b/implementations/rust/oo/benches/testdata.bin.gz new file mode 100644 index 0000000..469262f Binary files /dev/null and b/implementations/rust/oo/benches/testdata.bin.gz differ diff --git a/implementations/rust/oo/src/lib.rs b/implementations/rust/oo/src/lib.rs index 71a30ff..839caed 100644 --- a/implementations/rust/oo/src/lib.rs +++ b/implementations/rust/oo/src/lib.rs @@ -19,6 +19,7 @@ pub use packed::annotated_from_bytes; pub use packed::annotated_iovalue_from_bytes; pub use packed::from_bytes; pub use packed::iovalue_from_bytes; +pub use reader::IOValues; pub use reader::Reader; pub use repr::Annotations; pub use repr::ArcValue;