#!/bin/sh
#
# Wrapper for automating benchmarking runs.
# Usage:   bench passes user group [script]
#
# ..where passes is the number of times to run each script; uid/gid
# gives credentials to use when running the script; and script is a
# simple wrapper around each actual benchmark tool (eg. see run.*),
# if this is ommited, all run.* scripts are used in turn.
#
# Each run.foo script should report a comma-separated-value list of
# benchmark results on stdout or fail with a non-zero exit code;
# unless the -i option is supplied in which case it should instead
# report a comma-separated-value list of column headers (for report
# generation purposes).
#
#-----------------------------------------------------------------------
# Copyright (c) 2002-2003 Silicon Graphics, Inc.  All Rights Reserved.
#-----------------------------------------------------------------------
#
# creator
owner=nathans@sgi.com

iam=bench
tmp=/tmp/$$
here=`pwd`; export here
status=1	# failure is the default!

# get standard environment, filters and checks
. ./common.rc
. ./common.filter

_cleanup()
{
    echo "        *** umount"
    umount $SCRATCH_DEV >/dev/null 2>&1
    rm -f $tmp.*
}

OUT="bench.out"
LOG="bench.log"
FULL="bench.full"

_log()
{
    echo "$*" 1>&2
    echo "$*" >>$LOG
    echo "$*" >>$FULL
    sync
}

_logp()
{
    tee -a $FULL
}

_fail()
{
    _log "$*"
    status=1
    exit 1
}

_run_benchmark()
{
    pass=1
    uid=`id -u $user`
    gid=`id -g $group`
    
    while [ $pass -le $passes -o $passes -lt 0 ]
    do
        _log "        *** clean scratch device [$bench starting, pass $pass]"
        _scratch_mkfs 2>&1 | _fix_malloc >>$FULL
        _log "        *** mounting scratch device"
        _scratch_mount      || _fail "            !!! failed to mount"
        
        _log "        *** mkdir"
        mkdir $SCRATCH_MNT/bench \
                            || _fail "            !!! couldn't mkdir benchdir"
        chown -R $user.$group $SCRATCH_MNT/bench \
                            || _fail "            !!! couldn't chown benchdir"

        cd $SCRATCH_MNT/bench
        seq=`perl -e 'printf "results.%s.%03d\n", '$bench', '$pass`
        rm -f $seq $tmp.out

        _log "        *** bench [$seq]"
        $here/src/runas -u $uid -g $gid $here/run.$bench >$tmp.out 2>>$FULL
	[ $? -eq 0 ]        || _fail "            !!! $bench pass $pass failed"

        cd $here
        _fix_malloc < $tmp.out > $seq

        _log "        *** unmounting scratch device"
        umount $SCRATCH_DEV 2>&1 | _logp \
                            || _fail "            !!! failed to umount"

        _log "        *** post-umount filesystem check"
        _check_scratch_fs
        
        let "pass = pass + 1"
    done
}

_merge_results()
{
    echo Results for $bench benchmark
    $here/run.$bench -h
    echo results.$bench.* | sort -nu | xargs cat
    echo
}

# real QA test starts here

if [ $# -lt 3 ]; then
    echo Usage:  bench passes user group [script]
    exit 1
fi

passes=$1
user=$2
group=$3
shift; shift; shift

if [ $# -gt 0 ]; then
    benches="$@"
else
    benches=`echo run.* | sed -e 's/run\.//g'`
fi
[ -z "$benches" -o "$benches" = "*" ] && _fail "no benchmark scripts found"

trap "_cleanup; exit \$status" 0 1 2 3 15

_require_scratch
rm -f bench.* results.*

FULL_FSTYP_DETAILS=`_full_fstyp_details`
FULL_HOST_DETAILS=`_full_platform_details`
FULL_MKFS_OPTIONS=`_scratch_mkfs_options`
FULL_MOUNT_OPTIONS=`_scratch_mount_options`

# $OUT is the report which will ultimately be sent, keep it tidy.
cat >$OUT <<EOF
FSTYP         -- $FULL_FSTYP_DETAILS
PLATFORM      -- $FULL_HOST_DETAILS
MKFS_OPTIONS  -- $FULL_MKFS_OPTIONS
MOUNT_OPTIONS -- $FULL_MOUNT_OPTIONS

EOF

for bench in $benches
do
    echo "" >>$FULL
    echo "" >$LOG
    _log "*** benchmark started [passes=$passes, benchmark=$bench]"
    _log "*** (`date`)"
    _log "MKFS_OPTIONS  -- $FULL_MKFS_OPTIONS"
    _log "MOUNT_OPTIONS -- $FULL_MOUNT_OPTIONS"
    _log "        *** unmounting scratch device"
    umount $SCRATCH_DEV 2>&1 | _fix_malloc >>$FULL

    _run_benchmark | _fix_malloc
    _merge_results >>$OUT

    _log "*** done $bench"
done
status=0
