| 1 | #![feature (test)] |
| 2 | |
| 3 | extern crate test; |
| 4 | use crate::test::Bencher; |
| 5 | |
| 6 | use futures::channel::oneshot; |
| 7 | use futures::executor::block_on; |
| 8 | use futures::future; |
| 9 | use futures::stream::{FuturesUnordered, StreamExt}; |
| 10 | use futures::task::Poll; |
| 11 | use std::collections::VecDeque; |
| 12 | use std::thread; |
| 13 | |
| 14 | #[bench] |
| 15 | fn oneshots(b: &mut Bencher) { |
| 16 | const NUM: usize = 10_000; |
| 17 | |
| 18 | b.iter(|| { |
| 19 | let mut txs = VecDeque::with_capacity(NUM); |
| 20 | let mut rxs = FuturesUnordered::new(); |
| 21 | |
| 22 | for _ in 0..NUM { |
| 23 | let (tx, rx) = oneshot::channel(); |
| 24 | txs.push_back(tx); |
| 25 | rxs.push(rx); |
| 26 | } |
| 27 | |
| 28 | thread::spawn(move || { |
| 29 | while let Some(tx) = txs.pop_front() { |
| 30 | let _ = tx.send("hello" ); |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | block_on(future::poll_fn(move |cx| { |
| 35 | loop { |
| 36 | if let Poll::Ready(None) = rxs.poll_next_unpin(cx) { |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | Poll::Ready(()) |
| 41 | })) |
| 42 | }); |
| 43 | } |
| 44 | |