Skip to content

Commit

Permalink
Add brief to benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
FlixCoder committed Sep 25, 2024
1 parent 30fdb11 commit 128e2fb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use rust_serialization_benchmark::bench_bincode1;
use rust_serialization_benchmark::bench_bitcode;
#[cfg(feature = "borsh")]
use rust_serialization_benchmark::bench_borsh;
#[cfg(feature = "brief")]
use rust_serialization_benchmark::bench_brief;
#[cfg(feature = "capnp")]
use rust_serialization_benchmark::bench_capnp;
#[cfg(feature = "cbor4ii")]
Expand Down Expand Up @@ -91,6 +93,9 @@ fn bench_log(c: &mut Criterion) {
#[cfg(feature = "borsh")]
bench_borsh::bench(BENCH, c, &data);

#[cfg(feature = "brief")]
bench_brief::bench(BENCH, c, &data);

#[cfg(feature = "capnp")]
bench_capnp::bench(BENCH, c, &data, |bytes| {
let message_reader =
Expand Down Expand Up @@ -268,6 +273,9 @@ fn bench_mesh(c: &mut Criterion) {
#[cfg(feature = "borsh")]
bench_borsh::bench(BENCH, c, &data);

#[cfg(feature = "brief")]
bench_brief::bench(BENCH, c, &data);

#[cfg(feature = "capnp")]
bench_capnp::bench(BENCH, c, &data, |bytes| {
let message_reader =
Expand Down Expand Up @@ -432,6 +440,9 @@ fn bench_minecraft_savedata(c: &mut Criterion) {
#[cfg(feature = "borsh")]
bench_borsh::bench(BENCH, c, &data);

#[cfg(feature = "brief")]
bench_brief::bench(BENCH, c, &data);

#[cfg(feature = "capnp")]
bench_capnp::bench(BENCH, c, &data, |bytes| {
let message_reader =
Expand Down Expand Up @@ -599,6 +610,9 @@ fn bench_mk48(c: &mut Criterion) {
#[cfg(feature = "borsh")]
bench_borsh::bench(BENCH, c, &data);

#[cfg(feature = "brief")]
bench_brief::bench(BENCH, c, &data);

#[cfg(feature = "capnp")]
bench_capnp::bench(BENCH, c, &data, |bytes| {
let message_reader =
Expand Down
35 changes: 35 additions & 0 deletions src/bench_brief.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use criterion::{black_box, Criterion};
use serde::{Deserialize, Serialize};

pub fn bench<T>(name: &'static str, c: &mut Criterion, data: &T)
where
T: Serialize + for<'de> Deserialize<'de> + PartialEq,
{
const BUFFER_LEN: usize = 50_000_000;

let mut group = c.benchmark_group(format!("{}/brief", name));

let mut serialize_buffer = vec![0; BUFFER_LEN];
group.bench_function("serialize", |b| {
b.iter(|| {
black_box(
brief::to_slice(black_box(&data), black_box(serialize_buffer.as_mut_slice()))
.unwrap(),
);
})
});

let deserialize_buffer = brief::to_vec(&data).unwrap();

group.bench_function("deserialize", |b| {
b.iter(|| {
black_box(brief::from_slice::<'_, T>(black_box(&deserialize_buffer)).unwrap());
})
});

crate::bench_size(name, "brief", deserialize_buffer.as_slice());

assert!(brief::from_slice::<T>(&deserialize_buffer).unwrap() == *data);

group.finish();
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub mod bench_bincode1;
pub mod bench_bitcode;
#[cfg(feature = "borsh")]
pub mod bench_borsh;
#[cfg(feature = "brief")]
pub mod bench_brief;
#[cfg(feature = "capnp")]
pub mod bench_capnp;
#[cfg(feature = "cbor4ii")]
Expand Down

0 comments on commit 128e2fb

Please sign in to comment.