Skip to content

Commit 19004ef

Browse files
authored
Make use of Criterion bench groups (#590)
* turn: Use Criterion group * stun: Use Criterion group * sdp: Use Criterion group * rtp: Use Criterion group * util: Use Criterion group * media: Use Criterion group * srtp: Use Criterion group * fmt
1 parent 2c96756 commit 19004ef

File tree

7 files changed

+232
-126
lines changed

7 files changed

+232
-126
lines changed

media/benches/audio_buffer.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1+
use criterion::measurement::WallTime;
2+
use criterion::{black_box, criterion_main, BenchmarkGroup, Criterion};
23
use webrtc_media::audio::buffer::layout::{Deinterleaved, Interleaved};
34
use webrtc_media::audio::buffer::Buffer;
45

5-
fn benchmark_from(c: &mut Criterion) {
6+
fn benchmark_from(g: &mut BenchmarkGroup<WallTime>) {
67
type Sample = i32;
78
let channels = 4;
89
let frames = 100_000;
@@ -15,15 +16,15 @@ fn benchmark_from(c: &mut Criterion) {
1516
Buffer::new(samples, channels)
1617
};
1718

18-
c.bench_function("Buffer<T, Interleaved> => Buffer<T, Deinterleaved>", |b| {
19+
g.bench_function("Buffer/Interleaved to Deinterleaved", |b| {
1920
b.iter(|| {
2021
black_box(Buffer::<Sample, Interleaved>::from(
2122
deinterleaved_buffer.as_ref(),
2223
));
2324
})
2425
});
2526

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

35-
criterion_group!(benches, benchmark_from);
36+
fn benches() {
37+
let mut c = Criterion::default().configure_from_args();
38+
let mut g = c.benchmark_group("Media");
39+
40+
benchmark_from(&mut g);
41+
42+
g.finish();
43+
}
44+
3645
criterion_main!(benches);

rtp/benches/packet_bench.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
#![allow(clippy::needless_update)]
33

44
use bytes::{Bytes, BytesMut};
5-
use criterion::{criterion_group, criterion_main, Criterion};
5+
use criterion::measurement::WallTime;
6+
use criterion::{criterion_main, BenchmarkGroup, Criterion};
67
use rtp::header::*;
78
use rtp::packet::*;
89
use util::marshal::{Marshal, MarshalSize, Unmarshal};
910

10-
fn benchmark_packet(c: &mut Criterion) {
11+
fn benchmark_packet(g: &mut BenchmarkGroup<WallTime>) {
1112
let pkt = Packet {
1213
header: Header {
1314
extension: true,
@@ -38,25 +39,36 @@ fn benchmark_packet(c: &mut Criterion) {
3839
///////////////////////////////////////////////////////////////////////////////////////////////
3940
let mut buf = BytesMut::with_capacity(pkt.marshal_size());
4041
buf.resize(pkt.marshal_size(), 0);
41-
c.bench_function("Benchmark MarshalTo", |b| {
42+
// BenchmarkMarshalTo
43+
g.bench_function("marshal_to", |b| {
4244
b.iter(|| {
4345
let _ = pkt.marshal_to(&mut buf).unwrap();
4446
})
4547
});
4648

47-
c.bench_function("Benchmark Marshal", |b| {
49+
// BenchmarkMarshal
50+
g.bench_function("marshal", |b| {
4851
b.iter(|| {
4952
let _ = pkt.marshal().unwrap();
5053
})
5154
});
5255

53-
c.bench_function("Benchmark Unmarshal ", |b| {
56+
// BenchmarkUnmarshal
57+
g.bench_function("unmarshal", |b| {
5458
b.iter(|| {
5559
let buf = &mut raw.clone();
5660
let _ = Packet::unmarshal(buf).unwrap();
5761
})
5862
});
5963
}
6064

61-
criterion_group!(benches, benchmark_packet);
65+
fn benches() {
66+
let mut c = Criterion::default().configure_from_args();
67+
let mut g = c.benchmark_group("RTP");
68+
69+
benchmark_packet(&mut g);
70+
71+
g.finish();
72+
}
73+
6274
criterion_main!(benches);

sdp/benches/bench.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::io::Cursor;
22

3-
use criterion::{criterion_group, criterion_main, Criterion};
3+
use criterion::measurement::WallTime;
4+
use criterion::{criterion_main, BenchmarkGroup, Criterion};
45
use sdp::SessionDescription;
56

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

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

3637
///////////////////////////////////////////////////////////////////////////////////////////////
37-
c.bench_function("Benchmark Marshal", |b| {
38+
39+
// BenchmarkMarshal
40+
g.bench_function("Marshal", |b| {
3841
b.iter(|| {
3942
let _ = sdp.marshal();
4043
})
4144
});
4245

43-
c.bench_function("Benchmark Unmarshal ", |b| {
46+
// BenchmarkUnmarshal
47+
g.bench_function("Unmarshal", |b| {
4448
b.iter(|| {
4549
let mut reader = Cursor::new(CANONICAL_UNMARSHAL_SDP.as_bytes());
4650
let _ = SessionDescription::unmarshal(&mut reader).unwrap();
4751
})
4852
});
4953
}
5054

51-
criterion_group!(benches, benchmark_sdp);
55+
fn benches() {
56+
let mut c = Criterion::default().configure_from_args();
57+
let mut g = c.benchmark_group("SDP");
58+
59+
benchmark_sdp(&mut g);
60+
61+
g.finish();
62+
}
63+
5264
criterion_main!(benches);

srtp/benches/srtp_bench.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bytes::BytesMut;
2-
use criterion::{criterion_group, criterion_main, Criterion};
2+
use criterion::measurement::WallTime;
3+
use criterion::{criterion_main, BenchmarkGroup, Criterion};
34
use util::Marshal;
45
use webrtc_srtp::{context::Context, protection_profile::ProtectionProfile};
56

@@ -12,7 +13,7 @@ const RAW_RTCP: &[u8] = &[
1213
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1314
];
1415

15-
fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
16+
fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
1617
let mut ctx = Context::new(
1718
MASTER_KEY,
1819
MASTER_SALT,
@@ -27,7 +28,7 @@ fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
2728
pld.extend_from_slice(&[i as u8]);
2829
}
2930

30-
c.bench_function("Benchmark RTP encrypt", |b| {
31+
g.bench_function("Encrypt/RTP", |b| {
3132
let mut seq = 1;
3233
b.iter_batched(
3334
|| {
@@ -55,7 +56,7 @@ fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
5556
});
5657
}
5758

58-
fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
59+
fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
5960
let mut setup_ctx = Context::new(
6061
MASTER_KEY,
6162
MASTER_SALT,
@@ -79,7 +80,7 @@ fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
7980
pld.extend_from_slice(&[i as u8]);
8081
}
8182

82-
c.bench_function("Benchmark RTP decrypt", |b| {
83+
g.bench_function("Decrypt/RTP", |b| {
8384
let mut seq = 1;
8485
b.iter_batched(
8586
|| {
@@ -105,7 +106,7 @@ fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
105106
});
106107
}
107108

108-
fn benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
109+
fn benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
109110
let mut ctx = Context::new(
110111
MASTER_KEY,
111112
MASTER_SALT,
@@ -115,14 +116,14 @@ fn benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
115116
)
116117
.unwrap();
117118

118-
c.bench_function("Benchmark RTCP encrypt", |b| {
119+
g.bench_function("Encrypt/RTCP", |b| {
119120
b.iter(|| {
120121
ctx.encrypt_rtcp(RAW_RTCP).unwrap();
121122
});
122123
});
123124
}
124125

125-
fn benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
126+
fn benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(g: &mut BenchmarkGroup<WallTime>) {
126127
let encrypted = Context::new(
127128
MASTER_KEY,
128129
MASTER_SALT,
@@ -143,16 +144,21 @@ fn benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
143144
)
144145
.unwrap();
145146

146-
c.bench_function("Benchmark RTCP decrypt", |b| {
147+
g.bench_function("Decrypt/RTCP", |b| {
147148
b.iter(|| ctx.decrypt_rtcp(&encrypted).unwrap());
148149
});
149150
}
150151

151-
criterion_group!(
152-
benches,
153-
benchmark_encrypt_rtp_aes_128_cm_hmac_sha1,
154-
benchmark_decrypt_rtp_aes_128_cm_hmac_sha1,
155-
benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1,
156-
benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1
157-
);
152+
fn benches() {
153+
let mut c = Criterion::default().configure_from_args();
154+
let mut g = c.benchmark_group("SRTP");
155+
156+
benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(&mut g);
157+
benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(&mut g);
158+
benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(&mut g);
159+
benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(&mut g);
160+
161+
g.finish();
162+
}
163+
158164
criterion_main!(benches);

0 commit comments

Comments
 (0)