#![allow( clippy::large_enum_variant, clippy::type_complexity, clippy::too_many_arguments )] use criterion::{black_box, criterion_group, criterion_main, Criterion}; use mpstthree::binary::close::close; use mpstthree::binary::fork::fork_with_thread_id; use mpstthree::binary::recv::recv; use mpstthree::binary::send::send; use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send, session::Session}; use mpstthree::{choose, offer}; use std::error::Error; use std::thread::{spawn, JoinHandle}; // A enum BinaryA { More(Recv<(), Send<(), RecursA>>), Done(End), } type RecursA = Recv; fn binary_a_to_b(s: RecursA) -> Result<(), Box> { offer!(s, { BinaryA::Done(s) => { close(s) }, BinaryA::More(s) => { let (_, s) = recv(s)?; let s = send((), s); binary_a_to_b(s) }, }) } // B type RecursB = ::Dual; fn binary_b_to_a(s: Send<(), Recv<(), RecursB>>) -> Result> { let s = send((), s); let (_, s) = recv(s)?; Ok(s) } fn aux() { let mut threads = Vec::new(); let mut sessions = Vec::new(); for _ in 0..28 { let (thread, s): (JoinHandle<()>, RecursB) = fork_with_thread_id(black_box(binary_a_to_b)); threads.push(thread); sessions.push(s); } let main = spawn(move || { for _ in 0..LOOPS { sessions = sessions .into_iter() .map(|s| binary_b_to_a(choose!(BinaryA::More, s)).unwrap()) .collect::>(); } sessions .into_iter() .for_each(|s| close(choose!(BinaryA::Done, s)).unwrap()); threads.into_iter().for_each(|elt| elt.join().unwrap()); }); main.join().unwrap(); } ///////////////////////// static LOOPS: i64 = 100; pub fn mesh_protocol_binary(c: &mut Criterion) { c.bench_function(&format!("mesh eight binary {LOOPS}"), |b| b.iter(aux)); } ///////////////////////// criterion_group! { name = bench; config = Criterion::default().significance_level(0.05).without_plots().sample_size(100000); targets = mesh_protocol_binary, } criterion_main! { bench }