Skip to content
Merged
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
463 changes: 463 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,20 @@ clean:
# Watch and run tests on changes (requires bacon)
watch:
bacon test

# Check if `uv` is installed. See installation instructions https://docs.astral.sh/uv/getting-started/installation/
check-uv:
@if ! command -v uv >/dev/null 2>&1; then echo "Requires \`uv\`. See installation instructions https://docs.astral.sh/uv/getting-started/installation/"; exit 1; fi

# Run Actor->Broker->Actor transport benchmark and print throughput matrix.
# Print throughput matrix (messages/sec) from Criterion estimates.
bench-a2a: check-uv
cargo bench -p maiko --bench actor_to_actor_transport_maiko -- --noplot
uv run --project scripts scripts/bench_a2a_matrix.py

# Run all Actor->Actor transport benches, then compare throughput across frameworks.
bench-a2a-compare: check-uv
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_maiko -- --noplot
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_actix -- --noplot
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_ractor -- --noplot
uv run --project scripts scripts/bench_a2a_compare.py
18 changes: 18 additions & 0 deletions maiko/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
authors = ["David de Rosier <ddrcode@gmail.com>"]
autobenches = false
categories = ["asynchronous", "concurrency"]
description = "Lightweight event-driven actor runtime with topic-based pub/sub for Tokio"
documentation = "https://docs.rs/maiko"
Expand Down Expand Up @@ -38,8 +39,10 @@ tracing = "0.1"
uuid = { version = "1.20", features = ["v4"] }

[dev-dependencies]
actix = "0.13"
criterion = "0.8.2"
getrandom = "0.4"
ractor = { version = "0.15", features = ["async-trait"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }

[[example]]
Expand Down Expand Up @@ -69,3 +72,18 @@ name = "backpressure"
harness = false
name = "broker_subscriber_lookup"
path = "benches/broker_subscriber_lookup.rs"

[[bench]]
harness = false
name = "actor_to_actor_transport_maiko"
path = "benches/actor_to_actor_transport_maiko.rs"

[[bench]]
harness = false
name = "actor_to_actor_transport_actix"
path = "benches/actor_to_actor_transport_actix.rs"

[[bench]]
harness = false
name = "actor_to_actor_transport_ractor"
path = "benches/actor_to_actor_transport_ractor.rs"
139 changes: 139 additions & 0 deletions maiko/benches/COMPARISON.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Actor-to-Actor Comparison

This directory contains three comparative actor-to-actor transport benchmarks:

- `actor_to_actor_transport_maiko.rs`
- `actor_to_actor_transport_actix.rs`
- `actor_to_actor_transport_ractor.rs`

They all benchmark the same matrix:

- message counts: `1_000`, `10_000`, `100_000`
- payload sizes: `32B`, `256B`, `1KiB`, `4KiB`

Criterion reports throughput in messages/sec.

## Important framing

This comparison is useful, but it is not perfectly apples-to-apples.

`actix` and `ractor` are being exercised here as direct actor-to-actor transport systems. Maiko is not. Maiko routes messages through a broker, which means delivery is fundamentally a two-step path:

1. producer -> broker
2. broker -> subscriber

That architectural choice is deliberate. It gives Maiko topic-based routing, looser coupling, and a more detached event-driven model, but it also means Maiko should not be expected to compete with direct-message runtimes on raw actor-to-actor throughput.

Relatedly, Maiko is unlikely to reach `1M` events/sec in this style of benchmark, and that is not a project goal. Maiko is optimized primarily for ergonomics and a detached architecture. Pure transport performance is secondary.

## How to run

Run the full comparison from the repository root:

```sh
just bench-a2a-compare
```

That target executes:

```sh
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_maiko -- --noplot
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_actix -- --noplot
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_ractor -- --noplot
uv run --project scripts scripts/bench_a2a_compare.py
```

If you want to run the benches individually:

```sh
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_maiko -- --noplot
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_actix -- --noplot
cargo bench --profile bench -p maiko --bench actor_to_actor_transport_ractor -- --noplot
```

## Backpressure notes

Backpressure matters here because Maiko's transport semantics are intentionally bounded and blocking under load. A comparison that ignores that entirely for the other runtimes is easier to implement, but it stops being a like-for-like transport comparison.

### Maiko

Maiko's benchmark uses real backpressure:

- the producer sends via `Context::send(...).await`
- the payload topic uses `OverflowPolicy::Block`

That means the producer naturally waits when downstream capacity is exhausted.

### Actix

Actix also uses an explicit backpressure path in this comparison:

- the producer sends via `Addr::send(...).await`
- the consumer mailbox is explicitly bounded with `ctx.set_mailbox_capacity(MAILBOX_CAPACITY)`

This is the closest Actix analogue to Maiko's blocking send semantics. It is not a perfect apples-to-apples mapping, because Actix `send` is request/response-oriented and carries reply-channel overhead even when the result type is `()`, but it does provide actual capacity-coupled waiting.

### Ractor

No backpressure is implemented in the `ractor` benchmark.

The benchmark currently uses `ActorRef::send_message(...)`, which is a simple push path with no await-based mailbox backpressure. We explored replacing it with factory-based admission control, but that path appears substantially more cumbersome than the other two implementations and is not strictly necessary for keeping the comparative benchmark useful. Because of that, the `ractor` benchmark intentionally remains a simpler no-backpressure transport benchmark for now.

In short:

- Maiko: backpressure enabled
- Actix: backpressure enabled
- Ractor: backpressure intentionally not implemented

## Example results

These are example comparison results from one benchmark run.

### Absolute throughput

```text
╭─────────────┬───────────────┬───────────┬───────────┬───────────┬───────────╮
│ Framework │ N \ Payload │ 32B │ 256B │ 1KiB │ 4KiB │
├─────────────┼───────────────┼───────────┼───────────┼───────────┼───────────┤
│ maiko │ 1,000 │ 813,976 │ 822,254 │ 773,352 │ 720,358 │
│ maiko │ 10,000 │ 812,887 │ 812,014 │ 768,516 │ 643,479 │
│ maiko │ 100,000 │ 778,227 │ 782,469 │ 712,197 │ 713,271 │
│ actix │ 1,000 │ 4,072,205 │ 4,182,926 │ 3,710,988 │ 3,411,817 │
│ actix │ 10,000 │ 4,292,594 │ 4,373,233 │ 3,939,233 │ 3,080,245 │
│ actix │ 100,000 │ 4,276,946 │ 4,197,778 │ 3,911,589 │ 3,130,537 │
│ ractor │ 1,000 │ 3,194,561 │ 3,192,353 │ 2,692,416 │ 2,112,884 │
│ ractor │ 10,000 │ 3,012,807 │ 3,013,401 │ 2,571,302 │ 1,688,873 │
│ ractor │ 100,000 │ 3,069,069 │ 2,843,617 │ 2,434,496 │ 1,702,354 │
╰─────────────┴───────────────┴───────────┴───────────┴───────────┴───────────╯
```

Unit: messages/sec

### Relative throughput vs Maiko

```text
╭─────────────┬───────────────┬───────┬────────┬────────┬────────╮
│ Framework │ N \ Payload │ 32B │ 256B │ 1KiB │ 4KiB │
├─────────────┼───────────────┼───────┼────────┼────────┼────────┤
│ actix │ 1,000 │ 5.00x │ 5.09x │ 4.80x │ 4.74x │
│ actix │ 10,000 │ 5.28x │ 5.39x │ 5.13x │ 4.79x │
│ actix │ 100,000 │ 5.50x │ 5.36x │ 5.49x │ 4.39x │
│ ractor │ 1,000 │ 3.92x │ 3.88x │ 3.48x │ 2.93x │
│ ractor │ 10,000 │ 3.71x │ 3.71x │ 3.35x │ 2.62x │
│ ractor │ 100,000 │ 3.94x │ 3.63x │ 3.42x │ 2.39x │
╰─────────────┴───────────────┴───────┴────────┴────────┴────────╯
```

Unit: multiplier (higher is better)

## Reading the results

From the sample run:

- `actix` is the fastest implementation across the full matrix, roughly `4.4x` to `5.5x` faster than Maiko.
- `ractor` is consistently faster than Maiko as well, roughly `2.4x` to `3.9x` faster in this run.
- both `actix` and `ractor` degrade as payload size increases, but `ractor` drops off more sharply at `4KiB`.
- Maiko is the slowest of the three in raw throughput, but it is also the implementation whose benchmark most directly reflects its bounded, blocking pub/sub transport semantics.
- Maiko should therefore be read here as "brokered event transport with stronger architectural separation", not as "direct actor messaging tuned for maximum throughput".

These numbers should be read as transport benchmark results for the specific benchmark harnesses in this directory, not as universal claims about each framework in all workloads.
159 changes: 159 additions & 0 deletions maiko/benches/actor_to_actor_transport_actix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//! Benchmarks end-to-end Actor -> Actor transport throughput in `actix`.
//!
//! Matrix:
//! - message count: 1_000, 10_000, 100_000
//! - payload size: 32B, 256B, 1KiB, 4KiB
//!
//! Throughput unit is messages/sec.
//!
//! Backpressure is implemented at the producer send site via
//! `Addr::send(...).await`, with the consumer mailbox bounded by
//! `ctx.set_mailbox_capacity(MAILBOX_CAPACITY)`.
//!
//! Run with:
//! `cargo bench -p maiko --bench actor_to_actor_transport_actix -- --noplot`

use std::{hint::black_box, time::Duration};

use actix::prelude::*;
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use tokio::{sync::oneshot, time::Instant};

const MAILBOX_CAPACITY: usize = 128;

#[derive(Message)]
#[rtype(result = "()")]
struct BenchMessage(Vec<u8>);

#[derive(Message)]
#[rtype(result = "()")]
struct Start;

/// Benchmark producer actor.
/// It sends `remaining` payload messages as fast as possible after `Start`.
struct Producer {
consumer: Addr<Consumer>,
remaining: usize,
payload_template: Vec<u8>,
}

impl Actor for Producer {
type Context = Context<Self>;
}

impl Handler<Start> for Producer {
type Result = ResponseActFuture<Self, ()>;

fn handle(&mut self, _message: Start, _ctx: &mut Self::Context) -> Self::Result {
let consumer = self.consumer.clone();
let remaining = self.remaining;
let payload_template = self.payload_template.clone();

Box::pin(
async move {
for _ in 0..remaining {
consumer
.send(BenchMessage(payload_template.clone()))
.await
.expect("consumer mailbox should stay available");
}
}
.into_actor(self)
.map(|_, _actor, ctx| ctx.stop()),
)
}
}

/// Benchmark consumer actor.
/// It counts received messages and signals completion once `target` is reached.
struct Consumer {
received: usize,
target: usize,
done_tx: Option<oneshot::Sender<()>>,
}

impl Actor for Consumer {
type Context = Context<Self>;

fn started(&mut self, ctx: &mut Self::Context) {
ctx.set_mailbox_capacity(MAILBOX_CAPACITY);
}
}

impl Handler<BenchMessage> for Consumer {
type Result = ();

fn handle(&mut self, message: BenchMessage, ctx: &mut Self::Context) -> Self::Result {
black_box(message.0.len());
self.received += 1;
if self.received == self.target {
if let Some(done_tx) = self.done_tx.take() {
let _ = done_tx.send(());
}
ctx.stop();
}
}
}

fn run_round(message_count: usize, payload_size: usize) -> Duration {
let system = System::new();
system.block_on(async move {
let (done_tx, done_rx) = oneshot::channel::<()>();
let payload_template = vec![0_u8; payload_size];

let consumer = Consumer {
received: 0,
target: message_count,
done_tx: Some(done_tx),
}
.start();

let producer = Producer {
consumer,
remaining: message_count,
payload_template,
}
.start();

let start = Instant::now();
producer.do_send(Start);
done_rx
.await
.expect("consumer should signal completion for each round");
start.elapsed()
})
}

fn bench_actor_to_actor_transport_actix(c: &mut Criterion) {
let mut group = c.benchmark_group("actor_to_actor_transport_actix");
group.sample_size(10);
group.warm_up_time(Duration::from_millis(500));
group.measurement_time(Duration::from_secs(1));

let message_counts = [1_000_usize, 10_000, 100_000];
let payload_sizes = [32_usize, 256, 1_024, 4_096];

for &message_count in &message_counts {
for &payload_size in &payload_sizes {
group.throughput(Throughput::Elements(message_count as u64));
group.bench_with_input(
BenchmarkId::new(format!("{payload_size}B"), message_count),
&(message_count, payload_size),
|b, &(message_count, payload_size)| {
b.iter_custom(|iters| {
let mut total = Duration::ZERO;
for _ in 0..iters {
total += run_round(message_count, payload_size);
}
total
});
},
);
}
}

group.finish();
}

criterion_group!(benches, bench_actor_to_actor_transport_actix);
criterion_main!(benches);
Loading
Loading