summary refs log tree commit diff
path: root/src/test/bench/msgsend-ring.rs
blob: 1dfcd241b83b94e19f05a9e2d300584663c753de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// This test creates a bunch of tasks that simultaneously send to each
// other in a ring. The messages should all be basically
// independent. It's designed to hammer the global kernel lock, so
// that things will look really good once we get that lock out of the
// message path.

use comm::*;
use future::future;

extern mod std;
use std::time;

fn thread_ring(i: uint,
               count: uint,
               num_chan: comm::Chan<uint>,
               num_port: comm::Port<uint>) {
    // Send/Receive lots of messages.
    for uint::range(0u, count) |j| {
        num_chan.send(i * j);
        num_port.recv();
    };
}

fn main() {
    let args = os::args();
    let args = if os::getenv(~"RUST_BENCH").is_some() {
        ~[~"", ~"100", ~"10000"]
    } else if args.len() <= 1u {
        ~[~"", ~"100", ~"1000"]
    } else {
        args
    };        

    let num_tasks = uint::from_str(args[1]).get();
    let msg_per_task = uint::from_str(args[2]).get();

    let num_port = Port();
    let mut num_chan = Chan(&num_port);

    let start = time::precise_time_s();

    // create the ring
    let mut futures = ~[];

    for uint::range(1u, num_tasks) |i| {
        let get_chan = Port();
        let get_chan_chan = Chan(&get_chan);

        let new_future = do future::spawn
            |copy num_chan, move get_chan_chan| {
            let p = Port();
            get_chan_chan.send(Chan(&p));
            thread_ring(i, msg_per_task, num_chan,  p)
        };
        futures.push(new_future);
        
        num_chan = get_chan.recv();
    };

    // do our iteration
    thread_ring(0u, msg_per_task, num_chan, num_port);

    // synchronize
    for futures.each |f| { f.get() };

    let stop = time::precise_time_s();

    // all done, report stats.
    let num_msgs = num_tasks * msg_per_task;
    let elapsed = (stop - start);
    let rate = (num_msgs as float) / elapsed;

    io::println(fmt!("Sent %? messages in %? seconds",
                     num_msgs, elapsed));
    io::println(fmt!("  %? messages / second", rate));
    io::println(fmt!("  %? μs / message", 1000000. / rate));
}