Skip to content
Newer
Older
100644 118 lines (94 sloc) 2.58 KB
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
1 // -*- rust -*-
050170d @marijnh Remove remaining uses of iter and for-each
marijnh authored
2 // xfail-pretty
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
3
4 /*
5 A parallel version of fibonacci numbers.
6
7 This version is meant mostly as a way of stressing and benchmarking
8 the task system. It supports a lot of command-line arguments to
9 control how it runs.
10
11 */
12
13 use std;
14
6f5853f @graydon Libc/os/run/rand/io reorganization. Close #1373. Close #1638.
graydon authored
15 import std::{time, getopts};
34d7f05 @marijnh Major clean-up of std::io
marijnh authored
16 import io::writer_util;
fa9ad98 @graydon Copy first batch of material from libstd to libcore.
graydon authored
17 import int::range;
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
18 import pipes::port;
19 import pipes::chan;
20 import pipes::send;
21 import pipes::recv;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
22
fa27724 @boggle std: getopts now uses result::t (fixes #1289)
boggle authored
23 import core::result;
24 import result::{ok, err};
25
df7f21d @marijnh Reformat for new syntax
marijnh authored
26 fn fib(n: int) -> int {
a1ef79c @nikomatsakis update to use new spawn syntax
nikomatsakis authored
27 fn pfib(c: chan<int>, n: int) {
df7f21d @marijnh Reformat for new syntax
marijnh authored
28 if n == 0 {
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
29 c.send(0);
518dc52 @brson Reformat
brson authored
30 } else if n <= 2 {
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
31 c.send(1);
df7f21d @marijnh Reformat for new syntax
marijnh authored
32 } else {
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
33 let p = pipes::port_set();
34 let ch = p.chan();
d1fc2b5 @brson Convert to new closure syntax
brson authored
35 task::spawn(|| pfib(ch, n - 1) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
36 let ch = p.chan();
d1fc2b5 @brson Convert to new closure syntax
brson authored
37 task::spawn(|| pfib(ch, n - 2) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
38 c.send(p.recv() + p.recv());
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
39 }
40 }
41
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
42 let (ch, p) = pipes::stream();
d1fc2b5 @brson Convert to new closure syntax
brson authored
43 let t = task::spawn(|| pfib(ch, n) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
44 p.recv()
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
45 }
46
df7f21d @marijnh Reformat for new syntax
marijnh authored
47 type config = {stress: bool};
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
48
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
49 fn parse_opts(argv: ~[~str]) -> config {
50 let opts = ~[getopts::optflag(~"stress")];
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
51
f05a91a @brson Rename std::ivec to std::vec
brson authored
52 let opt_args = vec::slice(argv, 1u, vec::len(argv));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
53
df7f21d @marijnh Reformat for new syntax
marijnh authored
54
5f71a20 @brson Remove vecs from std::getopts
brson authored
55 alt getopts::getopts(opt_args, opts) {
b355936 @brson Convert ret to return
brson authored
56 ok(m) { return {stress: getopts::opt_present(m, ~"stress")} }
fa27724 @boggle std: getopts now uses result::t (fixes #1289)
boggle authored
57 err(_) { fail; }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
58 }
59 }
60
ebc61e3 @brson Remove temporary fn# syntax
brson authored
61 fn stress_task(&&id: int) {
dc07280 @nikomatsakis make --enforce-mut-vars always on, add mut annotations to remaining f…
nikomatsakis authored
62 let mut i = 0;
321fd80 @catamorphism Add an infinite loop construct
catamorphism authored
63 loop {
df7f21d @marijnh Reformat for new syntax
marijnh authored
64 let n = 15;
65 assert (fib(n) == fib(n));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
66 i += 1;
a9cc506 @paulstansifer Change syntax extension syntax: `#m[...]` -> `m!{...}`.
paulstansifer authored
67 error!{"%d: Completed %d iterations", id, i};
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
68 }
69 }
70
df7f21d @marijnh Reformat for new syntax
marijnh authored
71 fn stress(num_tasks: int) {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Closes
msullivan authored
72 let mut results = ~[];
d1fc2b5 @brson Convert to new closure syntax
brson authored
73 for range(0, num_tasks) |i| {
b30a58a @bblum Convert bench and run-pass tests to new task_builder interface
bblum authored
74 do task::task().future_result(|-r| {
75 results += ~[r];
76 }).spawn {
77 stress_task(i);
78 }
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
79 }
d1fc2b5 @brson Convert to new closure syntax
brson authored
80 for results.each |r| { future::get(r); }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
81 }
82
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
83 fn main(args: ~[~str]) {
84 let args = if os::getenv(~"RUST_BENCH").is_some() {
85 ~[~"", ~"20"]
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
86 } else if args.len() <= 1u {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
87 ~[~"", ~"8"]
df7f21d @marijnh Reformat for new syntax
marijnh authored
88 } else {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
89 args
90 };
df7f21d @marijnh Reformat for new syntax
marijnh authored
91
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
92 let opts = parse_opts(args);
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
93
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
94 if opts.stress {
95 stress(2);
96 } else {
97 let max = option::get(uint::parse_buf(str::bytes(args[1]),
98 10u)) as int;
df7f21d @marijnh Reformat for new syntax
marijnh authored
99
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
100 let num_trials = 10;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
101
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
102 let out = io::stdout();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
103
d1fc2b5 @brson Convert to new closure syntax
brson authored
104 for range(1, max + 1) |n| {
105 for range(0, num_trials) |i| {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
106 let start = time::precise_time_ns();
107 let fibn = fib(n);
108 let stop = time::precise_time_ns();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
109
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
110 let elapsed = stop - start;
39151f2 @graydon Prohibit trailing whitespace under 'tidy' script. Clean up all caught…
graydon authored
111
a9cc506 @paulstansifer Change syntax extension syntax: `#m[...]` -> `m!{...}`.
paulstansifer authored
112 out.write_line(fmt!{"%d\t%d\t%s", n, fibn,
113 u64::str(elapsed)});
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
114 }
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
115 }
116 }
f91351a @lkuper The wonky for...in... whitespace was bothering me. Sorry!
lkuper authored
117 }
Something went wrong with that request. Please try again.