#!/bin/bash

#
# Use this script to generate performance for the current suite of
# tests across the past several releases (those that are outlined
# below).
#
# To use it:
#
# * note that this script is currently hard-coded to our usage of it
#   at Cray in terms of referring to certain paths and pre-installed
#   locations of past releases.  With more effort, it could be
#   parameterized for external users, if desired.
#
# * set CHPL_HOME to indicate the current CHPL_HOME that you want to
#   use.  This hierarchy's util/ scripts will be used to run the
#   tests.
#
# * NOTE: To get results that are optimal / comparable to those
#   obtained in nightly performance runs, you need to host your
#   $CHPL_HOME from a local disk like /tmp rather than from an
#   NFS-mounted directory (BRAD: This means you, dummy).
#
# * make sure that your $CHPL_HOME is set up to run testing (e.g.,
#   can you run 'start_test'?)  If not, do a normal 'make test-venv'.
#
# * optionally, set CHPL_TEST_VENV_DIR if you want it to differ
#   from the default below.
#
# * optionally, set CHPL_TEST_PERF_DIR to indicate where you want
#   the output .dat and html/ files to be placed.  By default, it
#   will use $CHPL_HOME/test/perfdat-releases/
#
# * cd to the test/ directory (or subdirectory) that you want to test
#   (typically $CHPL_HOME/test) and run testReleasesPerformance.  As
#   in a normal test run, this will test all subdirectories under the
#   cwd.
#
# * the script will then run the current performance tests across all
#   past releases.
#

function testReleasePerformance {
    pushd $CHPL_HOME
    . util/setchplenv.bash
    popd
    $CHPL_TEST_UTIL_DIR/start_test -performance --compopts --no-cc-warnings -num-trials 3
}

if [[ -z $CHPL_HOME ]]; then
   echo "ERROR: CHPL_HOME must be set" && exit 1
fi

export CHPL_HOME_ORIG=$CHPL_HOME
export CHPL_TEST_UTIL_DIR=$CHPL_HOME_ORIG/util

if [[ -z $CHPL_TEST_VENV_DIR ]]; then
    export CHPL_TEST_VENV_DIR=`python $CHPL_TEST_UTIL_DIR/chplenv/chpl_home_utils.py --test-venv`
fi
if [[ ! -d $CHPL_TEST_VENV_DIR ]]; then
    echo "ERROR: incorrect or missing CHPL_TEST_VENV_DIR='$CHPL_TEST_VENV_DIR'"
    exit 1
fi

#
# This is a place where all releases will be unpacked and built with
# optimizations on, debugging off.
#
export chpl_release_home=/data/cf/chapel/chapel-releases

if [[ -z $CHPL_TEST_PERF_DIR ]]; then
    export CHPL_TEST_PERF_DIR=$CHPL_HOME/test/perfdat-releases
fi

echo "Writing output to $CHPL_TEST_PERF_DIR"

export CHPL_TEST_PERF_DATE="04/10/12"
export CHPL_HOME=$chpl_release_home/chapel-1.5
testReleasePerformance

export CHPL_TEST_PERF_DATE="10/8/12"
export CHPL_HOME=$chpl_release_home/chapel-1.6.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="04/5/13"
export CHPL_HOME=$chpl_release_home/chapel-1.7.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="10/04/13"
export CHPL_HOME=$chpl_release_home/chapel-1.8.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="04/07/14"
export CHPL_HOME=$chpl_release_home/chapel-1.9.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="09/22/14"
export CHPL_HOME=$chpl_release_home/chapel-1.10.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="03/25/15"
export CHPL_HOME=$chpl_release_home/chapel-1.11.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="09/24/15"
export CHPL_HOME=$chpl_release_home/chapel-1.12.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="03/29/16"
export CHPL_HOME=$chpl_release_home/chapel-1.13.1
testReleasePerformance

export CHPL_TEST_PERF_DATE="09/27/16"
export CHPL_HOME=$chpl_release_home/chapel-1.14.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="03/27/17"
export CHPL_HOME=$chpl_release_home/chapel-1.15.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="09/27/17"
export CHPL_HOME=$chpl_release_home/chapel-1.16.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="03/28/18"
export CHPL_HOME=$chpl_release_home/chapel-1.17.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="09/12/18"
export CHPL_HOME=$chpl_release_home/chapel-1.18.0
testReleasePerformance

export CHPL_TEST_PERF_DATE="03/12/19"
export CHPL_HOME=$chpl_release_home/chapel-1.19.0
testReleasePerformance

#
# ADD NEW RELEASES HERE AS THEY ARE CREATED.
#
# (The date should be the date of the release branch, which should
# match the date in $CHPL_HOME/util/test/perf/perfgraph.js)
#

unset CHPL_TEST_PERF_DATE
export CHPL_HOME=$CHPL_HOME_ORIG
$CHPL_TEST_UTIL_DIR/start_test --gen-graphs





#
# This final call gets the performance from the current $CHPL_HOME.
# As we develop the ability to splice different .dat files together
# better, we will probably want to retire this and just splice in
# data from the nightly file.
#

#export -n CHPL_TEST_PERF_DATE
#export CHPL_HOME=$CHPL_HOME_ORIG
#testReleasePerformance
