]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
Make complete-run store the timings, schedule tests and print time estimate
[i3/i3] / testcases / complete-run.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 # © 2010-2011 Michael Stapelberg and contributors
4 package complete_run;
5 use strict;
6 use warnings;
7 use v5.10;
8 # the following are modules which ship with Perl (>= 5.10):
9 use Pod::Usage;
10 use Cwd qw(abs_path);
11 use File::Temp qw(tempfile tempdir);
12 use Getopt::Long;
13 use POSIX ();
14 use TAP::Harness;
15 use TAP::Parser;
16 use TAP::Parser::Aggregator;
17 use Time::HiRes qw(time);
18 # these are shipped with the testsuite
19 use lib qw(lib);
20 use StartXDummy;
21 use StatusLine;
22 use TestWorker;
23 # the following modules are not shipped with Perl
24 use AnyEvent;
25 use AnyEvent::Util;
26 use AnyEvent::Handle;
27 use AnyEvent::I3 qw(:all);
28 use X11::XCB::Connection;
29 use JSON::XS; # AnyEvent::I3 depends on it, too.
30
31 # Close superfluous file descriptors which were passed by running in a VIM
32 # subshell or situations like that.
33 AnyEvent::Util::close_all_fds_except(0, 1, 2);
34
35 # convinience wrapper to write to the log file
36 my $log;
37 sub Log { say $log "@_" }
38
39 my %timings;
40 my $coverage_testing = 0;
41 my $valgrind = 0;
42 my $strace = 0;
43 my $help = 0;
44 # Number of tests to run in parallel. Important to know how many Xdummy
45 # instances we need to start (unless @displays are given). Defaults to
46 # num_cores * 2.
47 my $parallel = undef;
48 my @displays = ();
49
50 my $result = GetOptions(
51     "coverage-testing" => \$coverage_testing,
52     "valgrind" => \$valgrind,
53     "strace" => \$strace,
54     "display=s" => \@displays,
55     "parallel=i" => \$parallel,
56     "help|?" => \$help,
57 );
58
59 pod2usage(-verbose => 2, -exitcode => 0) if $help;
60
61 @displays = split(/,/, join(',', @displays));
62 @displays = map { s/ //g; $_ } @displays;
63
64 # No displays specified, let’s start some Xdummy instances.
65 @displays = start_xdummy($parallel) if @displays == 0;
66
67 # 1: create an output directory for this test-run
68 my $outdir = "testsuite-";
69 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
70 $outdir .= `git describe --tags`;
71 chomp($outdir);
72 mkdir($outdir) or die "Could not create $outdir";
73 unlink("latest") if -e "latest";
74 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
75
76
77 # connect to all displays for two reasons:
78 # 1: check if the display actually works
79 # 2: keep the connection open so that i3 is not the only client. this prevents
80 #    the X server from exiting (Xdummy will restart it, but not quick enough
81 #    sometimes)
82 my @worker;
83 for my $display (@displays) {
84     my $screen;
85     my $x = X11::XCB::Connection->new(display => $display);
86     if ($x->has_error) {
87         die "Could not connect to display $display\n";
88     } else {
89         # start a TestWorker for each display
90         push @worker, worker($display, $x, $outdir);
91     }
92 }
93
94 # Read previous timing information, if available. We will be able to roughly
95 # predict the test duration and schedule a good order for the tests.
96 my $timingsjson = StartXDummy::slurp('.last_run_timings.json');
97 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
98
99 # 2: get a list of all testcases
100 my @testfiles = @ARGV;
101
102 # if no files were passed on command line, run all tests from t/
103 @testfiles = <t/*.t> if @testfiles == 0;
104
105 # Re-order the files so that those which took the longest time in the previous
106 # run will be started at the beginning to not delay the whole run longer than
107 # necessary.
108 @testfiles = map  { $_->[0] }
109              sort { $b->[1] <=> $a->[1] }
110              map  { [$_, $timings{$_} // 999] } @testfiles;
111
112 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
113     if exists($timings{GLOBAL});
114
115 # Forget the old timings, we don’t necessarily run the same set of tests as
116 # before. Otherwise we would end up with left-overs.
117 %timings = (GLOBAL => time());
118
119 my $logfile = "$outdir/complete-run.log";
120 open $log, '>', $logfile or die "Could not create '$logfile': $!";
121 say "Writing logfile to '$logfile'...";
122
123 # 3: run all tests
124 my @done;
125 my $num = @testfiles;
126 my $harness = TAP::Harness->new({ });
127
128 my $aggregator = TAP::Parser::Aggregator->new();
129 $aggregator->start();
130
131 status_init(displays => \@displays, tests => $num);
132
133 my $cv = AE::cv;
134
135 # We start tests concurrently: For each display, one test gets started. Every
136 # test starts another test after completing.
137 for (@worker) { $cv->begin; take_job($_) }
138
139 $cv->recv;
140
141 $aggregator->stop();
142
143 # print empty lines to seperate failed tests from statuslines
144 print "\n\n";
145
146 for (@done) {
147     my ($test, $output) = @$_;
148     say "no output for $test" unless $output;
149     Log "output for $test:";
150     Log $output;
151     # print error messages of failed tests
152     say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
153 }
154
155 # 4: print summary
156 $harness->summary($aggregator);
157
158 close $log;
159
160 # 5: Save the timings for better scheduling/prediction next run.
161 $timings{GLOBAL} = time() - $timings{GLOBAL};
162 open(my $fh, '>', '.last_run_timings.json');
163 print $fh encode_json(\%timings);
164 close($fh);
165
166 # 6: Print the slowest test files.
167 my @slowest = map  { $_->[0] }
168               sort { $b->[1] <=> $a->[1] }
169               map  { [$_, $timings{$_}] }
170               grep { !/^GLOBAL$/ } keys %timings;
171 say '';
172 say 'The slowest tests are:';
173 printf("\t%s with %.2f seconds\n", $_, $timings{$_}) for @slowest[0..4];
174
175 END { cleanup() }
176
177 exit 0;
178
179 #
180 # Takes a test from the beginning of @testfiles and runs it.
181 #
182 # The TAP::Parser (which reads the test output) will get called as soon as
183 # there is some activity on the stdout file descriptor of the test process
184 # (using an AnyEvent->io watcher).
185 #
186 # When a test completes and @done contains $num entries, the $cv condvar gets
187 # triggered to finish testing.
188 #
189 sub take_job {
190     my ($worker) = @_;
191
192     my $test = shift @testfiles
193         or return $cv->end;
194
195     my $display = $worker->{display};
196
197     Log status($display, "$test: starting");
198     $timings{$test} = time();
199     worker_next($worker, $test);
200
201     # create a TAP::Parser with an in-memory fh
202     my $output;
203     my $parser = TAP::Parser->new({
204         source => do { open(my $fh, '<', \$output); $fh },
205     });
206
207     my $ipc = $worker->{ipc};
208
209     my $w;
210     $w = AnyEvent->io(
211         fh => $ipc,
212         poll => 'r',
213         cb => sub {
214             state $tests_completed = 0;
215             state $partial = '';
216
217             sysread($ipc, my $buf, 4096) or die "sysread: $!";
218
219             if ($partial) {
220                 $buf = $partial . $buf;
221                 $partial = '';
222             }
223
224             # make sure we feed TAP::Parser complete lines so it doesn't blow up
225             if (substr($buf, -1, 1) ne "\n") {
226                 my $nl = rindex($buf, "\n");
227                 if ($nl == -1) {
228                     $partial = $buf;
229                     return;
230                 }
231
232                 # strip partial from buffer
233                 $partial = substr($buf, $nl + 1, '');
234             }
235
236             # count lines before stripping eof-marker otherwise we might
237             # end up with for (1 .. 0) { } which would effectivly skip the loop
238             my $lines = $buf =~ tr/\n//;
239             my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
240
241             $output .= $buf;
242
243             for (1 .. $lines) {
244                 my $result = $parser->next;
245                 if (defined($result) and $result->is_test) {
246                     $tests_completed++;
247                     status($display, "$test: [$tests_completed/??] ");
248                 }
249             }
250
251             return unless $t_eof;
252
253             Log status($display, "$test: finished");
254             $timings{$test} = time() - $timings{$test};
255             status_completed(scalar @done);
256
257             $aggregator->add($test, $parser);
258             push @done, [ $test, $output ];
259
260             undef $w;
261             take_job($worker);
262         }
263     );
264 }
265
266 sub cleanup {
267     $_->() for our @CLEANUP;
268     exit;
269 }
270
271 # must be in a begin block because we C<exit 0> above
272 BEGIN {
273     $SIG{$_} = sub {
274         require Carp; Carp::cluck("Caught SIG$_[0]\n");
275         cleanup();
276     } for qw(INT TERM QUIT KILL PIPE)
277 }
278
279 __END__
280
281 =head1 NAME
282
283 complete-run.pl - Run the i3 testsuite
284
285 =head1 SYNOPSIS
286
287 complete-run.pl [files...]
288
289 =head1 EXAMPLE
290
291 To run the whole testsuite on a reasonable number of Xdummy instances (your
292 running X11 will not be touched), run:
293   ./complete-run.pl
294
295 To run only a specific test (useful when developing a new feature), run:
296   ./complete-run t/100-fullscreen.t
297
298 =head1 OPTIONS
299
300 =over 8
301
302 =item B<--display>
303
304 Specifies which X11 display should be used. Can be specified multiple times and
305 will parallelize the tests:
306
307   # Run tests on the second X server
308   ./complete-run.pl -d :1
309
310   # Run four tests in parallel on some Xdummy servers
311   ./complete-run.pl -d :1,:2,:3,:4
312
313 Note that it is not necessary to specify this anymore. If omitted,
314 complete-run.pl will start (num_cores * 2) Xdummy instances.
315
316 =item B<--valgrind>
317
318 Runs i3 under valgrind to find memory problems. The output will be available in
319 C<latest/valgrind-for-$test.log>.
320
321 =item B<--strace>
322
323 Runs i3 under strace to trace system calls. The output will be available in
324 C<latest/strace-for-$test.log>.
325
326 =item B<--coverage-testing>
327
328 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
329
330 =item B<--parallel>
331
332 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
333 instances for some reason).
334
335   # Run all tests on a single Xdummy instance
336   ./complete-run.pl -p 1