提交 dfd418eb authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Refactor payload channel notifications to use synchronous API, refactor…

Refactor payload channel notifications to use synchronous API, refactor benchmark to use it, remove send benchmark as it is not representative and just uses a lot of memory
上级 f2978ead
...@@ -65,12 +65,9 @@ version = "0.8.2" ...@@ -65,12 +65,9 @@ version = "0.8.2"
actix = "0.12.0" actix = "0.12.0"
actix-web = "4.0.0-beta.8" actix-web = "4.0.0-beta.8"
actix-web-actors = "4.0.0-beta.6" actix-web-actors = "4.0.0-beta.6"
criterion = "0.3.5"
env_logger = "0.9.0" env_logger = "0.9.0"
[dev-dependencies.criterion]
version = "0.3.5"
features = ["async_futures"]
[[bench]] [[bench]]
name = "direct_data" name = "direct_data"
harness = false harness = false
use criterion::async_executor::FuturesExecutor;
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
use futures_lite::StreamExt;
use mediasoup::prelude::*; use mediasoup::prelude::*;
use std::borrow::Cow; use std::borrow::Cow;
use std::sync::mpsc;
async fn create_data_producer_consumer_pair( async fn create_data_producer_consumer_pair(
) -> Result<(DataProducer, DataConsumer), Box<dyn std::error::Error>> { ) -> Result<(DataProducer, DataConsumer), Box<dyn std::error::Error>> {
...@@ -33,26 +32,6 @@ pub fn criterion_benchmark(c: &mut Criterion) { ...@@ -33,26 +32,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.take(512) .take(512)
.collect::<Vec<u8>>(); .collect::<Vec<u8>>();
{
let (data_producer, _data_consumer) = futures_lite::future::block_on(async {
create_data_producer_consumer_pair().await.unwrap()
});
let direct_data_producer =
if let DataProducer::Direct(direct_data_producer) = data_producer.clone() {
direct_data_producer
} else {
unreachable!()
};
group.bench_with_input("send", &data, |b, data| {
b.to_async(FuturesExecutor).iter(|| async {
let _ = direct_data_producer
.send(WebRtcMessage::Binary(Cow::from(data)))
.await;
})
});
}
{ {
let (data_producer, data_consumer) = futures_lite::future::block_on(async { let (data_producer, data_consumer) = futures_lite::future::block_on(async {
create_data_producer_consumer_pair().await.unwrap() create_data_producer_consumer_pair().await.unwrap()
...@@ -66,17 +45,15 @@ pub fn criterion_benchmark(c: &mut Criterion) { ...@@ -66,17 +45,15 @@ pub fn criterion_benchmark(c: &mut Criterion) {
}; };
group.bench_with_input("recv", &data, |b, data| { group.bench_with_input("recv", &data, |b, data| {
b.to_async(FuturesExecutor).iter(|| async { b.iter(|| {
let (sender, mut receiver) = async_channel::unbounded::<()>(); let (sender, receiver) = mpsc::sync_channel(1);
let _handler_id = data_consumer.on_message(move |_message| { let _handler_id = data_consumer.on_message(move |_message| {
let _ = sender.try_send(()); let _ = sender.send(());
}); });
let _ = direct_data_producer let _ = direct_data_producer.send(WebRtcMessage::Binary(Cow::from(data)));
.send(WebRtcMessage::Binary(Cow::from(data)))
.await;
let _ = receiver.next().await; let _ = receiver.recv();
}) })
}); });
} }
......
...@@ -440,13 +440,11 @@ impl DataProducer { ...@@ -440,13 +440,11 @@ impl DataProducer {
} }
impl DirectDataProducer { impl DirectDataProducer {
/// Sends direct messages from the Rust process. /// Sends direct messages from the Rust to the worker.
pub async fn send(&self, message: WebRtcMessage<'_>) -> Result<(), NotificationError> { pub fn send(&self, message: WebRtcMessage<'_>) -> Result<(), NotificationError> {
let (ppid, payload) = message.into_ppid_and_payload(); let (ppid, payload) = message.into_ppid_and_payload();
self.inner self.inner.payload_channel.notify(
.payload_channel
.notify(
DataProducerSendNotification { DataProducerSendNotification {
internal: DataProducerInternal { internal: DataProducerInternal {
router_id: self.inner.transport.router_id(), router_id: self.inner.transport.router_id(),
...@@ -457,7 +455,6 @@ impl DirectDataProducer { ...@@ -457,7 +455,6 @@ impl DirectDataProducer {
}, },
payload.into_owned(), payload.into_owned(),
) )
.await
} }
} }
......
...@@ -534,16 +534,13 @@ impl DirectTransport { ...@@ -534,16 +534,13 @@ impl DirectTransport {
/// Send a RTCP packet from the Rust process. /// Send a RTCP packet from the Rust process.
/// ///
/// * `rtcp_packet` - Bytes containing a valid RTCP packet (can be a compound packet). /// * `rtcp_packet` - Bytes containing a valid RTCP packet (can be a compound packet).
pub async fn send_rtcp(&self, rtcp_packet: Vec<u8>) -> Result<(), NotificationError> { pub fn send_rtcp(&self, rtcp_packet: Vec<u8>) -> Result<(), NotificationError> {
self.inner self.inner.payload_channel.notify(
.payload_channel
.notify(
TransportSendRtcpNotification { TransportSendRtcpNotification {
internal: self.get_internal(), internal: self.get_internal(),
}, },
rtcp_packet, rtcp_packet,
) )
.await
} }
/// Callback is called when the direct transport receives a RTCP packet from its router. /// Callback is called when the direct transport receives a RTCP packet from its router.
......
...@@ -765,10 +765,8 @@ impl Producer { ...@@ -765,10 +765,8 @@ impl Producer {
impl DirectProducer { impl DirectProducer {
/// Sends a RTP packet from the Rust process. /// Sends a RTP packet from the Rust process.
pub async fn send(&self, rtp_packet: Vec<u8>) -> Result<(), NotificationError> { pub fn send(&self, rtp_packet: Vec<u8>) -> Result<(), NotificationError> {
self.inner self.inner.payload_channel.notify(
.payload_channel
.notify(
ProducerSendNotification { ProducerSendNotification {
internal: ProducerInternal { internal: ProducerInternal {
router_id: self.inner.transport.router_id(), router_id: self.inner.transport.router_id(),
...@@ -778,7 +776,6 @@ impl DirectProducer { ...@@ -778,7 +776,6 @@ impl DirectProducer {
}, },
rtp_packet, rtp_packet,
) )
.await
} }
} }
......
...@@ -308,7 +308,7 @@ impl PayloadChannel { ...@@ -308,7 +308,7 @@ impl PayloadChannel {
}) })
} }
pub(crate) async fn notify<N>( pub(crate) fn notify<N>(
&self, &self,
notification: N, notification: N,
payload: Vec<u8>, payload: Vec<u8>,
......
...@@ -247,7 +247,6 @@ fn send_succeeds() { ...@@ -247,7 +247,6 @@ fn send_succeeds() {
direct_data_producer direct_data_producer
.send(message) .send(message)
.await
.expect("Failed to send message"); .expect("Failed to send message");
if id == num_messages { if id == num_messages {
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论