/// Single producer, single consumer #[bench] fn unbounded_1_tx(b: &mut Bencher) { letmut cx = noop_context();
b.iter(|| { let (tx, mut rx) = mpsc::unbounded();
// 1000 iterations to avoid measuring overhead of initialization // Result should be divided by 1000 for i in0..1000 { // Poll, not ready, park
assert_eq!(Poll::Pending, rx.poll_next_unpin(&mut cx));
UnboundedSender::unbounded_send(&tx, i).unwrap();
// Now poll ready
assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(&mut cx));
}
})
}
let tx: Vec<_> = (0..100).map(|_| tx.clone()).collect();
// 1000 send/recv operations total, result should be divided by 1000 for _ in0..10 { for (i, x) in tx.iter().enumerate() {
assert_eq!(Poll::Pending, rx.poll_next_unpin(&mut cx));
for i in0..1000 {
UnboundedSender::unbounded_send(&tx, i).expect("send"); // No need to create a task, because poll is not going to park.
assert_eq!(Poll::Ready(Some(i)), rx.poll_next_unpin(&mut cx));
}
})
}
/// A Stream that continuously sends incrementing number of the queue struct TestSender {
tx: Sender<u32>,
last: u32, // Last number sent
}
// Could be a Future, it doesn't matter impl Stream for TestSender { type Item = u32;
fn poll_next(mutself: Pin<&mutSelf>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { let this = &mut *self; letmut tx = Pin::new(&mut this.tx);
for i in0..10 { for x in &mut tx { // Send an item
assert_eq!(Poll::Ready(Some(i + 1)), x.poll_next_unpin(&mut cx)); // Then block
assert_eq!(Poll::Pending, x.poll_next_unpin(&mut cx)); // Recv the item
assert_eq!(Poll::Ready(Some(i + 1)), rx.poll_next_unpin(&mut cx));
}
}
})
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.