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

added pub/sub demo #303

Draft
wants to merge 1 commit into
base: thebutlah/add-experiments-dir
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion experiments/zenoh/src/lib.rs

This file was deleted.

102 changes: 102 additions & 0 deletions experiments/zenoh/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::{pin::pin, time::Duration};

use clap::Parser as _;
use color_eyre::{eyre::WrapErr as _, Result};
use zenoh::handlers::DefaultHandler;

#[derive(clap::Parser)]
enum Args {
Alice { payload_size: usize },
Bob {},
}

#[tokio::main]
async fn main() -> color_eyre::Result<()> {
orb_telemetry::TelemetryConfig::new().init();
tracing::debug!("debug logging is enabled");

let args = Args::parse();

match args {
Args::Alice { .. } => alice(args).await,
Args::Bob {} => bob().await,
}
}

async fn alice(args: Args) -> Result<()> {
let Args::Alice { payload_size } = args else {
unreachable!()
};

let session = zenoh::open(zenoh::Config::default())
.await
.expect("failed to open zenoh session");

let put_key = session.declare_keyexpr("alice/put/a").await.unwrap();
let payload: Vec<u8> = (0..payload_size).map(|v| v as u8).collect();
let publisher = session
.declare_publisher(put_key)
.congestion_control(zenoh::qos::CongestionControl::Block)
.encoding(zenoh::bytes::Encoding::ZENOH_BYTES)
.await
.unwrap();

let i = std::cell::Cell::new(0);
let mut send_fut = pin!(async {
loop {
let () = publisher.put(&payload).await.expect("failed to put");
i.set(i.get() + 1);
}
});
let interval_duration = Duration::from_millis(1000);
let mut interval = tokio::time::interval(interval_duration);
loop {
tokio::select! {
biased; _ = interval.tick() => {
let bytes_per_second = i.replace(0) * interval_duration.as_millis() as u64 * payload_size as u64 / 1000 ;
let mib_per_second: u64 = bytes_per_second >> 20;
tracing::info!("MiB/s: {mib_per_second}");
},
_ = &mut send_fut => break,
}
}

Ok(())
}

async fn bob() -> Result<()> {
let session = zenoh::open(zenoh::Config::default())
.await
.expect("failed to open zenoh session");

let get_key = session.declare_keyexpr("alice/put/a").await.unwrap();

let subscriber = session
.declare_subscriber(get_key)
.with(DefaultHandler::default())
.await
.expect("failed to create subscriber");

let byte_counter = std::cell::Cell::new(0u64);
let mut send_fut = pin!(async {
loop {
let sample = subscriber.recv_async().await.expect("failed to get");
let nbytes = sample.payload().slices().fold(0, |acc, s| acc + s.len());
byte_counter.set(byte_counter.get() + nbytes as u64);
}
});
let interval_duration = Duration::from_millis(1000);
let mut interval = tokio::time::interval(interval_duration);
loop {
tokio::select! {
biased; _ = interval.tick() => {
let bytes_per_second = byte_counter.replace(0) * interval_duration.as_millis() as u64 / 1000 ;
let mib_per_second: u64 = bytes_per_second >> 20;
tracing::info!("MiB/s: {mib_per_second}");
},
_ = &mut send_fut => break,
}
}

Ok(())
}
Loading