diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-02-04 18:00:59 -0800 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-02-04 20:01:55 -0800 |
| commit | b4e221bf90ba33fc0b765dfb9f1e6961f194e57a (patch) | |
| tree | 54457249f96359ff0c6cbd89feceea601562674a | |
| parent | 5d4b372902da7b3234e7386e9b86cd700b434377 (diff) | |
| download | rust-b4e221bf90ba33fc0b765dfb9f1e6961f194e57a.tar.gz rust-b4e221bf90ba33fc0b765dfb9f1e6961f194e57a.zip | |
bench: Add threadring shootout benchmark
| -rw-r--r-- | src/test/bench/shootout-threadring.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/test/bench/shootout-threadring.rs b/src/test/bench/shootout-threadring.rs new file mode 100644 index 00000000000..dce7336cd87 --- /dev/null +++ b/src/test/bench/shootout-threadring.rs @@ -0,0 +1,50 @@ +// Based on threadring.erlang by Jira Isa +use std; + +const n_threads: int = 503; + +fn start(+token: int) { + import iter::*; + + let p = comm::port(); + let ch = iter::foldl(bind int::range(2, n_threads + 1, _), + comm::chan(p)) { |ch, i| + // FIXME: Some twiddling because we don't have a standard + // reverse range function yet + let id = n_threads + 2 - i; + let {to_child, _} = task::spawn_connected::<int, int> {|p, _ch| + roundtrip(id, p, ch) + }; + to_child + }; + comm::send(ch, token); + roundtrip(1, p, ch); +} + +fn roundtrip(id: int, p: comm::port<int>, ch: comm::chan<int>) { + while (true) { + alt comm::recv(p) { + 1 { + std::io::println(#fmt("%d\n", id)); + ret; + } + token { + #debug("%d %d", id, token); + comm::send(ch, token - 1); + if token <= n_threads { + ret; + } + } + } + } +} + +fn main(args: [str]) { + let token = if vec::len(args) < 2u { + 1000 + } else { + int::from_str(args[1]) + }; + + start(token); +} \ No newline at end of file |
