]> git.lizzy.rs Git - rust.git/blob - src/test/bench/rt-messaging-ping-pong.rs
auto merge of #14855 : TeXitoi/rust/relicense-shootout-binarytrees, r=brson
[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::os;
21 use std::uint;
22
23 // This is a simple bench that creates M pairs of tasks. These
24 // tasks ping-pong back and forth over a pair of streams. This is a
25 // cannonical message-passing benchmark as it heavily strains message
26 // passing and almost nothing else.
27
28 fn ping_pong_bench(n: uint, m: uint) {
29
30     // Create pairs of tasks that pingpong back and forth.
31     fn run_pair(n: uint) {
32         // Create a stream A->B
33         let (atx, arx) = channel::<()>();
34         // Create a stream B->A
35         let (btx, brx) = channel::<()>();
36
37         spawn(proc() {
38             let (tx, rx) = (atx, brx);
39             for _ in range(0, n) {
40                 tx.send(());
41                 rx.recv();
42             }
43         });
44
45         spawn(proc() {
46             let (tx, rx) = (btx, arx);
47             for _ in range(0, n) {
48                 rx.recv();
49                 tx.send(());
50             }
51         });
52     }
53
54     for _ in range(0, m) {
55         run_pair(n)
56     }
57 }
58
59
60
61 fn main() {
62
63     let args = os::args();
64     let args = args.as_slice();
65     let n = if args.len() == 3 {
66         from_str::<uint>(args[1].as_slice()).unwrap()
67     } else {
68         10000
69     };
70
71     let m = if args.len() == 3 {
72         from_str::<uint>(args[2].as_slice()).unwrap()
73     } else {
74         4
75     };
76
77     ping_pong_bench(n, m);
78
79 }