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

=head1 SYNOPSIS

=head1 DESCRIPTION

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

=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;
}

my @result;

sub findHandler
{
    my ($fileName) = $_;
    my ($testCase,$testFunction);
    my $file = new IO::File('<' . $fileName) or die ('Unable to open ' . $fileName . ' for reading: ' . $!);
    while (my $line = <$file>) {
	chomp($line);
	if ($line =~ /^<TestCase name="([^"]*).*/) {
	    $testCase = $1;
	    $testCase =~ s/&#x002D;/-/g;
	} elsif ($line =~ /^<TestFunction name="([^"]*).*/) {
	    $testFunction = $1;
	    $testFunction =~ s/&#x002D;/-/g;
# Check for duration with blanks, duration without blanks is total, which we are not interested in . 
	} elsif ($line =~ /^  *<Duration msecs="([^"]*).*/) {
	    my $duration =  POSIX::ceil($1);
	    my $line = sprintf("%010d:%s:%s",$duration,$testCase,$testFunction);
	    push (@result, $line);
	}
    }
    $file->close();
}

# -- main

find({ wanted => \&findHandler}, getcwd());

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