]> git.lizzy.rs Git - rust.git/blob - src/test/bench/task-perf-one-million.rs
Replace all ~"" with "".to_owned()
[rust.git] / src / test / bench / task-perf-one-million.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test for concurrent tasks
12
13 // ignore-test OOM on linux-32 without opts
14
15 use std::os;
16 use std::task;
17 use std::uint;
18 use std::slice;
19
20 fn calc(children: uint, parent_wait_chan: &Sender<Sender<Sender<int>>>) {
21
22     let wait_ports: Vec<Reciever<Sender<Sender<int>>>> = vec::from_fn(children, |_| {
23         let (wait_port, wait_chan) = stream::<Sender<Sender<int>>>();
24         task::spawn(proc() {
25             calc(children / 2, &wait_chan);
26         });
27         wait_port
28     });
29
30     let child_start_chans: Vec<Sender<Sender<int>>> =
31         wait_ports.move_iter().map(|port| port.recv()).collect();
32
33     let (start_port, start_chan) = stream::<Sender<int>>();
34     parent_wait_chan.send(start_chan);
35     let parent_result_chan: Sender<int> = start_port.recv();
36
37     let child_sum_ports: Vec<Reciever<int>> =
38         child_start_chans.move_iter().map(|child_start_chan| {
39             let (child_sum_port, child_sum_chan) = stream::<int>();
40             child_start_chan.send(child_sum_chan);
41             child_sum_port
42     }).collect();
43
44     let sum = child_sum_ports.move_iter().fold(0, |sum, sum_port| sum + sum_port.recv() );
45
46     parent_result_chan.send(sum + 1);
47 }
48
49 fn main() {
50     let args = os::args();
51     let args = if os::getenv("RUST_BENCH").is_some() {
52         vec!("".to_owned(), "30".to_owned())
53     } else if args.len() <= 1u {
54         vec!("".to_owned(), "10".to_owned())
55     } else {
56         args
57     };
58
59     let children = from_str::<uint>(args[1]).unwrap();
60     let (wait_port, wait_chan) = stream();
61     task::spawn(proc() {
62         calc(children, &wait_chan);
63     });
64
65     let start_chan = wait_port.recv();
66     let (sum_port, sum_chan) = stream::<int>();
67     start_chan.send(sum_chan);
68     let sum = sum_port.recv();
69     println!("How many tasks? {} tasks.", sum);
70 }