#! /bin/sh
# The SPIMS software is covered by a license. The use of the software
# represents acceptance of the terms and conditions in the license.
# ******************************************************************
# Copyright (c) 1989, Swedish Institute of Computer Science
# parameters: {0,1,2} host names and protocol names
DEF_PROTOCOLS="udp tcp ftp sunrpc tcprpc"
DEF_HOST=`hostname`
DEFAULT_FILTER=basic
PROTOCOLS=""
CLIENTHOST=""
SERVERHOST=""
VERBOSE=0
STARTED=0

cd $SPIMSDIR
if [ $# = 0 ]; then
    echo 'Usage: benchmark [-v] <name> [[<client host>] <server host>] [<protocols>]'
    exit 1
fi
if [ $1 = "-v" ]; then
    VERBOSE=1; shift
fi
if [ -f benchmarks/$1 ]; then
    BENCHMARK=$1
    shift;
else
    echo Can\'t find benchmark specification for $1
    exit 1
fi
if [ -f filters/$BENCHMARK ]; then
    FILTER=$BENCHMARK
else
    FILTER=$DEFAULT_FILTER
fi

for p in $@; do
    if [ -d $p ]; then
        PROTOCOLS="$PROTOCOLS $p"
    else
	if [ "$CLIENTHOST" = "" ]; then
		CLIENTHOST=$p
	elif [ "$SERVERHOST" = "" ]; then
		SERVERHOST=$p
	else
		echo Usage: at most two host names: $p
		exit 1
	fi
    fi
done
if [ "$SERVERHOST" = "" ]; then
    SERVERHOST=$CLIENTHOST
    CLIENTHOST=""
fi
if [ "$SERVERHOST" = "" ]; then
    SERVERHOST=$DEF_HOST
fi
if [ "$CLIENTHOST" = "" ]; then
    CLIENTHOST=$DEF_HOST
fi

if [ "$PROTOCOLS" = "" ]; then
    PROTOCOLS=$DEF_PROTOCOLS
fi
#
if [ $VERBOSE = 1 ]; then
    echo Benchmark $BENCHMARK, filter $FILTER
    echo Protocols: $PROTOCOLS
    echo From $CLIENTHOST to $SERVERHOST
fi
#
# Make sure that there are demons running where needed
#
for h in $CLIENTHOST $SERVERHOST; do
    for p in $PROTOCOLS; do
        if `$p/bench $h <benchmarks/test >/dev/null 2>&1 `; then
            if [ $VERBOSE = 1 ]; then
                echo A demon for the $p protocol on $h is already running
            fi
        else
            if [ $VERBOSE = 1 ]; then
                echo Starting a demon for the $p protocol on $h
            fi
            startdemon $h $p
            STARTED=1
        fi
    done
done

#
# Test that all demons are running
#
if [ $STARTED != 0 ]; then
    if [ $VERBOSE = 1 ]; then
        echo Checking that the demons came alive
    fi
    sleep 10
    for h in $CLIENTHOST $SERVERHOST; do
        for p in $PROTOCOLS; do
            if `$p/bench $h <benchmarks/test >/dev/null 2>&1 `; then
                echo -n ""
            else
                echo Failed starting a demon on $h for the $p protocol
                exit 2
            fi
        done
    done
fi
#
# Do the measurements
#	Check the exit value??
#
for p in $PROTOCOLS; do
    echo "Measuring between $CLIENTHOST and $SERVERHOST: $p protocol..."
    $p/bench $CLIENTHOST $SERVERHOST <benchmarks/$BENCHMARK 2>&1 | filters/$FILTER
done




