File: analyze_tests.pl

package info (click to toggle)
libtest-harness-perl 3.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,764 kB
  • sloc: perl: 18,954; sh: 18; makefile: 7
file content (86 lines) | stat: -rw-r--r-- 2,093 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env perl

use strict;
use warnings;

use lib 'lib';
use App::Prove::State;
use List::Util 'sum';
use Lingua::EN::Numbers 'num2en';
use Text::Table;
use Carp;

sub minutes_and_seconds {
    my $seconds = shift;
    return ( int( $seconds / 60 ), int( $seconds % 60 ) );
}

my $state      = App::Prove::State->new( { store => '.prove' } );
my $results    = $state->results;
my $generation = $results->generation;
my @tests      = $results->tests;

my $total = sum( map { $_->elapsed } @tests );
my ( $minutes, $seconds ) = minutes_and_seconds($total);

my $num_tests = shift || 10;
my $total_tests = scalar $results->test_names;

if ( $num_tests > $total_tests ) {
    $num_tests = $total_tests;
}

my $num_word = num2en($num_tests);

my %time_for;
foreach my $test (@tests) {
    $time_for{ $test->name } = $test->elapsed;
}

my @sorted_by_time_desc
  = sort { $time_for{$b} <=> $time_for{$a} } keys %time_for;

print "Number of test programs: $total_tests\n";
print "Total runtime approximately $minutes minutes $seconds seconds\n\n";
print "\u$num_word slowest tests:\n";

my @rows;
for ( 0 .. $num_tests - 1 ) {
    my $test = $sorted_by_time_desc[$_];
    my $time = $time_for{$test};
    my ( $minutes, $seconds ) = minutes_and_seconds($time);
    push @rows => [ "${minutes}m ${seconds}s", $test, ];
}

print make_table(
    [qw/Time Test/],
    \@rows,
);

sub make_table {
    my ( $headers, $rows ) = @_;

    my @rule    = qw(- +);
    my @headers = \'| ';
    push @headers => map { $_ => \' | ' } @$headers;
    pop @headers;
    push @headers => \' |';

    unless ( 'ARRAY' eq ref $rows
        && 'ARRAY' eq ref $rows->[0]
        && @$headers == @{ $rows->[0] } )
    {
        croak(
            "make_table() rows must be an AoA with rows being same size as headers"
        );
    }
    my $table = Text::Table->new(@headers);
    $table->rule(@rule);
    $table->body_rule(@rule);
    $table->load(@$rows);
    return $table->rule(@rule),
      $table->title,
      $table->rule(@rule),
      map( { $table->body($_) } 0 .. @$rows ),
      $table->rule(@rule);
}