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