]> git.lizzy.rs Git - rust.git/blob - src/test/bench/msgsend-pipes.rs
Honor hidden doc attribute of derivable trait methods
[rust.git] / src / test / bench / msgsend-pipes.rs
1 // Copyright 2012 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 // A port of the simplistic benchmark from
12 //
13 //    http://github.com/PaulKeeble/ScalaVErlangAgents
14 //
15 // I *think* it's the same, more or less.
16
17 extern crate time;
18
19 use std::os;
20 use std::task;
21 use std::uint;
22
23 fn move_out<T>(_x: T) {}
24
25 enum request {
26     get_count,
27     bytes(uint),
28     stop
29 }
30
31 fn server(requests: &Receiver<request>, responses: &Sender<uint>) {
32     let mut count: uint = 0;
33     let mut done = false;
34     while !done {
35         match requests.recv_opt() {
36           Ok(get_count) => { responses.send(count.clone()); }
37           Ok(bytes(b)) => {
38             //println!("server: received {:?} bytes", b);
39             count += b;
40           }
41           Err(..) => { done = true; }
42           _ => { }
43         }
44     }
45     responses.send(count);
46     //println!("server exiting");
47 }
48
49 fn run(args: &[~str]) {
50     let (to_parent, from_child) = channel();
51
52     let size = from_str::<uint>(args[1]).unwrap();
53     let workers = from_str::<uint>(args[2]).unwrap();
54     let num_bytes = 100;
55     let start = time::precise_time_s();
56     let mut worker_results = Vec::new();
57     let from_parent = if workers == 1 {
58         let (to_child, from_parent) = channel();
59         let mut builder = task::task();
60         worker_results.push(builder.future_result());
61         builder.spawn(proc() {
62             for _ in range(0u, size / workers) {
63                 //println!("worker {:?}: sending {:?} bytes", i, num_bytes);
64                 to_child.send(bytes(num_bytes));
65             }
66             //println!("worker {:?} exiting", i);
67         });
68         from_parent
69     } else {
70         let (to_child, from_parent) = channel();
71         for _ in range(0u, workers) {
72             let to_child = to_child.clone();
73             let mut builder = task::task();
74             worker_results.push(builder.future_result());
75             builder.spawn(proc() {
76                 for _ in range(0u, size / workers) {
77                     //println!("worker {:?}: sending {:?} bytes", i, num_bytes);
78                     to_child.send(bytes(num_bytes));
79                 }
80                 //println!("worker {:?} exiting", i);
81             });
82         }
83         from_parent
84     };
85     task::spawn(proc() {
86         server(&from_parent, &to_parent);
87     });
88
89     for r in worker_results.iter() {
90         r.recv();
91     }
92
93     //println!("sending stop message");
94     //to_child.send(stop);
95     //move_out(to_child);
96     let result = from_child.recv();
97     let end = time::precise_time_s();
98     let elapsed = end - start;
99     print!("Count is {:?}\n", result);
100     print!("Test took {:?} seconds\n", elapsed);
101     let thruput = ((size / workers * workers) as f64) / (elapsed as f64);
102     print!("Throughput={} per sec\n", thruput);
103     assert_eq!(result, num_bytes * size);
104 }
105
106 fn main() {
107     let args = os::args();
108     let args = if os::getenv("RUST_BENCH").is_some() {
109         vec!("".to_owned(), "1000000".to_owned(), "8".to_owned())
110     } else if args.len() <= 1u {
111         vec!("".to_owned(), "10000".to_owned(), "4".to_owned())
112     } else {
113         args.clone().move_iter().collect()
114     };
115
116     println!("{:?}", args);
117     run(args.as_slice());
118 }