#!/usr/bin/perl -w
=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

Prints & sorts test case durations from a plain test log.

=head1 OPTIONS

=cut

use strict;
use warnings;

use IO::File;
use File::Find;
use File::Spec;
use Cwd;
use POSIX;

sub readFile
{
    my ($fileName) = @_;
    my @lines;
    my $file = new IO::File('<' . $fileName) or die ('Unable to open ' . $fileName . ' for reading: ' . $!);
    while (my $line = <$file>) {
        push (@lines, $line);
    }
    $file->close();
    return @lines;
}

# -- main

if (scalar(@ARGV) == 0) {
    print "Usage $0 <file>\n";
    exit(0);
}

my @lines =  readFile($ARGV[0]);

my $test;
my $duration;
my @result;

for my $line (@lines) {
    chomp($line);
    if ($line =~ /\*+ Start testing of (\w+) \*+/) {
        $test = $1;
    } elsif ($line =~ /Totals:.*blacklisted, (\d+)ms/) {
        $duration = $1;
        my $s = sprintf("%010d %s",$duration,$test);
        push(@result, $s);
    }
}

my @sorted = sort(@result);
for (my $l = scalar(@sorted) - 1; $l >= 0; --$l) {
     print $sorted[$l],"\n";
}
