# Copyright (c) 1997 The President and Fellows of Harvard College.
# All rights reserved.
# Copyright (c) 1997 Aaron B. Brown.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program, in the file COPYING in this distribution;
#   if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
#   Cambridge, MA 02139, USA.
#
# Results obtained from this benchmark may be published only under the
# name "HBench-OS".

#
# stats-full
#
# This perl script contains the default statistical policy used in producing
# summary output. It reads in a data file produced by an HBench-OS
# benchmark and spits out one line of analysis to stdout.
#
# The policy implemented here is a 20% trimmed mean: the top and bottom
# 20% of the data are discarded; the remainder is averaged and the 
# mean, median, and standard deviation are reported.
#
# This policy is easily replaced if you desire something different.
#
# $Id: stats-full,v 1.1.1.1 2003/02/28 17:19:54 fedorova Exp $

eval "exec perl $0 $*"
	if 0;

unless ($ARGV[0]) {
	die "Usage: stats-full <data-file>\n";
}

##
## ADJUST THIS TO CHANGE THE SIZE OF THE TRIMMING IN THE TRIMMED MEAN
##
$tailpercentage = 0.2;

$infilename = $ARGV[0];

# test the input file to insure it exists
unless (-e $infilename && -r $infilename) {
    die "Unable to open input file $infilename\n";
}

# open the file
open(INFILE,$infilename);

while (<INFILE>) {
    chop();
    # Only use the first number -- this way we ignore event counter values
    ($val) = split();

    push(@vals, $val);
    $count++;
}

# Now sort the numbers
@vals = sort {$a <=> $b;} @vals;

# Get the median
if ($count % 2 != 0) {
    $median = @vals[int($count/2)];
} else {
    $median = (@vals[int($count/2)] + @vals[int($count/2) - 1])/2.0;
}

# Now average the middle 60%
$tailsize = int($count*$tailpercentage);
$sum = 0;
$sumsq = 0;

for ($i = $tailsize; $i < $count - $tailsize; $i++) {
    $sum += @vals[$i];
    $sumsq += @vals[$i]*@vals[$i];
}
# XXX count -1 ???
$count -= 2*$tailsize;
$avg = $sum/$count;
$std = (1/($count)) * ($sumsq - 2*$avg*$sum + $count*$avg*$avg);
$std = sqrt($std);

printf("%f (std. %f, %.2f%%) [median %f]\n",$avg,$std,100*$std/$avg,$median);
