Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark for xtra vs tokio mpsc channels #181

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[[bench]]
name = "throughput"
harness = false

[[bench]]
name = "xtra_vs_tokio_mpsc"
harness = false
116 changes: 116 additions & 0 deletions benches/xtra_vs_tokio_mpsc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use std::future::Future;

use async_trait::async_trait;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use tokio::runtime::Runtime;
use tokio::sync::mpsc;
use xtra::{Actor, Context, Handler};

struct Counter(u64);

#[async_trait]
impl Actor for Counter {
type Stop = ();
async fn stopped(self) -> Self::Stop {}
}

#[derive(Debug)]
struct Increment {}
struct Stop;

#[async_trait::async_trait]
impl Handler<Increment> for Counter {
type Return = ();

async fn handle(&mut self, _: Increment, _ctx: &mut Context<Self>) {
self.0 += 1;
}
}

#[async_trait::async_trait]
impl Handler<Stop> for Counter {
type Return = ();

async fn handle(&mut self, _: Stop, ctx: &mut Context<Self>) {
ctx.stop_self();
}
}

fn mpsc_counter() -> (mpsc::UnboundedSender<Increment>, impl Future<Output = ()>) {
let (sender, mut receiver) = mpsc::unbounded_channel();

let actor = async move {
let mut _counter = 0;

while let Some(Increment {}) = receiver.recv().await {
_counter += 1;
}
};

(sender, actor)
}

fn xtra_throughput(c: &mut Criterion) {
let mut group = c.benchmark_group("increment");
let runtime = Runtime::new().unwrap();
let _g = runtime.enter();

for num_messages in [100, 1000, 10000] {
group.bench_with_input(
BenchmarkId::new("xtra", num_messages),
&num_messages,
|b, &num_messages| {
b.to_async(&runtime).iter_batched(
|| {
let (xtra_address, xtra_context) = Context::new(None);
runtime.spawn(xtra_context.run(Counter(0)));

xtra_address
},
|xtra_address| async move {
for _ in 0..num_messages - 1 {
xtra_address.send(Increment {}).await.unwrap();
}

xtra_address.send(Stop).await.unwrap();
},
BatchSize::SmallInput,
);
},
);
}
}

fn mpsc_throughput(c: &mut Criterion) {
let mut group = c.benchmark_group("increment");
let runtime = Runtime::new().unwrap();
let _g = runtime.enter();

for num_messages in [100, 1000, 10000] {
group.bench_with_input(
BenchmarkId::new("mpsc", num_messages),
&num_messages,
|b, &num_messages| {
b.to_async(&runtime).iter_batched(
|| {
let (mpsc_address, mpsc_actor) = mpsc_counter();
runtime.spawn(mpsc_actor);

mpsc_address
},
|mpsc_address| async move {
for _ in 0..num_messages - 1 {
mpsc_address.send(Increment {}).unwrap();
}

drop(mpsc_address);
},
BatchSize::SmallInput,
);
},
);
}
}

criterion_group!(benches, xtra_throughput, mpsc_throughput);
criterion_main!(benches);