source: tags/arb-6.0/UNIT_TESTER/reporter.pl

Last change on this file was 11774, checked in by westram, 10 years ago
  • temporarily dump all logs (to detect what's so slow on colab)

(reverted by [12410])

  • Property svn:executable set to *
File size: 12.0 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6# --------------------------------------------------------------------------------
7
8my $logdirectory = undef;
9my $slow_delay   = undef;
10
11# --------------------------------------------------------------------------------
12
13sub getModtime($) {
14  my ($file_or_dir) = @_;
15  if (-f $file_or_dir or -d $file_or_dir) {
16    my @st = stat($file_or_dir);
17    if (not @st) { die "can't stat '$file_or_dir' ($!)"; }
18    return $st[9];
19  }
20  return 0; # does not exist -> use epoch
21}
22sub getAge($) { my ($file_or_dir) = @_; return time-getModtime($file_or_dir); }
23
24# --------------------------------------------------------------------------------
25
26# when file $slow_stamp exists, slow tests get skipped (see sym2testcode.pl@SkipSlow)
27my $slow_stamp = 'skipslow.stamp';
28my $slow_age   = getAge($slow_stamp); # seconds since of last successful slow test
29
30sub shall_run_slow() { return (($slow_delay==0) or ($slow_age>($slow_delay*60))); }
31
32sub slow_init() {
33  if (shall_run_slow()) {
34    print "Running SLOW tests\n";
35    unlink($slow_stamp);
36  }
37  else {
38    print "Skipping SLOW tests\n";
39  }
40}
41
42sub slow_cleanup($) {
43  my ($tests_failed) = @_;
44
45  if (shall_run_slow() and not $tests_failed) {
46    system("touch $slow_stamp");
47  }
48}
49
50# --------------------------------------------------------------------------------
51
52sub get_existing_logs() {
53  my @logs;
54  opendir(LOGDIR,$logdirectory) || die "can't read directory '$logdirectory' (Reason: $!)";
55  foreach (readdir(LOGDIR)) {
56    if (/\.log$/o) { push @logs, $logdirectory.'/'.$_; }
57  }
58  closedir(LOGDIR);
59  return @logs;
60}
61
62sub do_init() {
63  if (-d $logdirectory) {
64    my @logs = get_existing_logs();
65    foreach (@logs) { unlink($_) || die "can't unlink '$_' (Reason: $!)"; }
66  }
67  slow_init();
68  return undef;
69}
70# --------------------------------------------------------------------------------
71
72my $expand_list_read = 0;
73my @expand_list = ();
74
75sub read_expand_list() {
76  my $expand_list = "../SOURCE_TOOLS/valgrind2grep.lst";
77
78  if (not -f $expand_list) {
79    my $cmd = '(cd ../SOURCE_TOOLS; make valgrind_update)';
80    system($cmd)==0 || die "failed to execute '$cmd' (Reason: $?)";
81  }
82
83  my $dir = `pwd`;
84  open(LIST,'<'.$expand_list) || die "can't read '$expand_list' (Reason: $!) in dir '$dir'";
85  my $line;
86  while (defined($line=<LIST>)) {
87    chomp($line);
88    push @expand_list, $line;
89  }
90  close(LIST);
91
92  $expand_list_read = 1;
93}
94
95my %expanded = (); # key=$filename, value=ref to array of possible expanded filenames.
96
97sub get_expanded_filenames($) {
98  my ($file) = @_;
99
100  my $found_r = $expanded{$file};
101  if (not defined $found_r) {
102    if ($expand_list_read==0) { read_expand_list(); }
103    my @expanded = ();
104    foreach (@expand_list) { if (/\/$file$/) { push @expanded, $_; } }
105    $expanded{$file} = \@expanded;
106    $found_r = $expanded{$file};
107  }
108  return @$found_r;
109}
110
111sub print_expand_pathless_messages($) {
112  my ($line) = @_;
113  chomp($line);
114  if ($line =~ /^([a-z0-9_\.]+):([0-9:]+):/oi) {
115    my ($file,$lineCol,$rest) = ($1,$2,$');
116    my @expanded = get_expanded_filenames($file);
117
118    if (scalar(@expanded)==0) {
119      print "$file:$lineCol: [unknown -> call 'make valgrind_update'] $rest\n";
120    }
121    else {
122      foreach (@expanded) {
123        print "$_:$lineCol: $rest\n";
124      }
125    }
126  }
127  else {
128    print $line."\n";
129  }
130}
131
132# --------------------------------------------------------------------------------
133
134sub dump_junitlog(\@) {
135  my ($content_r) = @_;
136  my $logfile = "logs/junit_log.xml"; # see also Makefile.suite@JUNITLOGNAME
137  open(JLOG,'>'.$logfile) || die "can't write '$logfile' (Reason: $!)";
138  print JLOG "<testsuites>\n";
139  foreach (@$content_r) {
140    print JLOG $_."\n";
141  }
142  print JLOG "</testsuites>\n";
143  close(JLOG);
144}
145
146# --------------------------------------------------------------------------------
147
148my $tests    = 0;
149my $skipped  = 0;
150my $passed   = 0;
151my $failed   = 0;
152my $warnings = 0;
153my $elapsed  = 0;
154my $crashed  = 0;
155my $valgrind = 0;
156
157my %duration = (); # key=unit, value=ms
158
159sub parse_log($\@) {
160  # parse reports generated by UnitTester.cxx@generateReport
161  my ($log,$junit_r) = @_;
162  open(LOG,$log) || die "can't open '$log' (Reason: $!)";
163
164  my $tests_this   = 0;
165  my $skipped_this = 0;
166  my $passedALL    = 0;
167  my $seenSummary  = 0;
168
169  my $curr_target        = undef;
170  my $last_error_message = undef;
171
172  my $unitName = 'unknownUnit';
173  if ($log =~ /\/([^\.\/]+)\.[^\/]+$/o) { $unitName = $1; }
174
175  my $dump_log = 0;
176
177  my @testcases   = ();
178  my $case_ok     = 0;
179  my $case_failed = 0;
180
181  while ($_ = <LOG>) {
182    chomp;
183    if (/^UnitTester:/) {
184      my $rest = $';
185      if ($rest =~ /^\s+\*\s+([A-Za-z0-9_]+)\s+=\s+([A-Z]*)/o) {
186        my ($testname,$result) = ($1,$2);
187        my $err = undef;
188        if ($result ne 'OK') {
189          if (defined $last_error_message) {
190            $err = $last_error_message;
191          }
192          else {
193            $err = 'unknown failure reason';
194          }
195        }
196        # append to junit log
197        my $testcase = "  <testcase name=\"$testname\" classname=\"$unitName.noclass\"";
198        if (defined $err) {
199          $testcase .= ">\n";
200          $testcase .= "   <error message=\"$err\"/>\n";
201          $testcase .= "  </testcase>";
202          $case_failed++;
203        }
204        else {
205          $testcase .= '/>';
206          $case_ok++;
207        }
208        push @testcases, $testcase;
209        $last_error_message = undef;
210      }
211
212      if (/tests=([0-9]+)/)   { $tests_this += $1; $seenSummary=1; }
213      if (/skipped=([0-9]+)/) { $skipped_this += $1; $dump_log = 1; }
214
215      if (/passed=([0-9]+)/)  { $passed += $1; }
216      if (/passed=ALL/)       { $passedALL = 1; }
217
218      if (/failed=([0-9]+)/)  { $failed += $1; $dump_log = 1; }
219      if (/warnings=([0-9]+)/)  { $warnings += $1; if ($failed==0) { $dump_log = 1; } }
220      if (/target=([^\s]+)/)  { $curr_target = $1; }
221      if (/time=([0-9.]+)/)   {
222        $elapsed += $1;
223        if (not defined $curr_target) { die "Don't know current target"; }
224        $duration{$curr_target} = $1;
225      }
226      if (/valgrind.*error/)  { $valgrind++; $dump_log = 1; }
227      if (/coverage/)  { $dump_log = 1; }
228    }
229    elsif (/^[^\s:]+:[0-9]+:\s+Error:\s+/o) {
230      if (not /\(details\sabove\)/) {
231        $last_error_message = $';
232      }
233    }
234    elsif (/^-+\s+(ARB-backtrace.*):$/) {
235      $last_error_message = $1;
236    }
237  }
238  close(LOG);
239
240  # write whole suite to junit log
241  {
242    my $case_all = $case_ok + $case_failed;
243    # my $stamp    = localtime;
244    my $stamp    = `date "+%Y-%m-%dT%T.%N%:z"`;
245    chomp($stamp);
246    push @$junit_r, " <testsuite name=\"$unitName\" tests=\"$case_all\" failures=\"$case_failed\" timestamp=\"$stamp\">";
247    foreach (@testcases) { push @$junit_r, $_; }
248    push @$junit_r, " </testsuite>";
249  }
250
251  if (not $seenSummary) { $dump_log = 1; }
252
253  $dump_log = 1; # @@@ temporarily dump all logs (to detect what's so slow on colab)
254
255  if ($dump_log==1) {
256    open(LOG,$log) || die "can't open '$log' (Reason: $!)";
257    my $line;
258    while (defined($line=<LOG>)) { print_expand_pathless_messages($line); }
259    close(LOG);
260  }
261  else {
262    my $log_ptr = $log;
263    $log_ptr =~ s/^\./UNIT_TESTER/;
264    # print "Suppressing dispensable $log_ptr\n";
265  }
266
267  if (not $seenSummary) {
268    my $ARBHOME = $ENV{ARBHOME};
269    print "$ARBHOME/UNIT_TESTER/$log:1:0: Warning: No summary found in '$log' (maybe the test did not compile or crashed)\n";
270    $crashed++;
271  }
272
273  $tests   += $tests_this;
274  $skipped += $skipped_this;
275
276  if ($passedALL==1) { $passed += ($tests_this-$skipped_this); }
277}
278
279sub percent($$) {
280  my ($part,$all) = @_;
281  if ($all) {
282    my $percent = 100*$part/$all;
283    return sprintf("%5.1f%%", $percent);
284  }
285  else {
286    $part==0 || die;
287    return "  0.0%";
288  }
289}
290
291sub slow_note() {
292  return (shall_run_slow() ? "" : " (slow tests skipped)");
293}
294
295my $BigOk = <<EndOk;
296  __  __ _    _  _
297 /  \\(  / )  (_)( \\
298(  O ))  (    _  ) )
299 \\__/(__\\_)  (_)(_/
300EndOk
301
302my $BigFailed = <<EndFailed;
303 ____  __   __  __    ____  ____   _
304(  __)/ _\\ (  )(  )  (  __)(    \\ / \\
305 ) _)/    \\ )( / (_/\\ ) _)  ) D ( \\_/
306(__) \\_/\\_/(__)\\____/(____)(____/ (_)
307EndFailed
308
309
310sub readableDuration($) {
311  # result should not be longer than 9 characters! (5 chars value, space, 3 chars unit)
312  my ($ms) = @_;
313  if ($ms>5000) {
314    my $sec = $ms / 1000.0;
315    if ($sec>99) {
316      my $min = $sec / 60.0;
317      return sprintf("%5.2f min", $min);
318    }
319    return sprintf("%5.2f sec", $sec);
320  }
321  return sprintf("%5i ms ", $ms);
322}
323
324sub print_summary($) {
325  my ($tests_failed) = @_;
326  print "\n-------------------- [ Unit-test summary ] --------------------\n";
327
328  my @summary = ();
329
330  push @summary, sprintf(" Tests   : %5i", $tests);
331  push @summary, sprintf(" Skipped : %5i =%s%s", $skipped, percent($skipped,$tests), slow_note());
332  push @summary, sprintf(" Passed  : %5i =%s", $passed, percent($passed,$tests));
333  push @summary, sprintf(" Failed  : %5i =%s", $failed, percent($failed,$tests));
334  push @summary, sprintf(" Sum.dur.: %9s", readableDuration($elapsed));
335  {
336    my @slowest = sort {
337      $duration{$b} <=> $duration{$a};
338    } map {
339      if ((defined $_) and ($_ ne '') and $duration{$_}>0) { $_; }
340      else { ; }
341    } keys %duration;
342
343    my $show = scalar(@slowest);
344    if ($show>3) { $show = 3; }
345    if ($show>0) {
346      for (my $s=0; $s<$show; ++$s) {
347        my $slowunit = $slowest[$s];
348        push @summary, sprintf("%s%9s (%s)", ($s==0 ? " Max.dur.: " : "           "), readableDuration($duration{$slowunit}), $slowunit);
349      }
350    }
351  }
352  push @summary, sprintf(" Crashed : %5i units", $crashed);
353  push @summary, sprintf(" Warnings: %5i", $warnings);
354  if ($valgrind>0) {
355    push @summary, sprintf(" Valgrind: %5i failures", $valgrind);
356  }
357
358  my @big;
359  my $Big = $tests_failed ? $BigFailed : $BigOk;
360  @big= split '\n', $Big;
361
362  my $vOffset = scalar(@summary) - scalar(@big);
363  if ($vOffset<0) { $vOffset = 0; }
364
365  my $col = 0;
366  for (my $i=0; $i<scalar(@big); $i++) {
367    my $j = $i+$vOffset;
368    my $len = length($summary[$j]);
369    if ($len>$col) { $col = $len; }
370  }
371
372  $col += 6; # add horizontal offset
373
374  for (my $i=0; $i<scalar(@big); $i++) {
375    my $j = $i+$vOffset;
376    my $padded = $summary[$j];
377    my $len = length($padded);
378    while ($len<$col) { $padded .= ' '; $len++; }
379    $summary[$j] = $padded.$big[$i];
380  }
381
382  foreach (@summary) { print $_."\n"; }
383}
384
385sub do_report() {
386  my @junit = ();
387  my @logs = get_existing_logs();
388  foreach (@logs) {
389    parse_log($_,@junit);
390  }
391
392  dump_junitlog(@junit);
393
394  my $tests_failed = (($failed>0) or ($crashed>0) or ($valgrind>0));
395  print_summary($tests_failed);
396  slow_cleanup($tests_failed);
397  if ($tests_failed) {
398    die "tests failed\n";
399  }
400  return undef;
401}
402
403sub check_obsolete_restricts() {
404  my $restrict = $ENV{CHECK_RESTRICT};
405  if (not defined $restrict) {
406    print "Can't check restriction (empty)\n";
407  }
408  else {
409    $restrict = ':'.$restrict.':';
410    if ($restrict =~ /:(WINDOW|ARBDB|AWT|CORE):/) {
411      my $lib = $1;
412      my $msl = 'Makefile.setup.local';
413
414      print "UNIT_TESTER/$msl:1: Error: Obsolete restriction '$lib' (should be 'lib$lib')\n";
415      my $grepcmd = "grep -n \'RESTRICT_LIB.*=.*$lib\' $msl";
416      open(GREP,$grepcmd.'|') || die "can't execute '$grepcmd' (Reason: $?)";
417      foreach (<GREP>) {
418        print "UNIT_TESTER/$msl:$_";
419      }
420      die;
421    }
422  }
423}
424
425# --------------------------------------------------------------------------------
426
427sub main() {
428  my $error = undef;
429  my $cb    = undef;
430  {
431    my $args = scalar(@ARGV);
432    if ($args==3) {
433      my $command   = shift @ARGV;
434
435      if ($command eq 'init') { $cb = \&do_init; }
436      elsif ($command eq 'report') { $cb = \&do_report; }
437      else { $error = "Unknown command '$command'"; }
438
439      if (not $error) {
440        $logdirectory = shift @ARGV;
441        $slow_delay = shift @ARGV;
442      }
443    }
444    else {
445      $error = 'Wrong number of arguments';
446    }
447  }
448  if ($error) {
449    print "Usage: reporter.pl [init|report] logdirectory slow-delay\n";
450    print "       slow-delay    >0 => run slow tests only every slow-delay minutes\n";
451  }
452  else {
453    check_obsolete_restricts();
454    eval { $error = &$cb(); };
455    if ($@) { $error = $@; }
456  }
457  if ($error) { die "Error: ".$error; }
458}
459main();
Note: See TracBrowser for help on using the repository browser.