]> git.lizzy.rs Git - rust.git/blob - src/test/bench/msgsend-ring-rw-arcs.rs
c615c510465e6783858df0c8f1d36b20388248db
[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 extern crate sync;
19 extern crate time;
20
21 use sync::RWArc;
22 use sync::Future;
23 use std::os;
24 use std::uint;
25
26 // A poor man's pipe.
27 type pipe = RWArc<Vec<uint> >;
28
29 fn send(p: &pipe, msg: uint) {
30     p.write_cond(|state, cond| {
31         state.push(msg);
32         cond.signal();
33     })
34 }
35 fn recv(p: &pipe) -> uint {
36     p.write_cond(|state, cond| {
37         while state.is_empty() {
38             cond.wait();
39         }
40         state.pop().unwrap()
41     })
42 }
43
44 fn init() -> (pipe,pipe) {
45     let x = RWArc::new(Vec::new());
46     ((&x).clone(), x)
47 }
48
49
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);
61         //log(error, _n);
62         num_port = Some(num_port2);
63     };
64 }
65
66 fn main() {
67     let args = os::args();
68     let args = if os::getenv("RUST_BENCH").is_some() {
69         vec!(~"", ~"100", ~"10000")
70     } else if args.len() <= 1u {
71         vec!(~"", ~"10", ~"100")
72     } else {
73         args.clone()
74     };
75
76     let num_tasks = from_str::<uint>(args[1]).unwrap();
77     let msg_per_task = from_str::<uint>(args[2]).unwrap();
78
79     let (mut num_chan, num_port) = init();
80
81     let start = time::precise_time_s();
82
83     // create the ring
84     let mut futures = Vec::new();
85
86     for i in range(1u, num_tasks) {
87         //println!("spawning %?", i);
88         let (new_chan, num_port) = init();
89         let num_chan_2 = num_chan.clone();
90         let new_future = Future::spawn(proc() {
91             thread_ring(i, msg_per_task, num_chan_2, num_port)
92         });
93         futures.push(new_future);
94         num_chan = new_chan;
95     };
96
97     // do our iteration
98     thread_ring(0, msg_per_task, num_chan, num_port);
99
100     // synchronize
101     for f in futures.mut_iter() {
102         let _ = f.get();
103     }
104
105     let stop = time::precise_time_s();
106
107     // all done, report stats.
108     let num_msgs = num_tasks * msg_per_task;
109     let elapsed = (stop - start);
110     let rate = (num_msgs as f64) / elapsed;
111
112     println!("Sent {} messages in {} seconds", num_msgs, elapsed);
113     println!("  {} messages / second", rate);
114     println!("  {} μs / message", 1000000. / rate);
115 }