aboutsummaryrefslogtreecommitdiff
path: root/benches/flatten_unordered.rs
blob: 64d5f9a4e379db4285d9f1ab70c957fd4dd936f4 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#![feature(test)]

extern crate test;
use crate::test::Bencher;

use futures::channel::oneshot;
use futures::executor::block_on;
use futures::future::{self, FutureExt};
use futures::stream::{self, StreamExt};
use futures::task::Poll;
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::unfold(rxs.into_iter(), |mut vals| {
            async {
                if let Some(next) = vals.next() {
                    let val = next.await.unwrap();
                    Some((val, vals))
                } else {
                    None
                }
            }
            .boxed()
        })
        .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(())
        }))
    });
}