summaryrefslogtreecommitdiffstats
path: root/vendor/futures-util/benches
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/futures-util/benches')
-rw-r--r--vendor/futures-util/benches/bilock.rs68
-rw-r--r--vendor/futures-util/benches/flatten_unordered.rs58
-rw-r--r--vendor/futures-util/benches/select.rs35
3 files changed, 161 insertions, 0 deletions
diff --git a/vendor/futures-util/benches/bilock.rs b/vendor/futures-util/benches/bilock.rs
new file mode 100644
index 000000000..013f3351e
--- /dev/null
+++ b/vendor/futures-util/benches/bilock.rs
@@ -0,0 +1,68 @@
+#![feature(test)]
+#![cfg(feature = "bilock")]
+
+extern crate test;
+
+use futures::task::Poll;
+use futures_test::task::noop_context;
+use futures_util::lock::BiLock;
+
+use crate::test::Bencher;
+
+#[bench]
+fn contended(b: &mut Bencher) {
+ let mut context = noop_context();
+
+ b.iter(|| {
+ let (x, y) = BiLock::new(1);
+
+ for _ in 0..1000 {
+ let x_guard = match x.poll_lock(&mut context) {
+ Poll::Ready(guard) => guard,
+ _ => panic!(),
+ };
+
+ // Try poll second lock while first lock still holds the lock
+ match y.poll_lock(&mut context) {
+ Poll::Pending => (),
+ _ => panic!(),
+ };
+
+ drop(x_guard);
+
+ let y_guard = match y.poll_lock(&mut context) {
+ Poll::Ready(guard) => guard,
+ _ => panic!(),
+ };
+
+ drop(y_guard);
+ }
+ (x, y)
+ });
+}
+
+#[bench]
+fn lock_unlock(b: &mut Bencher) {
+ let mut context = noop_context();
+
+ b.iter(|| {
+ let (x, y) = BiLock::new(1);
+
+ for _ in 0..1000 {
+ let x_guard = match x.poll_lock(&mut context) {
+ Poll::Ready(guard) => guard,
+ _ => panic!(),
+ };
+
+ drop(x_guard);
+
+ let y_guard = match y.poll_lock(&mut context) {
+ Poll::Ready(guard) => guard,
+ _ => panic!(),
+ };
+
+ drop(y_guard);
+ }
+ (x, y)
+ })
+}
diff --git a/vendor/futures-util/benches/flatten_unordered.rs b/vendor/futures-util/benches/flatten_unordered.rs
new file mode 100644
index 000000000..517b2816c
--- /dev/null
+++ b/vendor/futures-util/benches/flatten_unordered.rs
@@ -0,0 +1,58 @@
+#![feature(test)]
+
+extern crate test;
+use crate::test::Bencher;
+
+use futures::channel::oneshot;
+use futures::executor::block_on;
+use futures::future;
+use futures::stream::{self, StreamExt};
+use futures::task::Poll;
+use futures_util::FutureExt;
+use std::collections::VecDeque;
+use std::thread;
+
+#[bench]
+fn oneshot_streams(b: &mut Bencher) {
+ const STREAM_COUNT: usize = 10_000;
+ const STREAM_ITEM_COUNT: usize = 1;
+
+ b.iter(|| {
+ let mut txs = VecDeque::with_capacity(STREAM_COUNT);
+ let mut rxs = Vec::new();
+
+ for _ in 0..STREAM_COUNT {
+ let (tx, rx) = oneshot::channel();
+ txs.push_back(tx);
+ rxs.push(rx);
+ }
+
+ thread::spawn(move || {
+ let mut last = 1;
+ while let Some(tx) = txs.pop_front() {
+ let _ = tx.send(stream::iter(last..last + STREAM_ITEM_COUNT));
+ last += STREAM_ITEM_COUNT;
+ }
+ });
+
+ let mut flatten = stream::iter(rxs)
+ .map(|recv| recv.into_stream().map(|val| val.unwrap()).flatten())
+ .flatten_unordered(None);
+
+ block_on(future::poll_fn(move |cx| {
+ let mut count = 0;
+ loop {
+ match flatten.poll_next_unpin(cx) {
+ Poll::Ready(None) => break,
+ Poll::Ready(Some(_)) => {
+ count += 1;
+ }
+ _ => {}
+ }
+ }
+ assert_eq!(count, STREAM_COUNT * STREAM_ITEM_COUNT);
+
+ Poll::Ready(())
+ }))
+ });
+}
diff --git a/vendor/futures-util/benches/select.rs b/vendor/futures-util/benches/select.rs
new file mode 100644
index 000000000..5410a9529
--- /dev/null
+++ b/vendor/futures-util/benches/select.rs
@@ -0,0 +1,35 @@
+#![feature(test)]
+
+extern crate test;
+use crate::test::Bencher;
+
+use futures::executor::block_on;
+use futures::stream::{repeat, select, StreamExt};
+
+#[bench]
+fn select_streams(b: &mut Bencher) {
+ const STREAM_COUNT: usize = 10_000;
+
+ b.iter(|| {
+ let stream1 = repeat(1).take(STREAM_COUNT);
+ let stream2 = repeat(2).take(STREAM_COUNT);
+ let stream3 = repeat(3).take(STREAM_COUNT);
+ let stream4 = repeat(4).take(STREAM_COUNT);
+ let stream5 = repeat(5).take(STREAM_COUNT);
+ let stream6 = repeat(6).take(STREAM_COUNT);
+ let stream7 = repeat(7).take(STREAM_COUNT);
+ let count = block_on(async {
+ let count = select(
+ stream1,
+ select(
+ stream2,
+ select(stream3, select(stream4, select(stream5, select(stream6, stream7)))),
+ ),
+ )
+ .count()
+ .await;
+ count
+ });
+ assert_eq!(count, STREAM_COUNT * 7);
+ });
+}