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 // This test creates a bunch of tasks that simultaneously send to each
12 // other in a ring. The messages should all be basically
14 // This is like msgsend-ring-pipes but adapted to use Arcs.
16 // This also serves as a pipes test, because Arcs are implemented with pipes.
20 use std::sync::{Arc, Future, Mutex};
25 type pipe = Arc<Mutex<Vec<uint>>>;
27 fn send(p: &pipe, msg: uint) {
28 let mut arr = p.lock();
32 fn recv(p: &pipe) -> uint {
33 let mut arr = p.lock();
34 while arr.is_empty() {
40 fn init() -> (pipe,pipe) {
41 let m = Arc::new(Mutex::new(Vec::new()));
46 fn thread_ring(i: uint, count: uint, num_chan: pipe, num_port: pipe) {
47 let mut num_chan = Some(num_chan);
48 let mut num_port = Some(num_port);
49 // Send/Receive lots of messages.
50 for j in range(0u, count) {
51 //println!("task %?, iter %?", i, j);
52 let num_chan2 = num_chan.take_unwrap();
53 let num_port2 = num_port.take_unwrap();
54 send(&num_chan2, i * j);
55 num_chan = Some(num_chan2);
56 let _n = recv(&num_port2);
58 num_port = Some(num_port2);
63 let args = os::args();
64 let args = if os::getenv("RUST_BENCH").is_some() {
65 vec!("".to_string(), "100".to_string(), "10000".to_string())
66 } else if args.len() <= 1u {
67 vec!("".to_string(), "10".to_string(), "100".to_string())
69 args.clone().move_iter().collect()
72 let num_tasks = from_str::<uint>(args.get(1).as_slice()).unwrap();
73 let msg_per_task = from_str::<uint>(args.get(2).as_slice()).unwrap();
75 let (mut num_chan, num_port) = init();
77 let start = time::precise_time_s();
80 let mut futures = Vec::new();
82 for i in range(1u, num_tasks) {
83 //println!("spawning %?", i);
84 let (new_chan, num_port) = init();
85 let num_chan_2 = num_chan.clone();
86 let new_future = Future::spawn(proc() {
87 thread_ring(i, msg_per_task, num_chan_2, num_port)
89 futures.push(new_future);
94 thread_ring(0, msg_per_task, num_chan, num_port);
97 for f in futures.mut_iter() {
101 let stop = time::precise_time_s();
103 // all done, report stats.
104 let num_msgs = num_tasks * msg_per_task;
105 let elapsed = (stop - start);
106 let rate = (num_msgs as f64) / elapsed;
108 println!("Sent {} messages in {} seconds", num_msgs, elapsed);
109 println!(" {} messages / second", rate);
110 println!(" {} μs / message", 1000000. / rate);