Skip to content

Commit

Permalink
Add some benchmarks for writing some RTP packets
Browse files Browse the repository at this point in the history
  • Loading branch information
ystreet committed Aug 15, 2024
1 parent f8d143c commit 71be683
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ criterion = "0.5"
[[bench]]
name = "parse"
harness = false

[[bench]]
name = "write"
harness = false
110 changes: 110 additions & 0 deletions benches/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use rtp_types::RtpPacketBuilder;

fn default_builder<'a>() -> RtpPacketBuilder<&'a [u8], &'a [u8]> {
RtpPacketBuilder::new()
.payload_type(96)
.ssrc(0x12345678)
.sequence_number(0x100)
.timestamp(0x200)
}

fn write_into_vec<'a>(builder: &RtpPacketBuilder<&'a [u8], &'a [u8]>, data: &mut Vec<u8>) {
builder.write_into_vec(data).unwrap();
}

fn write_into_slice<'a>(builder: &RtpPacketBuilder<&'a [u8], &'a [u8]>, slice: &mut [u8]) {
builder.write_into(slice).unwrap();
}

fn write_vec<'a>(builder: &RtpPacketBuilder<&'a [u8], &'a [u8]>) {
let _data = builder.write_vec().unwrap();
}

fn bench_parse(c: &mut Criterion) {
let payload16 = [1; 100];
let payload256 = [2; 256];
let payload1280 = [3; 1280];
let payload65280 = [4; 65280];

let builders = [
("Header", default_builder()),
("CSRC", default_builder().add_csrc(0x081726354)),
(
"CSRC15",
default_builder()
.add_csrc(0x0)
.add_csrc(0x1)
.add_csrc(0x2)
.add_csrc(0x3)
.add_csrc(0x4)
.add_csrc(0x5)
.add_csrc(0x6)
.add_csrc(0x7)
.add_csrc(0x8)
.add_csrc(0x9)
.add_csrc(0xa)
.add_csrc(0xb)
.add_csrc(0xc)
.add_csrc(0xd)
.add_csrc(0xe),
),
(
"Extension",
default_builder().extension(0x2345, payload16.as_slice()),
),
("Payload16", default_builder().payload(payload16.as_slice())),
(
"Payload256",
default_builder().payload(payload256.as_slice()),
),
(
"Payload1280",
default_builder().payload(payload1280.as_slice()),
),
(
"Payload65280",
default_builder().payload(payload65280.as_slice()),
),
];

let mut group = c.benchmark_group("Packet/Build");
for (name, builder) in builders {
let slice_mut = vec![0; 1 << 16];
let empty_vec = vec![];
group.throughput(criterion::Throughput::Bytes(builder.calculate_size().unwrap() as u64));
group.bench_with_input(
BenchmarkId::from_parameter(format!("{name}/WriteIntoVec")),
&builder,
|b, builder| {
b.iter_batched_ref(
|| empty_vec.clone(),
|data| write_into_vec(builder, data),
criterion::BatchSize::LargeInput,
)
},
);
group.bench_with_input(
BenchmarkId::from_parameter(format!("{name}/WriteIntoSlice")),
&&builder,
|b, builder| {
b.iter_batched_ref(
|| slice_mut.clone(),
|slice| write_into_slice(builder, slice),
criterion::BatchSize::LargeInput,
)
},
);
group.bench_with_input(
BenchmarkId::from_parameter(format!("{name}/WriteVec")),
&builder,
|b, builder| b.iter(|| write_vec(builder)),
);
}
group.finish();
}

criterion_group!(packet_parse, bench_parse);
criterion_main!(packet_parse);

0 comments on commit 71be683

Please sign in to comment.