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.
18 // no-pretty-expanded FIXME #15189
19 // ignore-lexer-test FIXME #15679
22 use std::sync::{Arc, Future, Mutex, Condvar};
23 use std::time::Duration;
27 type pipe = Arc<(Mutex<Vec<uint>>, Condvar)>;
29 fn send(p: &pipe, msg: uint) {
30 let &(ref lock, ref cond) = &**p;
31 let mut arr = lock.lock();
35 fn recv(p: &pipe) -> uint {
36 let &(ref lock, ref cond) = &**p;
37 let mut arr = lock.lock();
38 while arr.is_empty() {
44 fn init() -> (pipe,pipe) {
45 let m = Arc::new((Mutex::new(Vec::new()), Condvar::new()));
50 fn thread_ring(i: uint, count: uint, num_chan: pipe, num_port: pipe) {
51 let mut num_chan = Some(num_chan);
52 let mut num_port = Some(num_port);
53 // Send/Receive lots of messages.
54 for j in range(0u, count) {
55 //println!("task %?, iter %?", i, j);
56 let num_chan2 = num_chan.take().unwrap();
57 let num_port2 = num_port.take().unwrap();
58 send(&num_chan2, i * j);
59 num_chan = Some(num_chan2);
60 let _n = recv(&num_port2);
62 num_port = Some(num_port2);
67 let args = os::args();
68 let args = if os::getenv("RUST_BENCH").is_some() {
69 vec!("".to_string(), "100".to_string(), "10000".to_string())
70 } else if args.len() <= 1u {
71 vec!("".to_string(), "10".to_string(), "100".to_string())
73 args.clone().into_iter().collect()
76 let num_tasks = from_str::<uint>(args[1].as_slice()).unwrap();
77 let msg_per_task = from_str::<uint>(args[2].as_slice()).unwrap();
79 let (mut num_chan, num_port) = init();
81 let mut p = Some((num_chan, num_port));
82 let dur = Duration::span(|| {
83 let (mut num_chan, num_port) = p.take().unwrap();
86 let mut futures = Vec::new();
88 for i in range(1u, num_tasks) {
89 //println!("spawning %?", i);
90 let (new_chan, num_port) = init();
91 let num_chan_2 = num_chan.clone();
92 let new_future = Future::spawn(proc() {
93 thread_ring(i, msg_per_task, num_chan_2, num_port)
95 futures.push(new_future);
100 thread_ring(0, msg_per_task, num_chan, num_port);
103 for f in futures.iter_mut() {
108 // all done, report stats.
109 let num_msgs = num_tasks * msg_per_task;
110 let rate = (num_msgs as f64) / (dur.num_milliseconds() as f64);
112 println!("Sent {} messages in {} ms", num_msgs, dur.num_milliseconds());
113 println!(" {} messages / second", rate / 1000.0);
114 println!(" {} μs / message", 1000000. / rate / 1000.0);