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