Skip to content

Commit 2d57b25

Browse files
author
Eric Holk
committed
Added a stress test mode to pfib.
1 parent f6e37f6 commit 2d57b25

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

src/test/bench/task-perf/pfib.rs

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import std::time;
1717
import std::str;
1818
import std::int::range;
1919
import std::io;
20+
import std::getopts;
21+
import std::task;
2022

2123
fn recv[T](&port[T] p) -> T {
2224
let T x;
@@ -33,7 +35,7 @@ fn fib(int n) -> int {
3335
c <| 1;
3436
}
3537
else {
36-
let port[int] p = port();
38+
auto p = port();
3739

3840
auto t1 = spawn pfib(chan(p), n - 1);
3941
auto t2 = spawn pfib(chan(p), n - 2);
@@ -42,11 +44,48 @@ fn fib(int n) -> int {
4244
}
4345
}
4446

45-
let port[int] p = port();
47+
auto p = port();
4648
auto t = spawn pfib(chan(p), n);
4749
ret recv(p);
4850
}
4951

52+
type config = rec(bool stress);
53+
54+
fn parse_opts(vec[str] argv) -> config {
55+
auto opts = [getopts::optflag("stress")];
56+
57+
auto opt_args = vec::slice(argv, 1u, vec::len(argv));
58+
59+
alt(getopts::getopts(opt_args, opts)) {
60+
case(getopts::success(?m)) {
61+
ret rec(stress = getopts::opt_present(m, "stress"))
62+
}
63+
case(getopts::failure(_)) {
64+
fail;
65+
}
66+
}
67+
}
68+
69+
fn stress_task(int id) {
70+
auto i = 0;
71+
while(true) {
72+
auto n = 15;
73+
assert(fib(n) == fib(n));
74+
i += 1;
75+
log_err #fmt("%d: Completed %d iterations", id, i);
76+
}
77+
}
78+
79+
fn stress(int num_tasks) {
80+
auto tasks = [];
81+
for each(int i in range(0, num_tasks)) {
82+
tasks += [spawn stress_task(i)];
83+
}
84+
for each(int i in range(0, num_tasks)) {
85+
task::join(tasks.(i));
86+
}
87+
}
88+
5089
fn main(vec[str] argv) {
5190
if(vec::len(argv) == 1u) {
5291
assert (fib(8) == 21);
@@ -56,24 +95,29 @@ fn main(vec[str] argv) {
5695
}
5796
else {
5897
// Interactive mode! Wooo!!!!
98+
auto opts = parse_opts(argv);
5999

60-
auto max = uint::parse_buf(str::bytes(argv.(1)), 10u) as int;
100+
if(opts.stress) {
101+
stress(2);
102+
}
103+
else {
104+
auto max = uint::parse_buf(str::bytes(argv.(1)), 10u) as int;
61105

62-
auto num_trials = 10;
106+
auto num_trials = 10;
63107

64-
auto out = io::stdout();
108+
auto out = io::stdout();
65109

66-
for each(int n in range(1, max + 1)) {
67-
for each(int i in range(0, num_trials)) {
68-
auto start = time::precise_time_ns();
69-
auto fibn = fib(n);
70-
auto stop = time::precise_time_ns();
110+
for each(int n in range(1, max + 1)) {
111+
for each(int i in range(0, num_trials)) {
112+
auto start = time::precise_time_ns();
113+
auto fibn = fib(n);
114+
auto stop = time::precise_time_ns();
71115

72-
auto elapsed = (stop - start) as int;
116+
auto elapsed = (stop - start) as int;
73117

74-
out.write_line(#fmt("%d\t%d\t%d", n, fibn, elapsed));
118+
out.write_line(#fmt("%d\t%d\t%d", n, fibn, elapsed));
119+
}
75120
}
76121
}
77-
78122
}
79123
}

0 commit comments

Comments
 (0)