# 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-single
#
# 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 number on standard output.
#
# 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 is reported. For more detailed results, use stats-full.
#
# This policy is easily replaced if you desire something different.
#
# $Id: stats-single,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;

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

for ($i = $tailsize; $i < $count - $tailsize; $i++) {
    $sum += @vals[$i];
}

$count -= 2*$tailsize;
$avg = $sum/$count;

printf("%f\n",$avg);
