Skip to content

Commit

Permalink
Allow to box messages in the channels implicitly (#47)
Browse files Browse the repository at this point in the history
* Allow to box messages in the channels implicitly

* Add benchmarks

* Move thread-pool feature to dev dependencies
  • Loading branch information
vstakhov authored Jul 18, 2023
1 parent 9242838 commit 2ec50dd
Show file tree
Hide file tree
Showing 21 changed files with 1,039 additions and 54 deletions.
312 changes: 302 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion orchestra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ dyn-clonable = "0.9"
[dev-dependencies]
trybuild = "1.0.61"
rustversion = "1.0.6"

criterion = { version = "0.4.0" }
futures = { version = "0.3", features = ["thread-pool"] }

[[example]]
name = "duo"
Expand All @@ -33,6 +34,10 @@ crate-type = ["bin"]
name = "dig"
crate-type = ["bin"]

[[bench]]
name = "bench_main"
harness = false

[features]
default = ["deny_unconsumed_messages","deny_unsent_messages"]
# Generate a file containing the generated code that
Expand Down
10 changes: 10 additions & 0 deletions orchestra/benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use criterion::criterion_main;

mod benchmarks;

criterion_main! {
benchmarks::bench_boxed_subsystem_u8::benches,
benchmarks::bench_unboxed_subsystem_u8::benches,
benchmarks::bench_boxed_subsystem_large::benches,
benchmarks::bench_unboxed_subsystem_large::benches,
}
106 changes: 106 additions & 0 deletions orchestra/benches/benchmarks/bench_boxed_subsystem_large.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::all)]

use criterion::{criterion_group, Criterion};
use futures::{executor, executor::ThreadPool, future::try_join_all};
use orchestra::{self as orchestra, Spawner, *};
use std::time::Instant;

pub use super::misc::*;

#[derive(Default)]
pub struct SenderSubsys(pub u64);

#[derive(Default)]
pub struct ReceiverSubsys(pub u64);

#[orchestra(signal=SigSigSig, event=EvX, error=Yikes, gen=AllMessages, boxed_messages=true)]
struct BenchBoxedLarge {
#[subsystem(sends: [Msg1029], consumes: MsgU64, message_capacity: 60000)]
sender: Sender,

#[subsystem(blocking, consumes: Msg1029, sends: [MsgU64], message_capacity: 60000, signal_capacity: 128)]
receiver: Receiver,
}

#[orchestra::subsystem(Sender, error=Yikes)]
impl<Context> SenderSubsys {
fn start(self, mut ctx: Context) -> SpawnedSubsystem<Yikes> {
let mut sender = ctx.sender().clone();
SpawnedSubsystem {
name: "Sender",
future: Box::pin(async move {
let mut input: u64 = 0;
for i in 0..self.0 {
sender.send_message(Msg1029([i as u8; 1029])).await;
input ^= (i as u8) as u64;
}
match ctx.recv().await.unwrap() {
FromOrchestra::Communication { msg } => assert!(msg.0 == input),
_ => panic!("unexpected message"),
}
Ok(())
}),
}
}
}

#[orchestra::subsystem(Receiver, error=Yikes)]
impl<Context> ReceiverSubsys {
fn start(self, mut ctx: Context) -> SpawnedSubsystem<Yikes> {
let mut sender = ctx.sender().clone();
SpawnedSubsystem {
name: "Receiver",
future: Box::pin(async move {
let mut input: u64 = 0;
for _ in 0..self.0 {
match ctx.recv().await.unwrap() {
FromOrchestra::Communication { msg } => input ^= msg.0[0] as u64,
_ => panic!("unexpected message"),
}
}
sender.send_message(MsgU64(input)).await;
Ok(())
}),
}
}
}

fn test_boxed_large(c: &mut Criterion) {
let pool = ThreadPool::new().unwrap();
c.bench_function("send boxed message 1029 bytes", |b| {
let pool = pool.clone();
b.iter_custom(|niters| {
let pool = pool.clone();
let start = executor::block_on(async move {
let (orchestra, _handle) = BenchBoxedLarge::builder()
.sender(SenderSubsys(niters))
.receiver(ReceiverSubsys(niters))
.spawner(DummySpawner(pool))
.build()
.unwrap();

let start = Instant::now();
try_join_all(orchestra.running_subsystems).await.unwrap();
start
});
start.elapsed()
})
});
}

criterion_group!(benches, test_boxed_large);
106 changes: 106 additions & 0 deletions orchestra/benches/benchmarks/bench_boxed_subsystem_u8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::all)]

use criterion::{criterion_group, Criterion};
use futures::{executor, executor::ThreadPool, future::try_join_all};
use orchestra::{self as orchestra, Spawner, *};
use std::time::Instant;

pub use super::misc::*;

#[derive(Default)]
pub struct SenderSubsys(pub u64);

#[derive(Default)]
pub struct ReceiverSubsys(pub u64);

#[orchestra(signal=SigSigSig, event=EvX, error=Yikes, gen=AllMessages, boxed_messages=true)]
struct BenchBoxedU8 {
#[subsystem(sends: [MsgU8], consumes: MsgU64, message_capacity: 60000)]
sender: Sender,

#[subsystem(blocking, consumes: MsgU8, sends: [MsgU64], message_capacity: 60000, signal_capacity: 128)]
receiver: Receiver,
}

#[orchestra::subsystem(Sender, error=Yikes)]
impl<Context> SenderSubsys {
fn start(self, mut ctx: Context) -> SpawnedSubsystem<Yikes> {
let mut sender = ctx.sender().clone();
SpawnedSubsystem {
name: "Sender",
future: Box::pin(async move {
let mut input: u64 = 0;
for i in 0..self.0 {
sender.send_message(MsgU8(i as u8)).await;
input ^= (i as u8) as u64;
}
match ctx.recv().await.unwrap() {
FromOrchestra::Communication { msg } => assert!(msg.0 == input),
_ => panic!("unexpected message"),
}
Ok(())
}),
}
}
}

#[orchestra::subsystem(Receiver, error=Yikes)]
impl<Context> ReceiverSubsys {
fn start(self, mut ctx: Context) -> SpawnedSubsystem<Yikes> {
let mut sender = ctx.sender().clone();
SpawnedSubsystem {
name: "Receiver",
future: Box::pin(async move {
let mut input: u64 = 0;
for _ in 0..self.0 {
match ctx.recv().await.unwrap() {
FromOrchestra::Communication { msg } => input ^= msg.0 as u64,
_ => panic!("unexpected message"),
}
}
sender.send_message(MsgU64(input)).await;
Ok(())
}),
}
}
}

fn test_boxed_u8(c: &mut Criterion) {
let pool = ThreadPool::new().unwrap();
c.bench_function("send boxed message u8", |b| {
let pool = pool.clone();
b.iter_custom(|niters| {
let pool = pool.clone();
let start = executor::block_on(async move {
let (orchestra, _handle) = BenchBoxedU8::builder()
.sender(SenderSubsys(niters))
.receiver(ReceiverSubsys(niters))
.spawner(DummySpawner(pool))
.build()
.unwrap();

let start = Instant::now();
try_join_all(orchestra.running_subsystems).await.unwrap();
start
});
start.elapsed()
})
});
}

criterion_group!(benches, test_boxed_u8);
106 changes: 106 additions & 0 deletions orchestra/benches/benchmarks/bench_unboxed_subsystem_large.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::all)]

use criterion::{criterion_group, Criterion};
use futures::{executor, executor::ThreadPool, future::try_join_all};
use orchestra::{self as orchestra, Spawner, *};
use std::time::Instant;

pub use super::misc::*;

#[derive(Default)]
pub struct SenderSubsys(pub u64);

#[derive(Default)]
pub struct ReceiverSubsys(pub u64);

#[orchestra(signal=SigSigSig, event=EvX, error=Yikes, gen=AllMessages)]
struct BenchUnboxedLarge {
#[subsystem(sends: [Msg1029], consumes: MsgU64, message_capacity: 60000)]
sender: Sender,

#[subsystem(blocking, consumes: Msg1029, sends: [MsgU64], message_capacity: 60000, signal_capacity: 128)]
receiver: Receiver,
}

#[orchestra::subsystem(Sender, error=Yikes)]
impl<Context> SenderSubsys {
fn start(self, mut ctx: Context) -> SpawnedSubsystem<Yikes> {
let mut sender = ctx.sender().clone();
SpawnedSubsystem {
name: "Sender",
future: Box::pin(async move {
let mut input: u64 = 0;
for i in 0..self.0 {
sender.send_message(Msg1029([i as u8; 1029])).await;
input ^= (i as u8) as u64;
}
match ctx.recv().await.unwrap() {
FromOrchestra::Communication { msg } => assert!(msg.0 == input),
_ => panic!("unexpected message"),
}
Ok(())
}),
}
}
}

#[orchestra::subsystem(Receiver, error=Yikes)]
impl<Context> ReceiverSubsys {
fn start(self, mut ctx: Context) -> SpawnedSubsystem<Yikes> {
let mut sender = ctx.sender().clone();
SpawnedSubsystem {
name: "Receiver",
future: Box::pin(async move {
let mut input: u64 = 0;
for _ in 0..self.0 {
match ctx.recv().await.unwrap() {
FromOrchestra::Communication { msg } => input ^= msg.0[0] as u64,
_ => panic!("unexpected message"),
}
}
sender.send_message(MsgU64(input)).await;
Ok(())
}),
}
}
}

fn test_unboxed_large(c: &mut Criterion) {
let pool = ThreadPool::new().unwrap();
c.bench_function("send unboxed message 1029 bytes", |b| {
let pool = pool.clone();
b.iter_custom(|niters| {
let pool = pool.clone();
let start = executor::block_on(async move {
let (orchestra, _handle) = BenchUnboxedLarge::builder()
.sender(SenderSubsys(niters))
.receiver(ReceiverSubsys(niters))
.spawner(DummySpawner(pool))
.build()
.unwrap();

let start = Instant::now();
try_join_all(orchestra.running_subsystems).await.unwrap();
start
});
start.elapsed()
})
});
}

criterion_group!(benches, test_unboxed_large);
Loading

0 comments on commit 2ec50dd

Please sign in to comment.