]> git.lizzy.rs Git - rust.git/blob - src/test/bench/msgsend-pipes.rs
Doc says to avoid mixing allocator instead of forbiding it
[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 extern crate debug;
19
20 use std::os;
21 use std::task;
22 use std::uint;
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           Ok(get_count) => { responses.send(count.clone()); }
38           Ok(bytes(b)) => {
39             //println!("server: received {:?} bytes", b);
40             count += b;
41           }
42           Err(..) => { done = true; }
43           _ => { }
44         }
45     }
46     responses.send(count);
47     //println!("server exiting");
48 }
49
50 fn run(args: &[String]) {
51     let (to_parent, from_child) = channel();
52
53     let size = from_str::<uint>(args[1].as_slice()).unwrap();
54     let workers = from_str::<uint>(args[2].as_slice()).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         worker_results.push(task::try_future(proc() {
61             for _ in range(0u, size / workers) {
62                 //println!("worker {:?}: sending {:?} bytes", i, num_bytes);
63                 to_child.send(bytes(num_bytes));
64             }
65             //println!("worker {:?} exiting", i);
66         }));
67         from_parent
68     } else {
69         let (to_child, from_parent) = channel();
70         for _ in range(0u, workers) {
71             let to_child = to_child.clone();
72             worker_results.push(task::try_future(proc() {
73                 for _ in range(0u, size / workers) {
74                     //println!("worker {:?}: sending {:?} bytes", i, num_bytes);
75                     to_child.send(bytes(num_bytes));
76                 }
77                 //println!("worker {:?} exiting", i);
78             }));
79         }
80         from_parent
81     };
82     task::spawn(proc() {
83         server(&from_parent, &to_parent);
84     });
85
86     for r in worker_results.move_iter() {
87         r.unwrap();
88     }
89
90     //println!("sending stop message");
91     //to_child.send(stop);
92     //move_out(to_child);
93     let result = from_child.recv();
94     let end = time::precise_time_s();
95     let elapsed = end - start;
96     print!("Count is {:?}\n", result);
97     print!("Test took {:?} seconds\n", elapsed);
98     let thruput = ((size / workers * workers) as f64) / (elapsed as f64);
99     print!("Throughput={} per sec\n", thruput);
100     assert_eq!(result, num_bytes * size);
101 }
102
103 fn main() {
104     let args = os::args();
105     let args = if os::getenv("RUST_BENCH").is_some() {
106         vec!("".to_string(), "1000000".to_string(), "8".to_string())
107     } else if args.len() <= 1u {
108         vec!("".to_string(), "10000".to_string(), "4".to_string())
109     } else {
110         args.clone().move_iter().map(|x| x.to_string()).collect()
111     };
112
113     println!("{:?}", args);
114     run(args.as_slice());
115 }