#!/usr/bin/perl

# analyze-multiplier.pl
#
# By Brian D. Davison
#
# Last Modified: 1 July 2001
#
# Goals:
#
# Reads multiplier log files
#
# Usage: grep RESULT <filename> | ./analyze-multiplier.pl

#use Socket;

while (<STDIN>) {
    local($line) = $_;

    ($day,$mon,$date,$time,$misc1,$proxy,$time1,$time2,$size,$misc2,$clientip,$type,$version,$method,@rest) = split(' ',$line);

    $url = join(' ',@rest);
    chop $misc2;
    chop $misc2;
    $misc2 = substr($misc2,1);
    ($ig1,$ig2,@comp) = split(',',$misc2);

    if (($comp[0] ne "OK") &&
	($ig1 ne "DUMMY")) {
	$reqstale{$proxy}++;
#	print "NOTOK: ", join(',',@comp), "\n";
    } else {
	($ts1,$tus1) = split(':',$time1);
	($ts2,$tus2) = split(':',$time2);
    
	$reqsok{$proxy}++;
	$ttime = ($tus1+$tus2)/1000000 + ($ts1+$ts2);
	$td{$proxy}[int (($tus1+$tus2)/1000 + 1000*($ts1+$ts2))]++;
	$totaltime{$proxy} += $ttime;

	$byteserved{$proxy} += $size;
    }
    $lines++;
}

print "\n$lines log entries analyzed.\n";
print "proxyname, MBytes retrieved, responses, %ok, %notOK, ave latency (s)\n\n";

foreach $i (keys %byteserved)
{
    printf("%s %.3f %d %.2f%% %.2f%% %f\n", $i, $byteserved{$i}/1024/1024, 
	   $reqsok{$i}+$reqstale{$i}, 100 * $reqsok{$i} / ($reqsok{$i} +
           $reqstale{$i}), 100 * $reqstale{$i} / ($reqsok{$i} + $reqstale{$i}),
	   $totaltime{$i} / $reqsok{$i});
}

foreach $i (keys %td)
{
    $totfract  = 0; $q1 = $q2 = $q3 = 0;
    print "For proxy $i:\n";
    foreach $j (0..$#{$td{$i}}) {
	$fract = $td{$i}[$j] / $reqsok{$i};
	$totfract += $fract;
	printf("CDF $i %.3f %.3f %.3f\n", $j/1000, $fract, $totfract);
    }
}


