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