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.
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.
11 // A port of the simplistic benchmark from
13 // http://github.com/PaulKeeble/ScalaVErlangAgents
15 // I *think* it's the same, more or less.
17 // This version uses pipes with a shared send endpoint. It should have
18 // different scalability characteristics compared to the select
21 #![feature(duration, duration_span)]
23 use std::sync::mpsc::{channel, Sender, Receiver};
26 use std::time::Duration;
28 fn move_out<T>(_x: T) {}
36 fn server(requests: &Receiver<request>, responses: &Sender<usize>) {
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);
46 Err(..) => { done = true; }
50 responses.send(count).unwrap();
51 //println!("server exiting");
54 fn run(args: &[String]) {
55 let (to_parent, from_child) = channel();
56 let (to_child, from_parent) = channel();
58 let size = args[1].parse::<usize>().unwrap();
59 let workers = args[2].parse::<usize>().unwrap();
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();
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();
73 //println!("worker {} exiting", i);
76 thread::spawn(move|| {
77 server(&from_parent, &to_parent);
80 for r in worker_results {
84 //println!("sending stop message");
85 to_child.send(request::stop).unwrap();
87 result = Some(from_child.recv().unwrap());
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.as_secs() as f64);
93 print!("Throughput={} per sec\n", thruput);
94 assert_eq!(result, num_bytes * size);
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())
104 args.map(|x| x.to_string()).collect()
107 println!("{:?}", args);