]> git.lizzy.rs Git - rust.git/blob - src/test/bench/rt-messaging-ping-pong.rs
doc: remove incomplete sentence
[rust.git] / src / test / bench / rt-messaging-ping-pong.rs
1 // Copyright 2014 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 // file at the top-level directory of this distribution and at
12 // http://rust-lang.org/COPYRIGHT.
13 //
14 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
15 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
16 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
17 // option. This file may not be copied, modified, or distributed
18 // except according to those terms.
19
20 use std::sync::mpsc::channel;
21 use std::os;
22 use std::str::from_str;
23 use std::thread::Thread;
24 use std::uint;
25
26 // This is a simple bench that creates M pairs of tasks. These
27 // tasks ping-pong back and forth over a pair of streams. This is a
28 // canonical message-passing benchmark as it heavily strains message
29 // passing and almost nothing else.
30
31 fn ping_pong_bench(n: uint, m: uint) {
32
33     // Create pairs of tasks that pingpong back and forth.
34     fn run_pair(n: uint) {
35         // Create a stream A->B
36         let (atx, arx) = channel::<()>();
37         // Create a stream B->A
38         let (btx, brx) = channel::<()>();
39
40         Thread::spawn(move|| {
41             let (tx, rx) = (atx, brx);
42             for _ in range(0, n) {
43                 tx.send(());
44                 rx.recv();
45             }
46         }).detach();
47
48         Thread::spawn(move|| {
49             let (tx, rx) = (btx, arx);
50             for _ in range(0, n) {
51                 rx.recv();
52                 tx.send(());
53             }
54         }).detach();
55     }
56
57     for _ in range(0, m) {
58         run_pair(n)
59     }
60 }
61
62
63
64 fn main() {
65
66     let args = os::args();
67     let args = args.as_slice();
68     let n = if args.len() == 3 {
69         from_str::<uint>(args[1].as_slice()).unwrap()
70     } else {
71         10000
72     };
73
74     let m = if args.len() == 3 {
75         from_str::<uint>(args[2].as_slice()).unwrap()
76     } else {
77         4
78     };
79
80     ping_pong_bench(n, m);
81
82 }