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.
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.
11 // file at the top-level directory of this distribution and at
12 // http://rust-lang.org/COPYRIGHT.
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.
20 use std::sync::mpsc::channel;
22 use std::thread::Thread;
24 // This is a simple bench that creates M pairs of tasks. These
25 // tasks ping-pong back and forth over a pair of streams. This is a
26 // canonical message-passing benchmark as it heavily strains message
27 // passing and almost nothing else.
29 fn ping_pong_bench(n: uint, m: uint) {
31 // Create pairs of tasks that pingpong back and forth.
32 fn run_pair(n: uint) {
33 // Create a channel: A->B
34 let (atx, arx) = channel();
35 // Create a channel: B->A
36 let (btx, brx) = channel();
38 let guard_a = Thread::scoped(move|| {
39 let (tx, rx) = (atx, brx);
46 let guard_b = Thread::scoped(move|| {
47 let (tx, rx) = (btx, arx);
67 let args = os::args();
68 let args = args.as_slice();
69 let n = if args.len() == 3 {
70 args[1].parse::<uint>().unwrap()
75 let m = if args.len() == 3 {
76 args[2].parse::<uint>().unwrap()
81 ping_pong_bench(n, m);