#!/usr/bin/perl -w

# Script to process JPRT logs of JTREG test execution.
# A typical element looks the following way:
#
# TEST: compiler/5091921/Test6985295.java
#  build: 6.038 seconds
#  compile: 6.038 seconds
#  main: 0.775 seconds
# TEST RESULT: Passed. Execution successful
#
# This script extracts the times, sums them up, and then
# sorts tests according to their cumulative execution time.


use strict;
use Getopt::Std;

# process options
my %option = ();
getopt("f", \%option);
die ("Usage: process.pl -f <filename>\n") unless defined $option{f};
my $filename = $option{f};

# get all lines from file
die "Unable to open file $filename\n" unless open(IN_FILE, $filename);
open(IN_FILE, $filename);
push(my @lines, <IN_FILE>);
close(IN_FILE);

# process data
my $size = @lines;
my $i = 0;
my %timehash;

while ($i < $size) {
    my $line = $lines[$i];
    if ($line =~ /TEST: (.*)/) {
	my $testname = $1;
	$i++;

	while ($i < $size) {
	    $line = $lines[$i];
            # we do not use (build) because if build != 0
	    # then time is already reported by compile.
	    if ($line =~ /  (compile|shell|main|clean): ([0-9\.]*)/) {
		my $category = $1;
		my $time = $2;
		if (defined $timehash{$testname}) {
		    $timehash{$testname} += $time;
		} else {
		    $timehash{$testname} = $time;
		}
		$i++;
	    } elsif ($line =~ /  build*/) {
		$i++;
	    } else {
		$i++;
		last;
	    }
	}
    } else {
	$i++;
    }
}

close(IN_FILE);

# print result

my @tests = sort(keys %timehash);
my $total_time = 0;

foreach my $test (@tests) {
    print $test . " | " . $timehash{$test} . "\n";
    $total_time += $timehash{$test};
}

print "=======================================================\n";
print "Total time is: $total_time\n";

