]> git.lizzy.rs Git - rust.git/blob - src/test/bench/msgsend-pipes.rs
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[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 use std::sync::mpsc::{channel, Sender, Receiver};
18 use std::os;
19 use std::thread::Thread;
20 use std::time::Duration;
21
22 enum request {
23     get_count,
24     bytes(uint),
25     stop
26 }
27
28 fn server(requests: &Receiver<request>, responses: &Sender<uint>) {
29     let mut count: uint = 0;
30     let mut done = false;
31     while !done {
32         match requests.recv() {
33           Ok(request::get_count) => { responses.send(count.clone()); }
34           Ok(request::bytes(b)) => {
35             //println!("server: received {} bytes", b);
36             count += b;
37           }
38           Err(..) => { done = true; }
39           _ => { }
40         }
41     }
42     responses.send(count).unwrap();
43     //println!("server exiting");
44 }
45
46 fn run(args: &[String]) {
47     let (to_parent, from_child) = channel();
48
49     let size = args[1].parse::<uint>().unwrap();
50     let workers = args[2].parse::<uint>().unwrap();
51     let num_bytes = 100;
52     let mut result = None;
53     let mut to_parent = Some(to_parent);
54     let dur = Duration::span(|| {
55         let to_parent = to_parent.take().unwrap();
56         let mut worker_results = Vec::new();
57         let from_parent = if workers == 1 {
58             let (to_child, from_parent) = channel();
59             worker_results.push(Thread::scoped(move|| {
60                 for _ in 0u..size / workers {
61                     //println!("worker {}: sending {} bytes", i, num_bytes);
62                     to_child.send(request::bytes(num_bytes));
63                 }
64                 //println!("worker {} exiting", i);
65             }));
66             from_parent
67         } else {
68             let (to_child, from_parent) = channel();
69             for _ in 0u..workers {
70                 let to_child = to_child.clone();
71                 worker_results.push(Thread::scoped(move|| {
72                     for _ in 0u..size / workers {
73                         //println!("worker {}: sending {} bytes", i, num_bytes);
74                         to_child.send(request::bytes(num_bytes));
75                     }
76                     //println!("worker {} exiting", i);
77                 }));
78             }
79             from_parent
80         };
81         Thread::spawn(move|| {
82             server(&from_parent, &to_parent);
83         });
84
85         for r in worker_results {
86             let _ = r.join();
87         }
88
89         //println!("sending stop message");
90         //to_child.send(stop);
91         //move_out(to_child);
92         result = Some(from_child.recv().unwrap());
93     });
94     let result = result.unwrap();
95     print!("Count is {}\n", result);
96     print!("Test took {} ms\n", dur.num_milliseconds());
97     let thruput = ((size / workers * workers) as f64) / (dur.num_milliseconds() as f64);
98     print!("Throughput={} per sec\n", thruput / 1000.0);
99     assert_eq!(result, num_bytes * size);
100 }
101
102 fn main() {
103     let args = os::args();
104     let args = if os::getenv("RUST_BENCH").is_some() {
105         vec!("".to_string(), "1000000".to_string(), "8".to_string())
106     } else if args.len() <= 1u {
107         vec!("".to_string(), "10000".to_string(), "4".to_string())
108     } else {
109         args.clone().into_iter().map(|x| x.to_string()).collect()
110     };
111
112     println!("{:?}", args);
113     run(&args);
114 }