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