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