summaryrefslogtreecommitdiffstats
path: root/vendor/futures-executor/benches
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/futures-executor/benches
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/futures-executor/benches')
-rw-r--r--vendor/futures-executor/benches/thread_notify.rs109
1 files changed, 109 insertions, 0 deletions
diff --git a/vendor/futures-executor/benches/thread_notify.rs b/vendor/futures-executor/benches/thread_notify.rs
new file mode 100644
index 000000000..88d0447cf
--- /dev/null
+++ b/vendor/futures-executor/benches/thread_notify.rs
@@ -0,0 +1,109 @@
+#![feature(test)]
+
+extern crate test;
+use crate::test::Bencher;
+
+use futures::executor::block_on;
+use futures::future::Future;
+use futures::task::{Context, Poll, Waker};
+use std::pin::Pin;
+
+#[bench]
+fn thread_yield_single_thread_one_wait(b: &mut Bencher) {
+ const NUM: usize = 10_000;
+
+ struct Yield {
+ rem: usize,
+ }
+
+ impl Future for Yield {
+ type Output = ();
+
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ if self.rem == 0 {
+ Poll::Ready(())
+ } else {
+ self.rem -= 1;
+ cx.waker().wake_by_ref();
+ Poll::Pending
+ }
+ }
+ }
+
+ b.iter(|| {
+ let y = Yield { rem: NUM };
+ block_on(y);
+ });
+}
+
+#[bench]
+fn thread_yield_single_thread_many_wait(b: &mut Bencher) {
+ const NUM: usize = 10_000;
+
+ struct Yield {
+ rem: usize,
+ }
+
+ impl Future for Yield {
+ type Output = ();
+
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ if self.rem == 0 {
+ Poll::Ready(())
+ } else {
+ self.rem -= 1;
+ cx.waker().wake_by_ref();
+ Poll::Pending
+ }
+ }
+ }
+
+ b.iter(|| {
+ for _ in 0..NUM {
+ let y = Yield { rem: 1 };
+ block_on(y);
+ }
+ });
+}
+
+#[bench]
+fn thread_yield_multi_thread(b: &mut Bencher) {
+ use std::sync::mpsc;
+ use std::thread;
+
+ const NUM: usize = 1_000;
+
+ let (tx, rx) = mpsc::sync_channel::<Waker>(10_000);
+
+ struct Yield {
+ rem: usize,
+ tx: mpsc::SyncSender<Waker>,
+ }
+ impl Unpin for Yield {}
+
+ impl Future for Yield {
+ type Output = ();
+
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ if self.rem == 0 {
+ Poll::Ready(())
+ } else {
+ self.rem -= 1;
+ self.tx.send(cx.waker().clone()).unwrap();
+ Poll::Pending
+ }
+ }
+ }
+
+ thread::spawn(move || {
+ while let Ok(task) = rx.recv() {
+ task.wake();
+ }
+ });
+
+ b.iter(move || {
+ let y = Yield { rem: NUM, tx: tx.clone() };
+
+ block_on(y);
+ });
+}