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 use std::sync::mpsc::{channel, Sender, Receiver};
19 use std::thread::Thread;
20 use std::time::Duration;
28 fn server(requests: &Receiver<request>, responses: &Sender<uint>) {
29 let mut count: uint = 0;
32 match requests.recv() {
33 Ok(request::get_count) => { responses.send(count.clone()); }
34 Ok(request::bytes(b)) => {
35 //println!("server: received {} bytes", b);
38 Err(..) => { done = true; }
42 responses.send(count).unwrap();
43 //println!("server exiting");
46 fn run(args: &[String]) {
47 let (to_parent, from_child) = channel();
49 let size = args[1].parse::<uint>().unwrap();
50 let workers = args[2].parse::<uint>().unwrap();
52 let mut result = None;
53 let mut to_parent = Some(to_parent);
54 let dur = Duration::span(|| {
55 let to_parent = to_parent.take().unwrap();
56 let mut worker_results = Vec::new();
57 let from_parent = if workers == 1 {
58 let (to_child, from_parent) = channel();
59 worker_results.push(Thread::scoped(move|| {
60 for _ in 0u..size / workers {
61 //println!("worker {}: sending {} bytes", i, num_bytes);
62 to_child.send(request::bytes(num_bytes));
64 //println!("worker {} exiting", i);
68 let (to_child, from_parent) = channel();
69 for _ in 0u..workers {
70 let to_child = to_child.clone();
71 worker_results.push(Thread::scoped(move|| {
72 for _ in 0u..size / workers {
73 //println!("worker {}: sending {} bytes", i, num_bytes);
74 to_child.send(request::bytes(num_bytes));
76 //println!("worker {} exiting", i);
81 Thread::spawn(move|| {
82 server(&from_parent, &to_parent);
85 for r in worker_results {
89 //println!("sending stop message");
90 //to_child.send(stop);
92 result = Some(from_child.recv().unwrap());
94 let result = result.unwrap();
95 print!("Count is {}\n", result);
96 print!("Test took {} ms\n", dur.num_milliseconds());
97 let thruput = ((size / workers * workers) as f64) / (dur.num_milliseconds() as f64);
98 print!("Throughput={} per sec\n", thruput / 1000.0);
99 assert_eq!(result, num_bytes * size);
103 let args = os::args();
104 let args = if os::getenv("RUST_BENCH").is_some() {
105 vec!("".to_string(), "1000000".to_string(), "8".to_string())
106 } else if args.len() <= 1u {
107 vec!("".to_string(), "10000".to_string(), "4".to_string())
109 args.clone().into_iter().map(|x| x.to_string()).collect()
112 println!("{:?}", args);