1 #![feature(test)]
2 
3 extern crate futures;
4 extern crate test;
5 
6 use futures::*;
7 use futures::executor::{Notify, NotifyHandle};
8 use futures::task::Task;
9 
10 use test::Bencher;
11 
notify_noop() -> NotifyHandle12 fn notify_noop() -> NotifyHandle {
13     struct Noop;
14 
15     impl Notify for Noop {
16         fn notify(&self, _id: usize) {}
17     }
18 
19     const NOOP : &'static Noop = &Noop;
20 
21     NotifyHandle::from(NOOP)
22 }
23 
24 #[bench]
task_init(b: &mut Bencher)25 fn task_init(b: &mut Bencher) {
26     const NUM: u32 = 100_000;
27 
28     struct MyFuture {
29         num: u32,
30         task: Option<Task>,
31     };
32 
33     impl Future for MyFuture {
34         type Item = ();
35         type Error = ();
36 
37         fn poll(&mut self) -> Poll<(), ()> {
38             if self.num == NUM {
39                 Ok(Async::Ready(()))
40             } else {
41                 self.num += 1;
42 
43                 if let Some(ref t) = self.task {
44                     if t.will_notify_current() {
45                         t.notify();
46                         return Ok(Async::NotReady);
47                     }
48                 }
49 
50                 let t = task::current();
51                 t.notify();
52                 self.task = Some(t);
53 
54                 Ok(Async::NotReady)
55             }
56         }
57     }
58 
59     let notify = notify_noop();
60 
61     let mut fut = executor::spawn(MyFuture {
62         num: 0,
63         task: None,
64     });
65 
66     b.iter(|| {
67         fut.get_mut().num = 0;
68 
69         while let Ok(Async::NotReady) = fut.poll_future_notify(&notify, 0) {
70         }
71     });
72 }
73