Skip to content

Commit

Permalink
bench/proto: add datagram benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
BiagioFesta committed Dec 29, 2023
1 parent fd1d297 commit 785be7c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions wtransport-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ readme = "../README.md"
workspace = ".."
rust-version = "1.70"

[[bench]]
name = "datagram"
harness = false

[[bench]]
name = "bytes"
harness = false
Expand Down
42 changes: 42 additions & 0 deletions wtransport-proto/benches/datagram.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BatchSize;
use criterion::Criterion;
use std::hint::black_box;
use wtransport_proto::datagram::Datagram;
use wtransport_proto::ids::QStreamId;

const QSTREAM_ID: QStreamId = QStreamId::MAX;
const PAYLOAD: &[u8] = b"Payload";

fn bench_datagram(c: &mut Criterion) {
c.bench_function("read", |b| {
b.iter_batched_ref(
|| {
let dgram = Datagram::new(QSTREAM_ID, PAYLOAD);
let mut buffer = vec![0; dgram.write_size()];
dgram.write(&mut buffer).unwrap();
buffer
},
|buffer| {
let _ = black_box(Datagram::read(buffer));
},
BatchSize::SmallInput,
)
});

c.bench_function("write", |b| {
b.iter_batched_ref(
|| {
let dgram = Datagram::new(QSTREAM_ID, PAYLOAD);
let buffer = vec![0; dgram.write_size()];
(dgram, buffer)
},
|(dgram, buffer)| dgram.write(buffer),
BatchSize::SmallInput,
)
});
}

criterion_group!(datagram, bench_datagram);
criterion_main!(datagram);

0 comments on commit 785be7c

Please sign in to comment.