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