You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asyncband.apache.org by ti...@apache.org on 2026/08/28 16:05:10 UTC

(asyncband) 01/02: test(mpsc): expand unbounded ecosystem benchmarks

This is an automated email from the ASF dual-hosted git repository.

tisonkun pushed a commit to branch codex/rewrite-unbounded-mpsc
in repository https://gitbox.apache.org/repos/asf/asyncband.git

commit 87c803e7da361994f0a2466ea0381626c883a6e4
Author: tison <wa...@gmail.com>
AuthorDate: Sat Aug 29 00:00:49 2026 +0800

    test(mpsc): expand unbounded ecosystem benchmarks
---
 Cargo.lock                             | 10 ++++
 Cargo.toml                             |  1 +
 benchmarks/Cargo.toml                  |  3 +-
 benchmarks/ecosystem/mpsc/adapters.rs  | 55 ++++++++++++++++++++++
 benchmarks/ecosystem/mpsc/support.rs   |  2 +-
 benchmarks/ecosystem/mpsc/unbounded.rs | 85 ++++++++++++++++++++++++++++++++--
 6 files changed, 151 insertions(+), 5 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index d293387..21f59e1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -97,6 +97,7 @@ dependencies = [
  "async-broadcast",
  "async-channel",
  "asyncband",
+ "crossbeam-channel",
  "divan",
  "flume",
  "pollster",
@@ -254,6 +255,15 @@ dependencies = [
  "url",
 ]
 
+[[package]]
+name = "crossbeam-channel"
+version = "0.5.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d85363c37faeca707aef026efa9f3b34d077bce547e48f770770625c6013679e"
+dependencies = [
+ "crossbeam-utils",
+]
+
 [[package]]
 name = "crossbeam-utils"
 version = "0.8.22"
diff --git a/Cargo.toml b/Cargo.toml
index 11086fb..07401bb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -39,6 +39,7 @@ async-broadcast = { version = "0.7.2" }
 async-channel = { version = "2.5.0" }
 cargo_metadata = { version = "0.23.1" }
 clap = { version = "4.6.5" }
+crossbeam-channel = { version = "0.5.16" }
 divan = { version = "0.1.21" }
 flume = { version = "0.12.0", default-features = false }
 pollster = { version = "1.0.1" }
diff --git a/benchmarks/Cargo.toml b/benchmarks/Cargo.toml
index 15a724f..196897a 100644
--- a/benchmarks/Cargo.toml
+++ b/benchmarks/Cargo.toml
@@ -44,10 +44,11 @@ asyncband = { workspace = true, features = [
   "singleflight",
   "waitgroup",
 ] }
+crossbeam-channel = { workspace = true }
 divan = { workspace = true }
 flume = { workspace = true, features = ["async"] }
 pollster = { workspace = true }
-tokio = { workspace = true, features = ["sync"] }
+tokio = { workspace = true, features = ["rt-multi-thread", "sync"] }
 
 [[bench]]
 harness = false
diff --git a/benchmarks/ecosystem/mpsc/adapters.rs b/benchmarks/ecosystem/mpsc/adapters.rs
index 7e78c21..b065b58 100644
--- a/benchmarks/ecosystem/mpsc/adapters.rs
+++ b/benchmarks/ecosystem/mpsc/adapters.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use std::future::Future;
 use std::task::Context;
 
 use crate::support::poll_ready;
@@ -22,6 +23,7 @@ use crate::support::poll_ready;
 pub struct Asyncband;
 pub struct Tokio;
 pub struct AsyncChannel;
+pub struct Crossbeam;
 pub struct Flume;
 
 pub trait BoundedMpsc: Send + Sync + 'static {
@@ -48,6 +50,10 @@ pub trait UnboundedMpsc: Send + Sync + 'static {
     fn recv_blocking(receiver: &mut Self::Receiver) -> usize;
 }
 
+pub trait AsyncUnboundedMpsc: UnboundedMpsc {
+    fn recv_async(receiver: &mut Self::Receiver) -> impl Future<Output = usize> + Send + '_;
+}
+
 impl BoundedMpsc for Asyncband {
     type Receiver = asyncband::mpsc::BoundedReceiver<usize>;
     type Sender = asyncband::mpsc::BoundedSender<usize>;
@@ -205,6 +211,12 @@ impl UnboundedMpsc for Asyncband {
     }
 }
 
+impl AsyncUnboundedMpsc for Asyncband {
+    async fn recv_async(receiver: &mut Self::Receiver) -> usize {
+        receiver.recv().await.unwrap()
+    }
+}
+
 impl UnboundedMpsc for Tokio {
     type Receiver = tokio::sync::mpsc::UnboundedReceiver<usize>;
     type Sender = tokio::sync::mpsc::UnboundedSender<usize>;
@@ -230,6 +242,12 @@ impl UnboundedMpsc for Tokio {
     }
 }
 
+impl AsyncUnboundedMpsc for Tokio {
+    async fn recv_async(receiver: &mut Self::Receiver) -> usize {
+        receiver.recv().await.unwrap()
+    }
+}
+
 impl UnboundedMpsc for AsyncChannel {
     type Receiver = async_channel::Receiver<usize>;
     type Sender = async_channel::Sender<usize>;
@@ -255,6 +273,37 @@ impl UnboundedMpsc for AsyncChannel {
     }
 }
 
+impl AsyncUnboundedMpsc for AsyncChannel {
+    async fn recv_async(receiver: &mut Self::Receiver) -> usize {
+        receiver.recv().await.unwrap()
+    }
+}
+
+impl UnboundedMpsc for Crossbeam {
+    type Receiver = crossbeam_channel::Receiver<usize>;
+    type Sender = crossbeam_channel::Sender<usize>;
+
+    fn channel() -> (Self::Sender, Self::Receiver) {
+        crossbeam_channel::unbounded()
+    }
+
+    fn send(sender: &Self::Sender, value: usize) {
+        sender.send(value).unwrap();
+    }
+
+    fn try_recv(receiver: &mut Self::Receiver) -> usize {
+        receiver.try_recv().unwrap()
+    }
+
+    fn recv_ready(receiver: &mut Self::Receiver, _context: &mut Context<'_>) -> usize {
+        receiver.try_recv().unwrap()
+    }
+
+    fn recv_blocking(receiver: &mut Self::Receiver) -> usize {
+        receiver.recv().unwrap()
+    }
+}
+
 impl UnboundedMpsc for Flume {
     type Receiver = flume::Receiver<usize>;
     type Sender = flume::Sender<usize>;
@@ -279,3 +328,9 @@ impl UnboundedMpsc for Flume {
         pollster::block_on(receiver.recv_async()).unwrap()
     }
 }
+
+impl AsyncUnboundedMpsc for Flume {
+    async fn recv_async(receiver: &mut Self::Receiver) -> usize {
+        receiver.recv_async().await.unwrap()
+    }
+}
diff --git a/benchmarks/ecosystem/mpsc/support.rs b/benchmarks/ecosystem/mpsc/support.rs
index aea2885..8a08c28 100644
--- a/benchmarks/ecosystem/mpsc/support.rs
+++ b/benchmarks/ecosystem/mpsc/support.rs
@@ -28,7 +28,7 @@ use super::adapters::UnboundedMpsc;
 
 pub const BOUNDED_CAPACITY: usize = 64;
 pub const BATCH_MESSAGES: usize = 16_384;
-pub const PRODUCER_COUNTS: &[usize] = &[1, 2, 4, 8];
+pub const PRODUCER_COUNTS: &[usize] = &[1, 2, 4, 8, 16, 32];
 
 pub trait ConcurrentMpsc: Send + Sync + 'static {
     type Sender: Clone + Send + 'static;
diff --git a/benchmarks/ecosystem/mpsc/unbounded.rs b/benchmarks/ecosystem/mpsc/unbounded.rs
index 6c8ac54..99b9c0a 100644
--- a/benchmarks/ecosystem/mpsc/unbounded.rs
+++ b/benchmarks/ecosystem/mpsc/unbounded.rs
@@ -20,7 +20,9 @@ use divan::black_box;
 use divan::counter::ItemsCount;
 
 use super::adapters::AsyncChannel;
+use super::adapters::AsyncUnboundedMpsc;
 use super::adapters::Asyncband;
+use super::adapters::Crossbeam;
 use super::adapters::Flume;
 use super::adapters::Tokio;
 use super::adapters::UnboundedMpsc;
@@ -30,7 +32,25 @@ use super::support::PRODUCER_COUNTS;
 use super::support::Unbounded;
 use crate::support::bench_context;
 
-#[divan::bench(types = [Asyncband, Tokio, AsyncChannel, Flume])]
+const SEQUENTIAL_MESSAGES: usize = 5_000;
+const ASYNC_PRODUCERS: usize = 5;
+const ASYNC_MESSAGES_PER_PRODUCER: usize = 1_000;
+
+#[divan::bench(types = [Asyncband, Tokio, AsyncChannel, Crossbeam, Flume])]
+fn create<C: UnboundedMpsc>(bencher: Bencher) {
+    bencher.bench_local(|| black_box(C::channel()));
+}
+
+#[divan::bench(types = [Asyncband, Tokio, AsyncChannel, Crossbeam, Flume])]
+fn oneshot<C: UnboundedMpsc>(bencher: Bencher) {
+    bencher.bench_local(|| {
+        let (sender, mut receiver) = C::channel();
+        C::send(&sender, black_box(usize::MAX));
+        black_box(C::try_recv(&mut receiver))
+    });
+}
+
+#[divan::bench(types = [Asyncband, Tokio, AsyncChannel, Crossbeam, Flume])]
 fn ready_round_trip<C: UnboundedMpsc>(bencher: Bencher) {
     let mut context = bench_context();
     let (sender, mut receiver) = C::channel();
@@ -41,7 +61,7 @@ fn ready_round_trip<C: UnboundedMpsc>(bencher: Bencher) {
     });
 }
 
-#[divan::bench(types = [Asyncband, Tokio, AsyncChannel, Flume])]
+#[divan::bench(types = [Asyncband, Tokio, AsyncChannel, Crossbeam, Flume])]
 fn try_round_trip<C: UnboundedMpsc>(bencher: Bencher) {
     let (sender, mut receiver) = C::channel();
 
@@ -52,7 +72,26 @@ fn try_round_trip<C: UnboundedMpsc>(bencher: Bencher) {
 }
 
 #[divan::bench(
-    types = [Asyncband, Tokio, AsyncChannel, Flume],
+    types = [Asyncband, Tokio, AsyncChannel, Crossbeam, Flume],
+    sample_count = 100,
+    sample_size = 1,
+    counter = ItemsCount::new(SEQUENTIAL_MESSAGES),
+)]
+fn sequential<C: UnboundedMpsc>(bencher: Bencher) {
+    bencher.bench_local(|| {
+        let (sender, mut receiver) = C::channel();
+
+        for value in 0..SEQUENTIAL_MESSAGES {
+            C::send(&sender, black_box(value));
+        }
+        for _ in 0..SEQUENTIAL_MESSAGES {
+            black_box(C::try_recv(&mut receiver));
+        }
+    });
+}
+
+#[divan::bench(
+    types = [Asyncband, Tokio, AsyncChannel, Crossbeam, Flume],
     args = PRODUCER_COUNTS,
     sample_count = 20,
     sample_size = 1,
@@ -63,3 +102,43 @@ fn concurrent<C: UnboundedMpsc>(bencher: Bencher, producer_count: usize) {
         .with_inputs(|| ConcurrentBatch::<Unbounded<C>>::new(producer_count))
         .bench_local_refs(|batch| batch.run());
 }
+
+#[divan::bench(
+    types = [Asyncband, Tokio, AsyncChannel, Flume],
+    sample_count = 20,
+    sample_size = 1,
+    counter = ItemsCount::new(ASYNC_PRODUCERS * ASYNC_MESSAGES_PER_PRODUCER),
+)]
+fn async_contention<C: AsyncUnboundedMpsc>(bencher: Bencher) {
+    let runtime = tokio::runtime::Builder::new_multi_thread()
+        .worker_threads(6)
+        .build()
+        .unwrap();
+
+    bencher.bench_local(|| {
+        runtime.block_on(async {
+            let (sender, mut receiver) = C::channel();
+            let workers = (0..ASYNC_PRODUCERS)
+                .map(|producer| {
+                    let sender = sender.clone();
+                    tokio::spawn(async move {
+                        let first = producer * ASYNC_MESSAGES_PER_PRODUCER;
+                        for offset in 0..ASYNC_MESSAGES_PER_PRODUCER {
+                            C::send(&sender, black_box(first + offset));
+                        }
+                    })
+                })
+                .collect::<Vec<_>>();
+            drop(sender);
+
+            let mut checksum = 0usize;
+            for _ in 0..ASYNC_PRODUCERS * ASYNC_MESSAGES_PER_PRODUCER {
+                checksum = checksum.wrapping_add(C::recv_async(&mut receiver).await);
+            }
+            for worker in workers {
+                worker.await.unwrap();
+            }
+            black_box(checksum)
+        })
+    });
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@asyncband.apache.org
For additional commands, e-mail: commits-help@asyncband.apache.org