#!/bin/bash

#######################################################################
# settings

# Min number of seconds allowed for the planner to plan the
# dax - if these limit is exeeded, the test fails. This is used
# to detect big improvements in the planner which would mean it
# is time to lower the upper (and lower) limits of this test
MIN_SECONDS=500


# Max number of seconds allowed for the planner to plan the
# dax - if these limit is exeeded, the test fails
MAX_SECONDS=2000

# set the java heap max to get the planner to not fail for
# the 4.3 branch. we keep same for master for comparison
# purposes.
export JAVA_HEAPMAX=6144

#######################################################################

set -e

TOPDIR=`pwd`

# get the dax and other files - it is big enough that we do not want it in svn
wget -nv -O  weekly_ahope.dax http://pegasus.isi.edu/static/bamboo/014-planner-performance-ahope/weekly_ahope.dax
wget -nv -O  conf/input_map.dat http://pegasus.isi.edu/static/bamboo/014-planner-performance-ahope/conf/input_map.dat
wget -nv -O  conf/output_map.dat http://pegasus.isi.edu/static/bamboo/014-planner-performance-ahope/conf/output_map.dat

START_TS=`/bin/date +'%s'`

# plan the workflow
/usr/bin/time pegasus-plan \
    -v \
    --conf pegasusrc \
    --dax weekly_ahope.dax \
    --sites local \
    -o local \
    --dir ./dags \
    --nocleanup \
    --force \
    | tee plan.out


END_TS=`/bin/date +'%s'`
DURATION=$(($END_TS - $START_TS))
echo
echo "Planner took $DURATION seconds"
echo "The lower limit was $MIN_SECONDS seconds"
echo "The upper limit was $MAX_SECONDS seconds"
echo

if [ $DURATION -gt $MAX_SECONDS ]; then
    echo "Error: Limit exceeded!"
    exit 1
fi

if [ $DURATION -lt $MIN_SECONDS ]; then
    echo "Error: Planning was faster than lower limit - time to lower limits!"
    exit 1
fi

echo "Test passed!"
exit 0

