#![allow( clippy::type_complexity, clippy::too_many_arguments, clippy::large_enum_variant )] use criterion::{black_box, criterion_group, criterion_main, Criterion}; use mpstthree::binary::struct_trait::end::End; use mpstthree::binary_atmp::struct_trait::{recv::RecvTimed, send::SendTimed}; use mpstthree::generate_atmp; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use std::collections::HashMap; use std::error::Error; use std::time::Instant; // See the folder scribble_protocols for the related Scribble protocol // Create the new MeshedChannels for three participants and the close and fork functions generate_atmp!(MeshedChannels, A, B); // Types // A type Choose0fromAtoB = SendTimed; // B enum Branching0fromAtoB { More(MeshedChannels), Done(MeshedChannels), } type RSRecursBtoA = RecvTimed< i32, 'a', 0, true, 10, true, ' ', SendTimed, >; type ThreeRoleA = RoleA>>; type RecursBtoA = RecvTimed; // Creating the MP sessions type EndpointA = MeshedChannels; type EndpointB = MeshedChannels, NameB>; // Functions fn endpoint_a(s: EndpointA, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); recurs_a(s, LOOPS, 1, all_clocks) } fn recurs_a( s: EndpointA, index: i32, old: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match index { 0 => { let s = choose_mpst_a_to_all!(s, all_clocks, Branching0fromAtoB::Done); s.close() } i => { let s = choose_mpst_a_to_all!(s, all_clocks, Branching0fromAtoB::More); let s = s.send(old, all_clocks)?; let (new, s) = s.recv(all_clocks)?; recurs_a(s, i - 1, new, all_clocks) } } } fn endpoint_b(s: EndpointB, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); recurs_b(s, 0, all_clocks) } fn recurs_b( s: EndpointB, old: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromAtoB::Done(s) => { s.close() }, Branching0fromAtoB::More(s) => { let (new, s) = s.recv(all_clocks)?; let s = s.send(new + old, all_clocks)?; recurs_b(s, new + old, all_clocks) }, }) } fn aux() { let (thread_a, thread_b) = fork_mpst(black_box(endpoint_a), black_box(endpoint_b)); thread_a.join().unwrap(); thread_b.join().unwrap(); } ///////////////////////// static LOOPS: i32 = 20; pub fn fib(c: &mut Criterion) { c.bench_function(&format!("ATMP Fibo {LOOPS}"), |b| b.iter(aux)); } ///////////////////////// criterion_group! { name = bench; config = Criterion::default().significance_level(0.05).without_plots().sample_size(100000); targets = fib, } criterion_main! { bench }