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