#![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::struct_trait::session::Session; 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, C, S); // Stacks // C type ThreeRoleC = RoleC>>; type FiveRoleC = RoleC>>>>; // S type TwoRoleS = RoleS>; // Types // Step 0 // C type Choose0fromCtoS = SendTimed; type EndpointC0 = MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromCtoS>, RoleS, NameC, >; // S enum Branching0fromCtoS { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose1fromStoC>, RoleC, NameS, >, ), Quit(MeshedChannels, RoleC, NameS>), } type Offer0fromCtoS = ::Dual; type EndpointS0 = MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', Offer0fromCtoS>, RoleC>, NameS, >; // Step 1 // C enum Branching1fromStoC { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose2fromCtoS>, RoleS, NameC, >, ), Loop( MeshedChannels, TwoRoleS, NameC>, ), } type Offer1fromStoC = ::Dual; type EndpointC1 = MeshedChannels, NameC>; // S type Choose1fromStoC = SendTimed; type EndpointS1 = MeshedChannels; // Step 2 // C type Choose2fromCtoS = SendTimed; type EndpointC2 = MeshedChannels; // S enum Branching2fromCtoS { Continue( MeshedChannels< RecvTimed< (), 'a', 0, true, 10, true, ' ', SendTimed<(), 'a', 0, true, 10, true, ' ', Offer3fromCtoS>, >, ThreeRoleC, NameS, >, ), Quit(MeshedChannels, RoleC, NameS>), } type Offer2fromCtoS = ::Dual; type EndpointS2 = MeshedChannels, NameS>; // Step 3 // C type Choose3fromCtoS = SendTimed; type EndpointC3 = MeshedChannels; // S enum Branching3fromCtoS { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose4fromStoC>, RoleC, NameS, >, ), Quit(MeshedChannels, RoleC, NameS>), } type Offer3fromCtoS = ::Dual; type EndpointS3 = MeshedChannels, NameS>; // Step 4 // C enum Branching4fromStoC { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose5fromCtoS>, RoleS, NameC, >, ), Loop( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Offer4fromStoC>, RoleS>, NameC, >, ), } type Offer4fromStoC = ::Dual; type EndpointC4 = MeshedChannels, NameC>; // S type Choose4fromStoC = SendTimed; type EndpointS4 = MeshedChannels; // Step 5 // C type Choose5fromCtoS = SendTimed; type EndpointC5 = MeshedChannels; // S enum Branching5fromCtoS { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose6fromStoC>, RoleC, NameS, >, ), Quit(MeshedChannels, RoleC, NameS>), } type Offer5fromCtoS = ::Dual; type EndpointS5 = MeshedChannels, NameS>; // Step 6 // C enum Branching6fromStoC { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose7fromCtoS>, RoleS, NameC, >, ), Loop( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Offer6fromStoC>, RoleS>, NameC, >, ), } type Offer6fromStoC = ::Dual; type EndpointC6 = MeshedChannels, NameC>; // S type Choose6fromStoC = SendTimed; type EndpointS6 = MeshedChannels; // Step 7 // C type Choose7fromCtoS = SendTimed; type EndpointC7 = MeshedChannels; // S enum Branching7fromCtoS { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose8fromStoC>, RoleC, NameS, >, ), Quit(MeshedChannels, RoleC, NameS>), } type Offer7fromCtoS = ::Dual; type EndpointS7 = MeshedChannels, NameS>; // Step 8 // C enum Branching8fromStoC { Continue( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose9fromCtoS>, RoleS, NameC, >, ), Loop( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose7fromCtoS>, RoleS, NameC, >, ), } type Offer8fromStoC = ::Dual; type EndpointC8 = MeshedChannels, NameC>; // S type Choose8fromStoC = SendTimed; type EndpointS8 = MeshedChannels; // Step 9 // C type Choose9fromCtoS = SendTimed; type EndpointC9 = MeshedChannels; // S enum Branching9fromCtoS { Continue(MeshedChannels), Loop( MeshedChannels< RecvTimed< (), 'a', 0, true, 10, true, ' ', SendTimed<(), 'a', 0, true, 10, true, ' ', Offer9fromCtoS>, >, ThreeRoleC, NameS, >, ), } type FullOffer10fromCtoS = RecvTimed< (), 'a', 0, true, 10, true, ' ', SendTimed< (), 'a', 0, true, 10, true, ' ', RecvTimed< (), 'a', 0, true, 10, true, ' ', RecvTimed<(), 'a', 0, true, 10, true, ' ', Offer10fromCtoS>, >, >, >; type Offer9fromCtoS = ::Dual; type EndpointS9 = MeshedChannels, NameS>; // Step 10 // C type Choose10fromCtoS = SendTimed; type EndpointC10 = MeshedChannels; // S enum Branching10fromCtoS { Data( MeshedChannels< RecvTimed< (), 'a', 0, true, 10, true, ' ', RecvTimed<(), 'a', 0, true, 10, true, ' ', Offer10fromCtoS>, >, ThreeRoleC, NameS, >, ), Subject( MeshedChannels< RecvTimed< (), 'a', 0, true, 10, true, ' ', RecvTimed<(), 'a', 0, true, 10, true, ' ', Offer10fromCtoS>, >, ThreeRoleC, NameS, >, ), End( MeshedChannels< RecvTimed< (), 'a', 0, true, 10, true, ' ', SendTimed<(), 'a', 0, true, 10, true, ' ', Offer7fromCtoS>, >, ThreeRoleC, NameS, >, ), } type Offer10fromCtoS = ::Dual; type EndpointS10 = MeshedChannels, NameS>; // Functions fn endpoint_c_init( s: EndpointC0, all_clocks: &mut HashMap, ) -> Result<(), Box> { endpoint_c_0(s, LOOPS, all_clocks) } fn endpoint_c_0( s: EndpointC0, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); let (_, s) = s.recv(all_clocks)?; match loops { 0 => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching0fromCtoS::Quit); let s = s.send((), all_clocks)?; s.close() } _ => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching0fromCtoS::Continue); let s = s.send((), all_clocks)?; endpoint_c_1(s, loops, all_clocks) } } } fn endpoint_c_1( s: EndpointC1, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching1fromStoC::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_2(s, loops, all_clocks) }, Branching1fromStoC::Loop(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_1(s, loops, all_clocks) }, }) } fn endpoint_c_2( s: EndpointC2, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching2fromCtoS::Quit); let s = s.send((), all_clocks)?; s.close() } _ => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching2fromCtoS::Continue); let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; endpoint_c_3(s, loops, all_clocks) } } } fn endpoint_c_3( s: EndpointC3, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching3fromCtoS::Quit); let s = s.send((), all_clocks)?; s.close() } _ => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching3fromCtoS::Continue); let s = s.send((), all_clocks)?; endpoint_c_4(s, loops, all_clocks) } } } fn endpoint_c_4( s: EndpointC4, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching4fromStoC::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_5(s, loops, all_clocks) }, Branching4fromStoC::Loop(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_4(s, loops, all_clocks) }, }) } fn endpoint_c_5( s: EndpointC5, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching5fromCtoS::Quit); let s = s.send((), all_clocks)?; s.close() } _ => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching5fromCtoS::Continue); let s = s.send((), all_clocks)?; endpoint_c_6(s, loops, all_clocks) } } } fn endpoint_c_6( s: EndpointC6, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching6fromStoC::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_7(s, loops, all_clocks) }, Branching6fromStoC::Loop(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_6(s, loops, all_clocks) }, }) } fn endpoint_c_7( s: EndpointC7, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching7fromCtoS::Quit); let s = s.send((), all_clocks)?; s.close() } _ => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching7fromCtoS::Continue); let s = s.send((), all_clocks)?; endpoint_c_8(s, loops, all_clocks) } } } fn endpoint_c_8( s: EndpointC8, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching8fromStoC::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_9(s, loops, all_clocks) }, Branching8fromStoC::Loop(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_c_7(s, loops, all_clocks) }, }) } fn endpoint_c_9( s: EndpointC9, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching9fromCtoS::Loop); let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; endpoint_c_9(s, loops, all_clocks) } _ => { let s = choose_mpst_c_to_all!(s, all_clocks, Branching9fromCtoS::Continue); let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let s = s.send((), all_clocks)?; endpoint_c_10(s, loops, all_clocks) } } } fn endpoint_c_10( s: EndpointC10, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { if loops == 0 { let s = choose_mpst_c_to_all!(s, all_clocks, Branching10fromCtoS::End); let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; endpoint_c_7(s, loops, all_clocks) } else if loops % 2 == 1 { let s = choose_mpst_c_to_all!(s, all_clocks, Branching10fromCtoS::Subject); let s = s.send((), all_clocks)?; let s = s.send((), all_clocks)?; endpoint_c_10(s, loops - 1, all_clocks) } else { let s = choose_mpst_c_to_all!(s, all_clocks, Branching10fromCtoS::Data); let s = s.send((), all_clocks)?; let s = s.send((), all_clocks)?; endpoint_c_10(s, loops - 1, all_clocks) } } /// fn endpoint_s_init( s: EndpointS0, all_clocks: &mut HashMap, ) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); endpoint_s_0(s, 100, all_clocks) } fn endpoint_s_0( s: EndpointS0, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { let s = s.send((), all_clocks)?; offer_mpst!(s, all_clocks, { Branching0fromCtoS::Quit(s) => { let (_, s) = s.recv(all_clocks)?; s.close() }, Branching0fromCtoS::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_s_1(s, loops, all_clocks) }, }) } fn endpoint_s_1( s: EndpointS1, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching1fromStoC::Loop); let s = s.send((), all_clocks)?; endpoint_s_1(s, loops, all_clocks) } _ => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching1fromStoC::Continue); let s = s.send((), all_clocks)?; endpoint_s_2(s, loops, all_clocks) } } } fn endpoint_s_2( s: EndpointS2, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching2fromCtoS::Quit(s) => { let (_, s) = s.recv(all_clocks)?; s.close() }, Branching2fromCtoS::Continue(s) => { let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_s_3(s, loops, all_clocks) }, }) } fn endpoint_s_3( s: EndpointS3, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching3fromCtoS::Quit(s) => { let (_, s) = s.recv(all_clocks)?; s.close() }, Branching3fromCtoS::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_s_4(s, loops, all_clocks) }, }) } fn endpoint_s_4( s: EndpointS4, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching4fromStoC::Loop); let s = s.send((), all_clocks)?; endpoint_s_4(s, loops, all_clocks) } _ => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching4fromStoC::Continue); let s = s.send((), all_clocks)?; endpoint_s_5(s, loops, all_clocks) } } } fn endpoint_s_5( s: EndpointS5, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching5fromCtoS::Quit(s) => { let (_, s) = s.recv(all_clocks)?; s.close() }, Branching5fromCtoS::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_s_6(s, loops, all_clocks) }, }) } fn endpoint_s_6( s: EndpointS6, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching6fromStoC::Loop); let s = s.send((), all_clocks)?; endpoint_s_6(s, loops, all_clocks) } _ => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching6fromStoC::Continue); let s = s.send((), all_clocks)?; endpoint_s_7(s, loops, all_clocks) } } } fn endpoint_s_7( s: EndpointS7, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching7fromCtoS::Quit(s) => { let (_, s) = s.recv(all_clocks)?; s.close() }, Branching7fromCtoS::Continue(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_s_8(s, loops, all_clocks) }, }) } fn endpoint_s_8( s: EndpointS8, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { match loops { 0 => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching8fromStoC::Loop); let s = s.send((), all_clocks)?; endpoint_s_7(s, loops, all_clocks) } _ => { let s = choose_mpst_s_to_all!(s, all_clocks, Branching8fromStoC::Continue); let s = s.send((), all_clocks)?; endpoint_s_9(s, loops, all_clocks) } } } fn endpoint_s_9( s: EndpointS9, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching9fromCtoS::Loop(s) => { let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_s_9(s, loops, all_clocks) }, Branching9fromCtoS::Continue(s) => { let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; let (_, s) = s.recv(all_clocks)?; endpoint_s_10(s, loops, all_clocks) }, }) } fn endpoint_s_10( s: EndpointS10, loops: i32, all_clocks: &mut HashMap, ) -> Result<(), Box> { offer_mpst!(s, all_clocks, { Branching10fromCtoS::Data(s) => { let (_, s) = s.recv(all_clocks)?; let (_, s) = s.recv(all_clocks)?; endpoint_s_10(s, loops - 1, all_clocks) }, Branching10fromCtoS::Subject(s) => { let (_, s) = s.recv(all_clocks)?; let (_, s) = s.recv(all_clocks)?; endpoint_s_10(s, loops - 1, all_clocks) }, Branching10fromCtoS::End(s) => { let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_s_7(s, loops - 1, all_clocks) }, }) } /// fn aux() { let (thread_c, thread_s) = fork_mpst(black_box(endpoint_c_init), black_box(endpoint_s_init)); thread_c.join().unwrap(); thread_s.join().unwrap(); } ///////////////////////// static LOOPS: i32 = 100; pub fn smtp(c: &mut Criterion) { c.bench_function("ATMP SMTP", |b| b.iter(aux)); } ///////////////////////// criterion_group! { name = bench; config = Criterion::default().significance_level(0.05).without_plots().sample_size(100000); targets = smtp, } criterion_main! { bench }