#! /bin/bash
. acsstartupAcsPorts
#--------------------------------------
CL_RHOST=

# These options can be recognized (longopts comma-separated. colon means argument is required)
LONGOPTS=help,rhost:
SHORTOPTS=h

# Usage info. Be nice and keep this up-to-date!
function printUsage {
   echo "Runs component performance tests"
   echo ""
	echo "Usage: `basename $0` [OPTIONS] "
	echo "Options: "
	echo "   -rhost remote_host        remote host"
   echo "   -h | -help                     prints this help and exits"
}

# Run getopt (posixly_correct needed). We run twice:
# First run is simply to check the commandline for correctness
# Second run is the real deal which replaces the command line args with getopt's output
export POSIXLY_CORRECT=1

getopt -n `basename $0` -Q -u -a -l $LONGOPTS $SHORTOPTS "$@" || {
   printUsage
	exit 43;
}

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

# Iterate over getopt's output and set CL_XXX variables accordingly
while : 
do
	case "$1" in
	--rhost)            CL_RHOST=$2 ; shift ;;
	--help)             CL_HELP=true ;; 
	-h)                 CL_HELP=true ;; 
	--) break ;;
	esac
	shift
done
shift

# must be unset! otherwise our custom export() function
# that is defined below doesn't get used by the shell
export POSIXLY_CORRECT=
unset POSIXLY_CORRECT

if [ "$CL_HELP" ] ; then
   printUsage
   exit 0
fi
#--------------------------------------
rm -rf $ACS_TMP
mkdir $ACS_TMP

if [ ! $CL_RHOST ]
then
    export DATABASE=../doc/compBulkData.dbm
    export OUT_HTML=../doc/ComponentBulkDataPerf.html
else
    export DATABASE=../doc/remoteCompBulkData.dbm
    export OUT_HTML=../doc/RemoteComponentBulkDataPerf.html
fi


#Startup services/manager
acsutilTATPrologue

export ACS_INSTANCE=`cat $ACS_TMP/acs_instance` 


function startupContainers
{
#Startup containers
if [ ! $CL_RHOST ]
then
    acsStartContainer -cpp  bilboContainer >& $ACS_TMP/bilboContainer.log&
    sleep 15

    acsStartContainer -java  frodoContainer >& $ACS_TMP/frodoContainer.log&
    sleep 15

    acsStartContainer -py  aragornContainer >& $ACS_TMP/aragornContainer.log&
    sleep 15
else
    export MANAGER_REFERENCE=corbaloc::`getIP`:`getManagerPort`/Manager


    echo "Starting remote containers on '$CL_RHOST'. It may be necessary to enter a password here..."
    (ssh -f $USER@$CL_RHOST acsStartContainer -cpp -m $MANAGER_REFERENCE  bilboContainer) >& $ACS_TMP/bilboContainer.log
    sleep 15

    (ssh -f $USER@$CL_RHOST acsStartContainer -java -m $MANAGER_REFERENCE frodoContainer) >& $ACS_TMP/frodoContainer.log
    sleep 15

    (ssh -f $USER@$CL_RHOST acsStartContainer -py -m $MANAGER_REFERENCE aragornContainer) >& $ACS_TMP/aragornContainer.log
    sleep 15
fi

acsStartContainer -cpp anotherCppContainer >& $ACS_TMP/anotherCppContainer.log&
sleep 15
}


function stopContainers
{
acsStopContainer anotherCppContainer  >& /dev/null
sleep 5

acsStopContainer aragornContainer >& /dev/null
sleep 5

acsStopContainer frodoContainer >& /dev/null
sleep 5

acsStopContainer bilboContainer >& /dev/null
sleep 5

sleep 5
}


#-------------------------------------------------------
#BULK DATA TESTS ARE TREATED SPECIAL FOR NOW
#DWF-for some reason or another more than three invocations
#consistently produces container segfaults on fox.ras.ucalgary.ca!
startupContainers
componentsBulkDataTest BDCPP01 BDSCPP01 3 10 #10KB
stopContainers

startupContainers
componentsBulkDataTest BDCPP01 BDSCPP01 3 100 #100KB
stopContainers

startupContainers
componentsBulkDataTest BDCPP01 BDSCPP01 3 1000 #1MB
stopContainers

startupContainers
componentsBulkDataTest BDCPP01 BDSCPP01 3 10000 #10MB
stopContainers

startupContainers
componentsBulkDataTest BDCPP01 BDSCPP01 3 100000 #100MB
stopContainers

startupContainers
componentsBulkDataTest BDCPP01 BDSCPP01 3 500000 #500MB
stopContainers

startupContainers
componentsBulkDataTest BDCPP01 BDSCPP01 3 1000000 #1GB
stopContainers

#-------------------------------------------------------
#Stop containers/manager/services
sleep 10
acsutilTATEpilogue

#Give everything time to finish...
sleep 5

#Load the data into the database
ACSPerfAnalyzer $DATABASE $ACS_TMP/bilboContainer.log
ACSPerfAnalyzer $DATABASE $ACS_TMP/frodoContainer.log
ACSPerfAnalyzer $DATABASE $ACS_TMP/aragornContainer.log
#DWF-next line is not really necessary...
ACSPerfAnalyzer $DATABASE $ACS_TMP/anotherCppContainer.log

#Generate HTML report
ACSPerfReportGen $DATABASE > $OUT_HTML
