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