|
| 1 | +// This test creates a bunch of tasks that simultaneously send to each |
| 2 | +// other in a ring. The messages should all be basically |
| 3 | +// independent. It's designed to hammer the global kernel lock, so |
| 4 | +// that things will look really good once we get that lock out of the |
| 5 | +// message path. |
| 6 | + |
| 7 | +import newcomm::*; |
| 8 | +import future::future; |
| 9 | + |
| 10 | +use std; |
| 11 | +import std::time; |
| 12 | + |
| 13 | +fn thread_ring(i: uint, |
| 14 | + count: uint, |
| 15 | + num_chan: chan<uint>, |
| 16 | + num_port: port<uint>) { |
| 17 | + // Send/Receive lots of messages. |
| 18 | + for uint::range(0u, count) {|j| |
| 19 | + num_chan.send(i * j); |
| 20 | + num_port.recv(); |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +fn main(args: [str]) { |
| 25 | + let args = if os::getenv("RUST_BENCH").is_some() { |
| 26 | + ["", "100", "10000"] |
| 27 | + } else if args.len() <= 1u { |
| 28 | + ["", "100", "1000"] |
| 29 | + } else { |
| 30 | + args |
| 31 | + }; |
| 32 | + |
| 33 | + let num_tasks = option::get(uint::from_str(args[1])); |
| 34 | + let msg_per_task = option::get(uint::from_str(args[2])); |
| 35 | + |
| 36 | + let num_port = port(); |
| 37 | + let mut num_chan = chan(num_port); |
| 38 | + |
| 39 | + let start = time::precise_time_s(); |
| 40 | + |
| 41 | + // create the ring |
| 42 | + let mut futures = []; |
| 43 | + |
| 44 | + for uint::range(1u, num_tasks) {|i| |
| 45 | + let get_chan = port(); |
| 46 | + let get_chan_chan = chan(get_chan); |
| 47 | + { |
| 48 | + let num_chan = num_chan.clone(); |
| 49 | + futures += [future::spawn {|move num_chan, move get_chan_chan| |
| 50 | + let p = port(); |
| 51 | + get_chan_chan.send(chan(p)); |
| 52 | + thread_ring(i, msg_per_task, num_chan, p) |
| 53 | + }]; |
| 54 | + } |
| 55 | + |
| 56 | + num_chan = get_chan.recv(); |
| 57 | + }; |
| 58 | + |
| 59 | + // do our iteration |
| 60 | + thread_ring(0u, msg_per_task, num_chan, num_port); |
| 61 | + |
| 62 | + // synchronize |
| 63 | + for futures.each {|f| f.get() }; |
| 64 | + |
| 65 | + let stop = time::precise_time_s(); |
| 66 | + |
| 67 | + // all done, report stats. |
| 68 | + let num_msgs = num_tasks * msg_per_task; |
| 69 | + let elapsed = (stop - start); |
| 70 | + let rate = (num_msgs as float) / elapsed; |
| 71 | + |
| 72 | + io::println(#fmt("Sent %? messages in %? seconds", |
| 73 | + num_msgs, elapsed)); |
| 74 | + io::println(#fmt(" %? messages / second", rate)); |
| 75 | + io::println(#fmt(" %? μs / message", 1000000. / rate)); |
| 76 | +} |
0 commit comments