summaryrefslogtreecommitdiffstats
path: root/third_party/rust/futures-0.1.31/benches/futures_unordered.rs
blob: c922df55412fb787228c28043b1dd71e87e87901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#![feature(test)]

extern crate futures;
extern crate test;

use futures::*;
use futures::stream::FuturesUnordered;
use futures::sync::oneshot;

use test::Bencher;

use std::collections::VecDeque;
use std::thread;

#[bench]
fn oneshots(b: &mut Bencher) {
    const NUM: usize = 10_000;

    b.iter(|| {
        let mut txs = VecDeque::with_capacity(NUM);
        let mut rxs = FuturesUnordered::new();

        for _ in 0..NUM {
            let (tx, rx) = oneshot::channel();
            txs.push_back(tx);
            rxs.push(rx);
        }

        thread::spawn(move || {
            while let Some(tx) = txs.pop_front() {
                let _ = tx.send("hello");
            }
        });

        future::lazy(move || {
            loop {
                if let Ok(Async::Ready(None)) = rxs.poll() {
                    return Ok::<(), ()>(());
                }
            }
        }).wait().unwrap();
    });
}