]> git.lizzy.rs Git - rust.git/blob - src/test/bench/msgsend-ring-mutex-arcs.rs
Auto merge of #28827 - thepowersgang:unsafe-const-fn-2, r=Aatch
[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 threads 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
20 #![feature(duration_span)]
21
22 use std::env;
23 use std::sync::{Arc, Mutex, Condvar};
24 use std::time::Duration;
25 use std::thread;
26
27 // A poor man's pipe.
28 type pipe = Arc<(Mutex<Vec<usize>>, Condvar)>;
29
30 fn send(p: &pipe, msg: usize) {
31     let &(ref lock, ref cond) = &**p;
32     let mut arr = lock.lock().unwrap();
33     arr.push(msg);
34     cond.notify_one();
35 }
36 fn recv(p: &pipe) -> usize {
37     let &(ref lock, ref cond) = &**p;
38     let mut arr = lock.lock().unwrap();
39     while arr.is_empty() {
40         arr = cond.wait(arr).unwrap();
41     }
42     arr.pop().unwrap()
43 }
44
45 fn init() -> (pipe,pipe) {
46     let m = Arc::new((Mutex::new(Vec::new()), Condvar::new()));
47     ((&m).clone(), m)
48 }
49
50
51 fn thread_ring(i: usize, count: usize, num_chan: pipe, num_port: pipe) {
52     let mut num_chan = Some(num_chan);
53     let mut num_port = Some(num_port);
54     // Send/Receive lots of messages.
55     for j in 0..count {
56         //println!("thread %?, iter %?", i, j);
57         let num_chan2 = num_chan.take().unwrap();
58         let num_port2 = num_port.take().unwrap();
59         send(&num_chan2, i * j);
60         num_chan = Some(num_chan2);
61         let _n = recv(&num_port2);
62         //log(error, _n);
63         num_port = Some(num_port2);
64     };
65 }
66
67 fn main() {
68     let args = env::args();
69     let args = if env::var_os("RUST_BENCH").is_some() {
70         vec!("".to_string(), "100".to_string(), "10000".to_string())
71     } else if args.len() <= 1 {
72         vec!("".to_string(), "10".to_string(), "100".to_string())
73     } else {
74         args.collect()
75     };
76
77     let num_tasks = args[1].parse::<usize>().unwrap();
78     let msg_per_task = args[2].parse::<usize>().unwrap();
79
80     let (num_chan, num_port) = init();
81
82     let mut p = Some((num_chan, num_port));
83     let dur = Duration::span(|| {
84         let (mut num_chan, num_port) = p.take().unwrap();
85
86         // create the ring
87         let mut futures = Vec::new();
88
89         for i in 1..num_tasks {
90             //println!("spawning %?", i);
91             let (new_chan, num_port) = init();
92             let num_chan_2 = num_chan.clone();
93             let new_future = thread::spawn(move|| {
94                 thread_ring(i, msg_per_task, num_chan_2, num_port)
95             });
96             futures.push(new_future);
97             num_chan = new_chan;
98         };
99
100         // do our iteration
101         thread_ring(0, msg_per_task, num_chan, num_port);
102
103         // synchronize
104         for f in futures {
105             f.join().unwrap()
106         }
107     });
108
109     // all done, report stats.
110     let num_msgs = num_tasks * msg_per_task;
111     let rate = (num_msgs as f64) / (dur.as_secs() as f64);
112
113     println!("Sent {} messages in {:?}", num_msgs, dur);
114     println!("  {} messages / second", rate);
115     println!("  {} μs / message", 1000000. / rate);
116 }