#!/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";
	@h = (); $hh = "";
	grep($_<7900 && ($h[$_/100]+=1/2),@tmp);
	grep(($n=$_,grep($hh.=($_<=$n)?" ":"X",@h),$hh.="\n"),reverse(0..20));
	print " histo $what = \n$hh";
#	print "samples = ",join(",",@tmp),"\n";
	print "\n";
    }
}

while(<>) {
    chop;

    /^.* - - \[(\d+)\/Oct\/1995:(\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 (/pks-extract-key/i) {
	split;
	$size = $_[$#_];
	if (/op=index/i) {
	    if ($size eq "-") {
		$indexfailed++;
	    } else {
		&sample("index",$size);
	    }
	} elsif (/op=vindex/i) {
	    if ($size eq "-") {
		$vindexfailed++;
	    } else {
		&sample("vindex",$size);
	    }
	} elsif (/op=get/i) {
	    if (/search=0x/i) {
		if ($size eq "-") {
		    $getidfailed++;
		} else {
		    &sample("getid",$size);
		}
	    } else {
		if ($size eq "-") {
		    $getnamefailed++;
		} else {
		    &sample("getname",$size);
		}
	    }
	}
    } elsif (/pks-add-key/i) {
	if ($size !~ /-/) {
	    &sample("add", $size);
	}
    }
}

print " count index failed = ",$indexfailed,"\n";
&stats("index");
print "count vindex failed = ",$vindexfailed,"\n";
&stats("vindex");
print "count getid failed = ",$getidfailed,"\n";
&stats("getid");
print "count getname failed = ",$getnamefailed,"\n";
&stats("getname");
&stats("add");

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