]> git.lizzy.rs Git - rust.git/blob - src/test/bench/msgsend-ring-rw-arcs.rs
auto merge of #14886 : alexcrichton/rust/rt-improvements, r=brson
[rust.git] / src / test / bench / msgsend-ring-rw-arcs.rs
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.
4 //
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.
10
11 // This test creates a bunch of tasks that simultaneously send to each
12 // other in a ring. The messages should all be basically
13 // independent.
14 // This is like msgsend-ring-pipes but adapted to use Arcs.
15
16 // This also serves as a pipes test, because Arcs are implemented with pipes.
17
18 // ignore-pretty FIXME #15189
19
20 extern crate time;
21
22 use std::sync::{RWLock, Arc, Future};
23 use std::os;
24 use std::uint;
25
26 // A poor man's pipe.
27 type pipe = Arc<RWLock<Vec<uint>>>;
28
29 fn send(p: &pipe, msg: uint) {
30     let mut arr = p.write();
31     arr.push(msg);
32     arr.cond.signal();
33 }
34 fn recv(p: &pipe) -> uint {
35     let mut arr = p.write();
36     while arr.is_empty() {
37         arr.cond.wait();
38     }
39     arr.pop().unwrap()
40 }
41
42 fn init() -> (pipe,pipe) {
43     let x = Arc::new(RWLock::new(Vec::new()));
44     ((&x).clone(), x)
45 }
46
47
48 fn thread_ring(i: uint, count: uint, num_chan: pipe, num_port: pipe) {
49     let mut num_chan = Some(num_chan);
50     let mut num_port = Some(num_port);
51     // Send/Receive lots of messages.
52     for j in range(0u, count) {
53         //println!("task %?, iter %?", i, j);
54         let num_chan2 = num_chan.take_unwrap();
55         let num_port2 = num_port.take_unwrap();
56         send(&num_chan2, i * j);
57         num_chan = Some(num_chan2);
58         let _n = recv(&num_port2);
59         //log(error, _n);
60         num_port = Some(num_port2);
61     };
62 }
63
64 fn main() {
65     let args = os::args();
66     let args = if os::getenv("RUST_BENCH").is_some() {
67         vec!("".to_string(), "100".to_string(), "10000".to_string())
68     } else if args.len() <= 1u {
69         vec!("".to_string(), "10".to_string(), "100".to_string())
70     } else {
71         args.clone().move_iter().collect()
72     };
73
74     let num_tasks = from_str::<uint>(args.get(1).as_slice()).unwrap();
75     let msg_per_task = from_str::<uint>(args.get(2).as_slice()).unwrap();
76
77     let (mut num_chan, num_port) = init();
78
79     let start = time::precise_time_s();
80
81     // create the ring
82     let mut futures = Vec::new();
83
84     for i in range(1u, num_tasks) {
85         //println!("spawning %?", i);
86         let (new_chan, num_port) = init();
87         let num_chan_2 = num_chan.clone();
88         let new_future = Future::spawn(proc() {
89             thread_ring(i, msg_per_task, num_chan_2, num_port)
90         });
91         futures.push(new_future);
92         num_chan = new_chan;
93     };
94
95     // do our iteration
96     thread_ring(0, msg_per_task, num_chan, num_port);
97
98     // synchronize
99     for f in futures.mut_iter() {
100         let _ = f.get();
101     }
102
103     let stop = time::precise_time_s();
104
105     // all done, report stats.
106     let num_msgs = num_tasks * msg_per_task;
107     let elapsed = (stop - start);
108     let rate = (num_msgs as f64) / elapsed;
109
110     println!("Sent {} messages in {} seconds", num_msgs, elapsed);
111     println!("  {} messages / second", rate);
112     println!("  {} μs / message", 1000000. / rate);
113 }