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