Merge pull request #1190 from esdrubal/encoder-convert
[mono.git] / mono / tests / stress-runner.pl
1 #!/usr/bin/perl -w
2
3 # mono stress test tool
4 # This stress test runner is designed to detect possible
5 # leaks, runtime slowdowns and crashes when a task is performed
6 # repeatedly.
7 # A stress program should be written to repeat for a number of times
8 # a specific task: it is run a first time to collect info about memory
9 # and cpu usage: this run should last a couple of seconds or so.
10 # Then, the same program is run with a number of iterations that is at least
11 # 2 orders of magnitude bigger than the first run (3 orders should be used,
12 # eventually, to detect smaller leaks).
13 # Of course the right time for the test and the ratio depends on the test
14 # itself, so it's configurable per-test.
15 # The test driver will then check that the second run has used roughly the
16 # same amount of memory as the first and a proportionally bigger cpu time.
17 # Note: with a conservative GC there may be more false positives than
18 # with a precise one, because heap size may grow depending on timing etc.
19 # so failing results need to be checked carefully. In some cases a solution
20 # is to increase the number of runs in the dry run.
21
22 use POSIX ":sys_wait_h";
23 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
24
25 # in milliseconds between checks of resource usage
26 my $interval = 50;
27 # multiplier to allow some wiggle room
28 my $wiggle_ratio = 1.05;
29 # if the test computer is too fast or if we want to stress test more,
30 # we multiply the test ratio by this number. Use the --times=x option.
31 my $extra_strong = 1;
32
33 # descriptions of the tests to run
34 # for each test:
35 #       program is the program to run
36 #       args an array ref of argumenst to pass to program
37 #       arg-knob is the index of the argument in args that changes the number of iterations
38 #       ratio is the multiplier applied to the arg-knob argument
39 my %tests = (
40         'domain-stress' => {
41                 'program' => 'domain-stress.exe',
42                 # threads, domains, allocs, loops
43                 'args' => [2, 10, 1000, 1],
44                 'arg-knob' => 3, # loops
45                 'ratio' => 30,
46         },
47         'gchandle-stress' => {
48                 'program' => 'gchandle-stress.exe',
49                 # allocs, loops
50                 'args' => [80000, 2],
51                 'arg-knob' => 1, # loops
52                 'ratio' => 20,
53         },
54         'monitor-stress' => {
55                 'program' => 'monitor-stress.exe',
56                 # loops
57                 'args' => [10],
58                 'arg-knob' => 0, # loops
59                 'ratio' => 20,
60         },
61         'gc-stress' => {
62                 'program' => 'gc-stress.exe',
63                 # loops
64                 'args' => [25],
65                 'arg-knob' => 0, # loops
66                 'ratio' => 20,
67         },
68         'gc-graystack-stress' => {
69                 'program' => 'gc-graystack-stress.exe',
70                 # width, depth, collections
71                 'args' => [125, 10000, 100],
72                 'arg-knob' => 2, # loops
73                 'ratio' => 10,
74         },
75         'thread-stress' => {
76                 'program' => 'thread-stress.exe',
77                 # loops
78                 'args' => [20],
79                 'arg-knob' => 0, # loops
80                 'ratio' => 20,
81         },
82         'abort-stress-1' => {
83                 'program' => 'abort-stress-1.exe',
84                 # loops,
85                 'args' => [20],
86                 'arg-knob' => 0, # loops
87                 'ratio' => 20,
88         },
89         # FIXME: This tests exits, so it has no loops, instead it should be run more times
90         'exit-stress' => {
91                 'program' => 'exit-stress.exe',
92                 # loops,
93                 'args' => [10],
94                 'arg-knob' => 0, # loops
95                 'ratio' => 20,
96         }
97         # FIXME: This test deadlocks, bug 72740.
98         # We need hang detection
99         #'abort-stress-2' => {
100         #       'program' => 'abort-stress-2.exe',
101         #       # loops,
102         #       'args' => [20],
103         #       'arg-knob' => 0, # loops
104         #       'ratio' => 20,
105         #}
106 );
107
108 # poor man option handling
109 while (@ARGV) {
110         my $arg = shift @ARGV;
111         if ($arg =~ /^--times=(\d+)$/) {
112                 $extra_strong = $1;
113                 next;
114         }
115         if ($arg =~ /^--interval=(\d+)$/) {
116                 $interval = $1;
117                 next;
118         }
119         unshift @ARGV, $arg;
120         last;
121 }
122 my $test_rx = shift (@ARGV) || '.';
123 # the mono runtime to use and the arguments to pass to it
124 my @mono_args = @ARGV;
125 my @results = ();
126 my %vmmap = qw(VmSize 0 VmLck 1 VmRSS 2 VmData 3 VmStk 4 VmExe 5 VmLib 6 VmHWM 7 VmPTE 8 VmPeak 9);
127 my @vmnames = sort {$vmmap{$a} <=> $vmmap{$b}} keys %vmmap;
128 # VmRSS depends on the operating system's decisions
129 my %vmignore = qw(VmRSS 1);
130 my $errorcount = 0;
131 my $numtests = 0;
132
133 @mono_args = 'mono' unless @mono_args;
134
135 apply_options ();
136
137 foreach my $test (sort keys %tests) {
138         next unless ($tests{$test}->{'program'} =~ /$test_rx/);
139         $numtests++;
140         run_test ($test, 'dry');
141         run_test ($test, 'stress');
142 }
143
144 # print all the reports at the end
145 foreach my $test (sort keys %tests) {
146         next unless ($tests{$test}->{'program'} =~ /$test_rx/);
147         print_test_report ($test);
148 }
149
150 print "No tests matched '$test_rx'.\n" unless $numtests;
151
152 if ($errorcount) {
153         print "Total issues: $errorcount\n";
154         exit (1);
155 } else {
156         exit (0);
157 }
158
159 sub run_test {
160         my ($name, $mode) = @_;
161         my $test = $tests {$name};
162         my @targs = (@mono_args, $test->{program});
163         my @results = ();
164         my @rargs = @{$test->{"args"}};
165
166         if ($mode ne "dry") {
167                 # FIXME: set also a timeout
168                 $rargs [$test->{"arg-knob"}] *= $test->{"ratio"};
169         }
170         push @targs, @rargs;
171         print "Running test '$name' in $mode mode\n";
172         my $start_time = [gettimeofday];
173         my $pid = fork ();
174         if ($pid == 0) {
175                 exec @targs;
176                 die "Cannot exec: $! (@targs)\n";
177         } else {
178                 my $kid;
179                 do {
180                         $kid = waitpid (-1, WNOHANG);
181                         my $sample = collect_memusage ($pid);
182                         push @results, $sample if (defined ($sample) && @{$sample});
183                         # sleep for a few ms
184                         usleep ($interval * 1000) unless $kid > 0;
185                 } until $kid > 0;
186         }
187         my $end_time = [gettimeofday];
188         $test->{"$mode-cputime"} = tv_interval ($start_time, $end_time);
189         $test->{"$mode-memusage"} = [summarize_result (@results)];
190 }
191
192 sub print_test_report {
193         my ($name) = shift;
194         my $test = $tests {$name};
195         my ($cpu_dry, $cpu_test) = ($test->{'dry-cputime'}, $test->{'stress-cputime'});
196         my @dry_mem = @{$test->{'dry-memusage'}};
197         my @test_mem = @{$test->{'stress-memusage'}};
198         my $ratio = $test->{'ratio'};
199         print "Report for test: $name\n";
200         print "Cpu usage: dry: $cpu_dry, stress: $cpu_test\n";
201         print "Memory usage (KB):\n";
202         print "\t       ",join ("\t", @vmnames), "\n";
203         print "\t   dry: ", join ("\t", @dry_mem), "\n";
204         print "\tstress: ", join ("\t", @test_mem), "\n";
205         if ($cpu_test > ($cpu_dry * $ratio) * $wiggle_ratio) {
206                 print "Cpu usage not proportional to ratio $ratio.\n";
207                 $errorcount++;
208         }
209         my $i;
210         for ($i = 0; $i < @dry_mem; ++$i) {
211                 next if exists $vmignore {$vmnames [$i]};
212                 if ($test_mem [$i] > $dry_mem [$i] * $wiggle_ratio) {
213                         print "Memory usage $vmnames[$i] not constant.\n";
214                         $errorcount++;
215                 }
216         }
217 }
218
219 sub collect_memusage {
220         my ($pid) = @_;
221         open (PROC, "</proc/$pid/status") || return undef; # might be dead already
222         my @sample = ();
223         while (<PROC>) {
224                 next unless /^(Vm.*?):\s+(\d+)\s+kB/;
225                 $sample [$vmmap {$1}] = $2;
226         }
227         close (PROC);
228         return \@sample;
229 }
230
231 sub summarize_result {
232         my (@data) = @_;
233         my (@result) = (0) x 7;
234         my $i;
235         foreach my $sample (@data) {
236                 for ($i = 0; $i < 7; ++$i) {
237                         if ($sample->[$i] > $result [$i]) {
238                                 $result [$i] = $sample->[$i];
239                         }
240                 }
241         }
242         return @result;
243 }
244
245 sub apply_options {
246         foreach my $test (values %tests) {
247                 $test->{args}->[$test->{'arg-knob'}] *= $extra_strong;
248         }
249 }
250