From 5c0e5052dbf6ab99500a54fa11a766396f841386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 25 Oct 2023 12:13:34 +0200 Subject: [PATCH] Rust: add bench for producer creation/dump (#1192) Rust: add bench for producer creation/dump --- rust/Cargo.toml | 3 + rust/benches/producer.rs | 166 +++++++++++++++++++++++++++++++++++++++ rust/src/prelude.rs | 5 +- 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 rust/benches/producer.rs diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 1760125b11..f884cf9dad 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -81,3 +81,6 @@ version = "4.2.1" [[bench]] name = "direct_data" harness = false +[[bench]] +name = "producer" +harness = false diff --git a/rust/benches/producer.rs b/rust/benches/producer.rs new file mode 100644 index 0000000000..18493d9c93 --- /dev/null +++ b/rust/benches/producer.rs @@ -0,0 +1,166 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use mediasoup::prelude::*; +use std::env; +use std::net::{IpAddr, Ipv4Addr}; +use std::num::{NonZeroU32, NonZeroU8}; + +fn media_codecs() -> Vec { + vec![ + RtpCodecCapability::Audio { + mime_type: MimeTypeAudio::Opus, + preferred_payload_type: None, + clock_rate: NonZeroU32::new(48000).unwrap(), + channels: NonZeroU8::new(2).unwrap(), + parameters: RtpCodecParametersParameters::from([("foo", "111".into())]), + rtcp_feedback: vec![], + }, + RtpCodecCapability::Video { + mime_type: MimeTypeVideo::Vp8, + preferred_payload_type: None, + clock_rate: NonZeroU32::new(90000).unwrap(), + parameters: RtpCodecParametersParameters::default(), + rtcp_feedback: vec![], + }, + RtpCodecCapability::Video { + mime_type: MimeTypeVideo::H264, + preferred_payload_type: None, + clock_rate: NonZeroU32::new(90000).unwrap(), + parameters: RtpCodecParametersParameters::from([ + ("level-asymmetry-allowed", 1_u32.into()), + ("packetization-mode", 1_u32.into()), + ("profile-level-id", "4d0032".into()), + ("foo", "bar".into()), + ]), + rtcp_feedback: vec![], + }, + ] +} + +async fn init() -> (Worker, Router, WebRtcTransport, WebRtcTransport) { + { + let mut builder = env_logger::builder(); + if env::var(env_logger::DEFAULT_FILTER_ENV).is_err() { + builder.filter_level(log::LevelFilter::Off); + } + let _ = builder.is_test(true).try_init(); + } + + let worker_manager = WorkerManager::new(); + + let worker = worker_manager + .create_worker(WorkerSettings::default()) + .await + .expect("Failed to create worker"); + + let router = worker + .create_router(RouterOptions::new(media_codecs())) + .await + .expect("Failed to create router"); + + let transport_options = + WebRtcTransportOptions::new(WebRtcTransportListenInfos::new(ListenInfo { + protocol: Protocol::Udp, + ip: IpAddr::V4(Ipv4Addr::LOCALHOST), + announced_ip: None, + port: None, + send_buffer_size: None, + recv_buffer_size: None, + })); + + let transport_1 = router + .create_webrtc_transport(transport_options.clone()) + .await + .expect("Failed to create transport1"); + + let transport_2 = router + .create_webrtc_transport(transport_options) + .await + .expect("Failed to create transport2"); + + (worker, router, transport_1, transport_2) +} + +fn audio_producer_options() -> ProducerOptions { + ProducerOptions::new( + MediaKind::Audio, + RtpParameters { + mid: Some(fastrand::u32(100_000_000..999_999_999).to_string()), + codecs: vec![RtpCodecParameters::Audio { + mime_type: MimeTypeAudio::Opus, + payload_type: 0, + clock_rate: NonZeroU32::new(48000).unwrap(), + channels: NonZeroU8::new(2).unwrap(), + parameters: RtpCodecParametersParameters::from([ + ("useinbandfec", 1_u32.into()), + ("usedtx", 1_u32.into()), + ("foo", "222.222".into()), + ("bar", "333".into()), + ]), + rtcp_feedback: vec![], + }], + header_extensions: vec![ + RtpHeaderExtensionParameters { + uri: RtpHeaderExtensionUri::Mid, + id: 10, + encrypt: false, + }, + RtpHeaderExtensionParameters { + uri: RtpHeaderExtensionUri::AudioLevel, + id: 12, + encrypt: false, + }, + ], + // Missing encodings on purpose. + encodings: vec![], + rtcp: RtcpParameters { + cname: Some("audio-1".to_string()), + ..RtcpParameters::default() + }, + }, + ) +} + +pub fn criterion_benchmark(c: &mut Criterion) { + let mut group = c.benchmark_group("producer"); + + { + let (_worker, _router, transport_1, _transport_2) = + futures_lite::future::block_on(async { init().await }); + + let producer = futures_lite::future::block_on(async { + let (_worker, _router, transport_1, _transport_2) = init().await; + transport_1 + .produce(audio_producer_options()) + .await + .expect("Failed to produce audio") + }); + + group.bench_function("create", |b| { + b.iter(|| { + let _ = futures_lite::future::block_on(async { + transport_1 + .produce(audio_producer_options()) + .await + .expect("Failed to produce audio") + }); + }) + }); + + group.bench_function("dump", |b| { + b.iter(|| { + let _ = futures_lite::future::block_on(async { producer.dump().await }); + }) + }); + + group.bench_function("stats", |b| { + b.iter(|| { + let _ = futures_lite::future::block_on(async { producer.get_stats().await }); + }) + }); + } + + group.finish(); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/rust/src/prelude.rs b/rust/src/prelude.rs index 62953870e5..9195a393b4 100644 --- a/rust/src/prelude.rs +++ b/rust/src/prelude.rs @@ -63,8 +63,9 @@ pub use crate::data_structures::{ AppData, DtlsParameters, IceCandidate, IceParameters, ListenInfo, Protocol, WebRtcMessage, }; pub use crate::rtp_parameters::{ - MediaKind, MimeTypeAudio, MimeTypeVideo, RtcpFeedback, RtpCapabilities, - RtpCapabilitiesFinalized, RtpCodecCapability, RtpCodecParametersParameters, RtpParameters, + MediaKind, MimeTypeAudio, MimeTypeVideo, RtcpFeedback, RtcpParameters, RtpCapabilities, + RtpCapabilitiesFinalized, RtpCodecCapability, RtpCodecParameters, RtpCodecParametersParameters, + RtpHeaderExtensionParameters, RtpHeaderExtensionUri, RtpParameters, }; pub use crate::sctp_parameters::SctpStreamParameters; pub use crate::srtp_parameters::SrtpCryptoSuite;