Skip to content

Commit 3f1b868

Browse files
logist322rainliu
authored andcommitted
Add benches
1 parent 9143232 commit 3f1b868

File tree

2 files changed

+80
-17
lines changed

2 files changed

+80
-17
lines changed

srtp/benches/srtp_bench.rs

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
use bytes::BytesMut;
1+
use bytes::{Bytes, BytesMut};
22
use criterion::{criterion_group, criterion_main, Criterion};
33
use util::Marshal;
44
use webrtc_srtp::{context::Context, protection_profile::ProtectionProfile};
55

6+
const RAW_RTCP: Bytes = Bytes::from_static(&[
7+
0x81, 0xc8, 0x00, 0x0b, 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
8+
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
9+
]);
10+
611
fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
712
let mut ctx = Context::new(
813
&vec![
@@ -20,7 +25,7 @@ fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
2025
pld.extend_from_slice(&[i as u8]);
2126
}
2227

23-
c.bench_function("Benchmark encrypt", |b| {
28+
c.bench_function("Benchmark RTP encrypt", |b| {
2429
let mut seq = 1;
2530
b.iter_batched(
2631
|| {
@@ -58,7 +63,7 @@ fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
5863
None,
5964
None,
6065
)
61-
.unwrap();
66+
.unwrap();
6267

6368
let mut ctx = Context::new(
6469
&vec![
@@ -69,14 +74,14 @@ fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
6974
None,
7075
None,
7176
)
72-
.unwrap();
77+
.unwrap();
7378

7479
let mut pld = BytesMut::new();
7580
for i in 0..1200 {
7681
pld.extend_from_slice(&[i as u8]);
7782
}
7883

79-
c.bench_function("Benchmark decrypt", |b| {
84+
c.bench_function("Benchmark RTP decrypt", |b| {
8085
let mut seq = 1;
8186
b.iter_batched(
8287
|| {
@@ -96,15 +101,67 @@ fn benchmark_decrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
96101
seq += 1;
97102
setup_ctx.encrypt_rtp(&pkt.marshal().unwrap()).unwrap()
98103
},
99-
|encrypted| {
100-
ctx.decrypt_rtp(&encrypted).unwrap()
101-
},
104+
|encrypted| ctx.decrypt_rtp(&encrypted).unwrap(),
102105
criterion::BatchSize::LargeInput,
103106
);
104107
});
105108
}
106109

110+
fn benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
111+
let mut ctx = Context::new(
112+
&vec![
113+
96, 180, 31, 4, 119, 137, 128, 252, 75, 194, 252, 44, 63, 56, 61, 55,
114+
],
115+
&vec![247, 26, 49, 94, 99, 29, 79, 94, 5, 111, 252, 216, 62, 195],
116+
ProtectionProfile::Aes128CmHmacSha1_80,
117+
None,
118+
None,
119+
)
120+
.unwrap();
121+
122+
c.bench_function("Benchmark RTCP encrypt", |b| {
123+
b.iter(|| {
124+
ctx.encrypt_rtcp(&RAW_RTCP)
125+
.unwrap();
126+
});
127+
});
128+
}
129+
130+
fn benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
131+
let encrypted = Context::new(
132+
&vec![
133+
96, 180, 31, 4, 119, 137, 128, 252, 75, 194, 252, 44, 63, 56, 61, 55,
134+
],
135+
&vec![247, 26, 49, 94, 99, 29, 79, 94, 5, 111, 252, 216, 62, 195],
136+
ProtectionProfile::Aes128CmHmacSha1_80,
137+
None,
138+
None,
139+
)
140+
.unwrap()
141+
.encrypt_rtcp(&RAW_RTCP)
142+
.unwrap();
143+
144+
let mut ctx = Context::new(
145+
&vec![
146+
96, 180, 31, 4, 119, 137, 128, 252, 75, 194, 252, 44, 63, 56, 61, 55,
147+
],
148+
&vec![247, 26, 49, 94, 99, 29, 79, 94, 5, 111, 252, 216, 62, 195],
149+
ProtectionProfile::Aes128CmHmacSha1_80,
150+
None,
151+
None,
152+
)
153+
.unwrap();
107154

155+
c.bench_function("Benchmark RTCP decrypt", |b| {
156+
b.iter(|| ctx.decrypt_rtcp(&encrypted).unwrap());
157+
});
158+
}
108159

109-
criterion_group!(benches, benchmark_encrypt_rtp_aes_128_cm_hmac_sha1, benchmark_decrypt_rtp_aes_128_cm_hmac_sha1);
160+
criterion_group!(
161+
benches,
162+
benchmark_encrypt_rtp_aes_128_cm_hmac_sha1,
163+
benchmark_decrypt_rtp_aes_128_cm_hmac_sha1,
164+
benchmark_encrypt_rtcp_aes_128_cm_hmac_sha1,
165+
benchmark_decrypt_rtcp_aes_128_cm_hmac_sha1
166+
);
110167
criterion_main!(benches);

srtp/src/cipher/cipher_aes_cm_hmac_sha1/mod.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ use crate::error::{Error, Result};
77
use crate::key_derivation::*;
88
use crate::protection_profile::*;
99

10+
mod ctrcipher;
1011
#[cfg(feature = "openssl")]
1112
mod opensslcipher;
12-
mod ctrcipher;
1313

1414
type HmacSha1 = Hmac<Sha1>;
1515

1616
pub const CIPHER_AES_CM_HMAC_SHA1AUTH_TAG_LEN: usize = 10;
1717

18-
1918
pub(crate) struct CipherInner {
2019
srtp_session_salt: Vec<u8>,
2120
srtp_session_auth: HmacSha1,
@@ -131,10 +130,17 @@ impl CipherAesCmHmacSha1 {
131130
pub fn new(master_key: &[u8], master_salt: &[u8]) -> Result<Box<dyn Cipher + Send>> {
132131
let inner = CipherInner::new(master_key, master_salt)?;
133132

134-
if cfg!(feature = "openssl") {
135-
Ok(Box::new(opensslcipher::OpenSslCipher::new(inner, master_key, master_salt)?))
136-
} else {
137-
Ok(Box::new(ctrcipher::CtrCipher::new(inner, master_key, master_salt)?))
138-
}
133+
#[cfg(feature = "openssl")]
134+
return Ok(Box::new(opensslcipher::OpenSslCipher::new(
135+
inner,
136+
master_key,
137+
master_salt,
138+
)?));
139+
140+
Ok(Box::new(ctrcipher::CtrCipher::new(
141+
inner,
142+
master_key,
143+
master_salt,
144+
)?))
139145
}
140-
}
146+
}

0 commit comments

Comments
 (0)