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 use std::sync::mpsc::{channel, Sender, Receiver};
23 use std::thread::Thread;
24 use std::time::Duration;
26 fn move_out<T>(_x: T) {}
34 fn server(requests: &Receiver<request>, responses: &Sender<uint>) {
38 match requests.recv() {
39 Ok(request::get_count) => { responses.send(count.clone()).unwrap(); }
40 Ok(request::bytes(b)) => {
41 //println!("server: received {} bytes", b);
44 Err(..) => { done = true; }
48 responses.send(count).unwrap();
49 //println!("server exiting");
52 fn run(args: &[String]) {
53 let (to_parent, from_child) = channel();
54 let (to_child, from_parent) = channel();
56 let size = args[1].parse::<uint>().unwrap();
57 let workers = args[2].parse::<uint>().unwrap();
59 let mut result = None;
60 let mut p = Some((to_child, to_parent, from_parent));
61 let dur = Duration::span(|| {
62 let (to_child, to_parent, from_parent) = p.take().unwrap();
63 let mut worker_results = Vec::new();
64 for _ in 0u..workers {
65 let to_child = to_child.clone();
66 worker_results.push(Thread::scoped(move|| {
67 for _ in 0u..size / workers {
68 //println!("worker {}: sending {} bytes", i, num_bytes);
69 to_child.send(request::bytes(num_bytes)).unwrap();
71 //println!("worker {} exiting", i);
74 Thread::spawn(move|| {
75 server(&from_parent, &to_parent);
78 for r in worker_results {
82 //println!("sending stop message");
83 to_child.send(request::stop).unwrap();
85 result = Some(from_child.recv().unwrap());
87 let result = result.unwrap();
88 print!("Count is {}\n", result);
89 print!("Test took {} ms\n", dur.num_milliseconds());
90 let thruput = ((size / workers * workers) as f64) / (dur.num_milliseconds() as f64);
91 print!("Throughput={} per sec\n", thruput / 1000.0);
92 assert_eq!(result, num_bytes * size);
96 let args = os::args();
97 let args = if os::getenv("RUST_BENCH").is_some() {
98 vec!("".to_string(), "1000000".to_string(), "10000".to_string())
99 } else if args.len() <= 1u {
100 vec!("".to_string(), "10000".to_string(), "4".to_string())
102 args.into_iter().map(|x| x.to_string()).collect()
105 println!("{:?}", args);
106 run(args.as_slice());