Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of Criterion bench groups #590

Merged
merged 8 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions media/benches/audio_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::measurement::WallTime;
use criterion::{black_box, criterion_main, BenchmarkGroup, Criterion};
use webrtc_media::audio::buffer::layout::{Deinterleaved, Interleaved};
use webrtc_media::audio::buffer::Buffer;

fn benchmark_from(c: &mut Criterion) {
fn benchmark_from(g: &mut BenchmarkGroup<WallTime>) {
type Sample = i32;
let channels = 4;
let frames = 100_000;
Expand All @@ -15,15 +16,15 @@ fn benchmark_from(c: &mut Criterion) {
Buffer::new(samples, channels)
};

c.bench_function("Buffer<T, Interleaved> => Buffer<T, Deinterleaved>", |b| {
g.bench_function("Buffer/Interleaved to Deinterleaved", |b| {
b.iter(|| {
black_box(Buffer::<Sample, Interleaved>::from(
deinterleaved_buffer.as_ref(),
));
})
});

c.bench_function("Buffer<T, Deinterleaved> => Buffer<T, Interleaved>", |b| {
g.bench_function("Buffer/Deinterleaved to Interleaved", |b| {
b.iter(|| {
black_box(Buffer::<Sample, Deinterleaved>::from(
interleaved_buffer.as_ref(),
Expand All @@ -32,5 +33,13 @@ fn benchmark_from(c: &mut Criterion) {
});
}

criterion_group!(benches, benchmark_from);
fn benches() {
let mut c = Criterion::default().configure_from_args();
let mut g = c.benchmark_group("Media");

benchmark_from(&mut g);

g.finish();
}

criterion_main!(benches);
24 changes: 18 additions & 6 deletions rtp/benches/packet_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#![allow(clippy::needless_update)]

use bytes::{Bytes, BytesMut};
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::measurement::WallTime;
use criterion::{criterion_main, BenchmarkGroup, Criterion};
use rtp::header::*;
use rtp::packet::*;
use util::marshal::{Marshal, MarshalSize, Unmarshal};

fn benchmark_packet(c: &mut Criterion) {
fn benchmark_packet(g: &mut BenchmarkGroup<WallTime>) {
let pkt = Packet {
header: Header {
extension: true,
Expand Down Expand Up @@ -38,25 +39,36 @@ fn benchmark_packet(c: &mut Criterion) {
///////////////////////////////////////////////////////////////////////////////////////////////
let mut buf = BytesMut::with_capacity(pkt.marshal_size());
buf.resize(pkt.marshal_size(), 0);
c.bench_function("Benchmark MarshalTo", |b| {
// BenchmarkMarshalTo
g.bench_function("marshal_to", |b| {
b.iter(|| {
let _ = pkt.marshal_to(&mut buf).unwrap();
})
});

c.bench_function("Benchmark Marshal", |b| {
// BenchmarkMarshal
g.bench_function("marshal", |b| {
b.iter(|| {
let _ = pkt.marshal().unwrap();
})
});

c.bench_function("Benchmark Unmarshal ", |b| {
// BenchmarkUnmarshal
g.bench_function("unmarshal", |b| {
b.iter(|| {
let buf = &mut raw.clone();
let _ = Packet::unmarshal(buf).unwrap();
})
});
}

criterion_group!(benches, benchmark_packet);
fn benches() {
let mut c = Criterion::default().configure_from_args();
let mut g = c.benchmark_group("RTP");

benchmark_packet(&mut g);

g.finish();
}

criterion_main!(benches);
22 changes: 17 additions & 5 deletions sdp/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Cursor;

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::measurement::WallTime;
use criterion::{criterion_main, BenchmarkGroup, Criterion};
use sdp::SessionDescription;

const CANONICAL_UNMARSHAL_SDP: &str = "v=0\r\n\
Expand Down Expand Up @@ -29,24 +30,35 @@ a=sendrecv\r\n\
m=video 51372 RTP/AVP 99\r\n\
a=rtpmap:99 h263-1998/90000\r\n";

fn benchmark_sdp(c: &mut Criterion) {
fn benchmark_sdp(g: &mut BenchmarkGroup<WallTime>) {
let mut reader = Cursor::new(CANONICAL_UNMARSHAL_SDP.as_bytes());
let sdp = SessionDescription::unmarshal(&mut reader).unwrap();

///////////////////////////////////////////////////////////////////////////////////////////////
c.bench_function("Benchmark Marshal", |b| {

// BenchmarkMarshal
g.bench_function("Marshal", |b| {
b.iter(|| {
let _ = sdp.marshal();
})
});

c.bench_function("Benchmark Unmarshal ", |b| {
// BenchmarkUnmarshal
g.bench_function("Unmarshal", |b| {
b.iter(|| {
let mut reader = Cursor::new(CANONICAL_UNMARSHAL_SDP.as_bytes());
let _ = SessionDescription::unmarshal(&mut reader).unwrap();
})
});
}

criterion_group!(benches, benchmark_sdp);
fn benches() {
let mut c = Criterion::default().configure_from_args();
let mut g = c.benchmark_group("SDP");

benchmark_sdp(&mut g);

g.finish();
}

criterion_main!(benches);
38 changes: 22 additions & 16 deletions srtp/benches/srtp_bench.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bytes::BytesMut;
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::measurement::WallTime;
use criterion::{criterion_main, BenchmarkGroup, Criterion};
use util::Marshal;
use webrtc_srtp::{context::Context, protection_profile::ProtectionProfile};

Expand All @@ -12,7 +13,7 @@ const RAW_RTCP: &[u8] = &[
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
];

fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
let mut ctx = Context::new(
MASTER_KEY,
MASTER_SALT,
Expand All @@ -27,7 +28,7 @@ fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
pld.extend_from_slice(&[i as u8]);
}

c.bench_function("Benchmark RTP encrypt", |b| {
g.bench_function("Encrypt/RTP", |b| {
let mut seq = 1;
b.iter_batched(
|| {
Expand Down Expand Up @@ -55,7 +56,7 @@ fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
});
}

fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
let mut setup_ctx = Context::new(
MASTER_KEY,
MASTER_SALT,
Expand All @@ -79,7 +80,7 @@ fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
pld.extend_from_slice(&[i as u8]);
}

c.bench_function("Benchmark RTP decrypt", |b| {
g.bench_function("Decrypt/RTP", |b| {
let mut seq = 1;
b.iter_batched(
|| {
Expand All @@ -105,7 +106,7 @@ fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
});
}

fn benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
fn benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
let mut ctx = Context::new(
MASTER_KEY,
MASTER_SALT,
Expand All @@ -115,14 +116,14 @@ fn benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
)
.unwrap();

c.bench_function("Benchmark RTCP encrypt", |b| {
g.bench_function("Encrypt/RTCP", |b| {
b.iter(|| {
ctx.encrypt_rtcp(RAW_RTCP).unwrap();
});
});
}

fn benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
fn benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
let encrypted = Context::new(
MASTER_KEY,
MASTER_SALT,
Expand All @@ -143,16 +144,21 @@ fn benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
)
.unwrap();

c.bench_function("Benchmark RTCP decrypt", |b| {
g.bench_function("Decrypt/RTCP", |b| {
b.iter(|| ctx.decrypt_rtcp(&encrypted).unwrap());
});
}

criterion_group!(
benches,
benchmark_encrypt_rtp_aes_128_cm_hmac_sha1,
benchmark_decrypt_rtp_aes_128_cm_hmac_sha1,
benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1,
benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1
);
fn benches() {
let mut c = Criterion::default().configure_from_args();
let mut g = c.benchmark_group("SRTP");

benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(&mut g);
benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(&mut g);
benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(&mut g);
benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(&mut g);

g.finish();
}

criterion_main!(benches);
Loading
Loading