#!/afs/athena/contrib/perl/perl5

sub sample {
    local($what,$x,$count) = @_;

    if (!$count) { $count = 1; }

    push(@{$samples{$what}}, ($x)x$count);
    $sum{$what} += $x*$count;
    $sumsquare{$what} += $x*$x*$count;
    $count{$what} += $count;
}

sub mean { $sum{$_[0]}/$count{$_[0]}; }

sub stddev { sqrt(($sumsquare{$_[0]}/$count{$_[0]})-
		  ($sum{$_[0]}/$count{$_[0]})*($sum{$_[0]}/$count{$_[0]})); }

sub stats {
    local($what) = @_;

    if (!$count{$what}) {
	print "no stats for $what\n\n";
    } else {
	print " count $what = ",$count{$what},"\n";
	print "  mean $what = ",&mean($what),"\n";
	@tmp = (sort {$a<=>$b} @{$samples{$what}});
	print "median $what = ",$tmp[$#tmp/2],"\n";
	print "stddev $what = ",&stddev($what),"\n";
	print "\n";
    }
}

while(<>) {
    chop;

    /^Jan (\d+) (\d+):(\d+):(\d+)/ || next;

    ### offset in seconds from midnight, 1 october 1995
    $time = ($1-1)*86400+$2*3600+$3*60+$4;
    $msg = $';

    $firsttime = $time if !$firsttime;
    $lasttime = $time;

    if (/pksd: mail_req:.*: incremental/i) {
	$mail_incremental++;
    } elsif (/pksd: mail_req:.*: add/i) {
	$mail_add++;
    } elsif (/pksd: mail_req:.*: get/i) {
	$mail_get++;
    } elsif (/pksd: mail_req:.*: index/i) {
	$mail_index++;
    } elsif (/pksd: mail_req:.*: verbose index/i) {
	$mail_vindex++;
    }

    if (m,/pks/add,) {
	$web_add++;
    } elsif (/op=index/i) {
	$web_index++;
    } elsif (/op=vindex/i) {
	$web_vindex++;
    } elsif (/op=get/i) {
	$web_get++;
    }

    if (/pksd: kd_(add|index|get):/i) {
	$what = $1;
	if (/completed/i) {
	    &sample("time:".$what, $time-$reqstart);
	} else {
	    $reqstart = $time;
	}
    }
}

print "add: mail + web = ",$mail_incremental+$mail_add," + $web_add\n";
&stats("time:add");
print "index: mail + web = ",$mail_index+$mail_vindex," + ",
    $web_index+$web_vindex,"\n";
&stats("time:index");
print "get: mail + web = $mail_get + $web_get\n";
&stats("time:get");

print "total time is ",$lasttime-$firsttime," seconds (",
    ($lasttime-$firsttime)/86400," days).\n";
