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 #![feature(duration, duration_span)]
19 use std
::sync
::mpsc
::{channel, Sender, Receiver}
;
22 use std
::time
::Duration
;
30 fn server(requests
: &Receiver
<request
>, responses
: &Sender
<usize>) {
31 let mut count
: usize = 0;
34 match requests
.recv() {
35 Ok(request
::get_count
) => { responses.send(count.clone()); }
36 Ok(request
::bytes(b
)) => {
37 //println!("server: received {} bytes", b);
40 Err(..) => { done = true; }
44 responses
.send(count
).unwrap();
45 //println!("server exiting");
48 fn run(args
: &[String
]) {
49 let (to_parent
, from_child
) = channel();
51 let size
= args
[1].parse
::<usize>().unwrap();
52 let workers
= args
[2].parse
::<usize>().unwrap();
54 let mut result
= None
;
55 let mut to_parent
= Some(to_parent
);
56 let dur
= Duration
::span(|| {
57 let to_parent
= to_parent
.take().unwrap();
58 let mut worker_results
= Vec
::new();
59 let from_parent
= if workers
== 1 {
60 let (to_child
, from_parent
) = channel();
61 worker_results
.push(thread
::spawn(move|| {
62 for _
in 0..size
/ workers
{
63 //println!("worker {}: sending {} bytes", i, num_bytes);
64 to_child
.send(request
::bytes(num_bytes
));
66 //println!("worker {} exiting", i);
70 let (to_child
, from_parent
) = channel();
72 let to_child
= to_child
.clone();
73 worker_results
.push(thread
::spawn(move|| {
74 for _
in 0..size
/ workers
{
75 //println!("worker {}: sending {} bytes", i, num_bytes);
76 to_child
.send(request
::bytes(num_bytes
));
78 //println!("worker {} exiting", i);
83 thread
::spawn(move|| {
84 server(&from_parent
, &to_parent
);
87 for r
in worker_results
{
91 //println!("sending stop message");
92 //to_child.send(stop);
94 result
= Some(from_child
.recv().unwrap());
96 let result
= result
.unwrap();
97 print
!("Count is {}\n", result
);
98 print
!("Test took {}\n", dur
);
99 let thruput
= ((size
/ workers
* workers
) as f64) / (dur
.secs() as f64);
100 print
!("Throughput={} per sec\n", thruput
);
101 assert_eq
!(result
, num_bytes
* size
);
105 let args
= env
::args();
106 let args
= if env
::var_os("RUST_BENCH").is_some() {
107 vec
!("".to_string(), "1000000".to_string(), "8".to_string())
108 } else if args
.len() <= 1 {
109 vec
!("".to_string(), "10000".to_string(), "4".to_string())
111 args
.map(|x
| x
.to_string()).collect()
114 println
!("{:?}", args
);