use std::cell::RefCell; use codas_flow::Flow; use criterion::{criterion_group, criterion_main, Criterion}; use tokio::sync::broadcast; fn channels(c: &mut Criterion) { let mut group = c.benchmark_group("Channels"); group.bench_function("Many(1):Many(1) Flow; Move->Read", |b| { let i = RefCell::new(0); let (pubs, [subs]) = Flow::::new(64).unwrap(); let pubs = RefCell::new(pubs); let subs = RefCell::new(subs); b.to_async(tokio::runtime::Runtime::new().unwrap()) .iter(|| async { let mut i = i.borrow_mut(); let mut pubs = pubs.borrow_mut(); let mut next = pubs.next().await.expect("next"); next.value = *i; drop(next); let value = subs.borrow_mut().next().await.expect("value").value; assert_eq!(*i, value); *i += 1; }); }); group.bench_function("Many(1):Many(1) Tokio; Move->Clone", |b| { let i = RefCell::new(0); let (tx, rx) = broadcast::channel(64); let rx = RefCell::new(rx); b.to_async(tokio::runtime::Runtime::new().unwrap()) .iter(|| async { let mut i = i.borrow_mut(); let _ = tx.send(TestStruct { value: *i }).unwrap(); let msg = rx.borrow_mut().recv().await; assert_eq!(*i, msg.expect("msg").value); *i += 1; }); }); } // Create a new group named `benches` and // run it with all benchmark methods. criterion_group!(benches, channels); criterion_main!(benches); /// Simplistic test data structure for [`channels`]. #[derive(Debug, Default, Clone)] struct TestStruct { value: i64, }