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