rust/src/test/bench/task-perf-one-million.rs

73 lines
1.5 KiB
Rust
Raw Normal View History

2012-02-15 11:52:43 -08:00
// Test for concurrent tasks
enum msg {
ready(comm::chan<msg>),
start,
done(int),
}
fn calc(children: uint, parent_ch: comm::chan<msg>) {
let port = comm::port();
let chan = comm::chan(port);
let mut child_chs = ~[];
let mut sum = 0;
2012-02-15 11:52:43 -08:00
for iter::repeat (children) {
do task::spawn {
2012-02-15 11:52:43 -08:00
calc(0u, chan);
};
}
for iter::repeat (children) {
2012-08-06 12:34:08 -07:00
match check comm::recv(port) {
2012-08-03 19:59:04 -07:00
ready(child_ch) => {
vec::push(child_chs, child_ch);
2012-02-15 11:52:43 -08:00
}
}
}
comm::send(parent_ch, ready(chan));
2012-08-06 12:34:08 -07:00
match check comm::recv(port) {
2012-08-03 19:59:04 -07:00
start => {
2012-06-30 16:19:07 -07:00
do vec::iter (child_chs) |child_ch| {
2012-02-15 11:52:43 -08:00
comm::send(child_ch, start);
}
}
}
for iter::repeat (children) {
2012-08-06 12:34:08 -07:00
match check comm::recv(port) {
2012-08-03 19:59:04 -07:00
done(child_sum) => { sum += child_sum; }
2012-02-15 11:52:43 -08:00
}
}
comm::send(parent_ch, done(sum + 1));
}
fn main(args: ~[~str]) {
let args = if os::getenv(~"RUST_BENCH").is_some() {
~[~"", ~"100000"]
} else if args.len() <= 1u {
~[~"", ~"100"]
2012-02-15 11:52:43 -08:00
} else {
args
2012-02-15 11:52:43 -08:00
};
let children = uint::from_str(args[1]).get();
2012-02-15 11:52:43 -08:00
let port = comm::port();
let chan = comm::chan(port);
do task::spawn {
2012-02-15 11:52:43 -08:00
calc(children, chan);
};
2012-08-06 12:34:08 -07:00
match check comm::recv(port) {
2012-08-03 19:59:04 -07:00
ready(chan) => {
2012-02-15 11:52:43 -08:00
comm::send(chan, start);
}
}
2012-08-06 12:34:08 -07:00
let sum = match check comm::recv(port) {
2012-08-03 19:59:04 -07:00
done(sum) => { sum }
2012-02-15 11:52:43 -08:00
};
error!{"How many tasks? %d tasks.", sum};
}