@@ -17,6 +17,8 @@ import std::time;
17
17
import std:: str;
18
18
import std:: int:: range;
19
19
import std:: io;
20
+ import std:: getopts;
21
+ import std:: task;
20
22
21
23
fn recv[ T ] ( & port[ T ] p) -> T {
22
24
let T x;
@@ -33,7 +35,7 @@ fn fib(int n) -> int {
33
35
c <| 1 ;
34
36
}
35
37
else {
36
- let port [ int ] p = port ( ) ;
38
+ auto p = port ( ) ;
37
39
38
40
auto t1 = spawn pfib( chan ( p) , n - 1 ) ;
39
41
auto t2 = spawn pfib( chan ( p) , n - 2 ) ;
@@ -42,11 +44,48 @@ fn fib(int n) -> int {
42
44
}
43
45
}
44
46
45
- let port [ int ] p = port ( ) ;
47
+ auto p = port ( ) ;
46
48
auto t = spawn pfib( chan ( p) , n) ;
47
49
ret recv( p) ;
48
50
}
49
51
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, 1 u, 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
+
50
89
fn main( vec[ str] argv ) {
51
90
if ( vec:: len ( argv) == 1 u) {
52
91
assert ( fib ( 8 ) == 21 ) ;
@@ -56,24 +95,29 @@ fn main(vec[str] argv) {
56
95
}
57
96
else {
58
97
// Interactive mode! Wooo!!!!
98
+ auto opts = parse_opts ( argv) ;
59
99
60
- auto max = uint:: parse_buf ( str:: bytes ( argv. ( 1 ) ) , 10 u) as int ;
100
+ if ( opts. stress ) {
101
+ stress ( 2 ) ;
102
+ }
103
+ else {
104
+ auto max = uint:: parse_buf ( str:: bytes ( argv. ( 1 ) ) , 10 u) as int ;
61
105
62
- auto num_trials = 10 ;
106
+ auto num_trials = 10 ;
63
107
64
- auto out = io:: stdout ( ) ;
108
+ auto out = io:: stdout ( ) ;
65
109
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( ) ;
71
115
72
- auto elapsed = ( stop - start) as int;
116
+ auto elapsed = ( stop - start) as int;
73
117
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
+ }
75
120
}
76
121
}
77
-
78
122
}
79
123
}
0 commit comments