#!/bin/bash
#
# This is a policy benchmark script
#
# ARG_POSITIONAL_SINGLE([agent],[path to agent],[])
# ARG_POSITIONAL_SINGLE([policy],[path to policy file],[])
# ARG_POSITIONAL_SINGLE([n],[number of times to run policy],[])
# ARG_HELP([Policy Benchmark Script])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.9.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
# Generated online by https://argbash.io/generate


die()
{
    local _ret="${2:-1}"
    test "${_PRINT_HELP:-no}" = yes && print_help >&2
    echo "$1" >&2
    exit "${_ret}"
}


begins_with_short_option()
{
    local first_option all_short_options='h'
    first_option="${1:0:1}"
    test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}

# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
# THE DEFAULTS INITIALIZATION - OPTIONALS


print_help()
{
    printf '%s\n' "Policy Benchmark Script"
    printf 'Usage: %s [-h|--help] <agent> <policy> <n>\n' "$0"
    printf '\t%s\n' "<agent>: absolute path to agent"
    printf '\t%s\n' "<policy>: absolute path to policy file"
    printf '\t%s\n' "<n>: number of times to run policy"
    printf '\t%s\n' "-h, --help: Prints help"
}


parse_commandline()
{
    _positionals_count=0
    while test $# -gt 0
    do
        _key="$1"
        case "$_key" in
            -h|--help)
                print_help
                exit 0
                ;;
            -h*)
                print_help
                exit 0
                ;;
            *)
                _last_positional="$1"
                _positionals+=("$_last_positional")
                _positionals_count=$((_positionals_count + 1))
                ;;
        esac
        shift
    done
}


handle_passed_args_count()
{
    local _required_args_string="'agent', 'policy' and 'n'"
    test "${_positionals_count}" -ge 3 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 3 (namely: $_required_args_string), but got only ${_positionals_count}." 1
    test "${_positionals_count}" -le 3 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 3 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
}


assign_positional_args()
{
    local _positional_name _shift_for=$1
    _positional_names="_arg_agent _arg_policy _arg_n "

    shift "$_shift_for"
    for _positional_name in ${_positional_names}
    do
        test $# -gt 0 || break
        eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
        shift
    done
}

parse_commandline "$@"
handle_passed_args_count
assign_positional_args 1 "${_positionals[@]}"

# OTHER STUFF GENERATED BY Argbash

### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash

# Collect only real result from `time` command
TIMEFORMAT=%R

sum=0
array=()
for (( i=0; i<$_arg_n; i++))
do
    time=$(time ($_arg_agent -KIf $_arg_policy &>/dev/null) 2>&1)
    sum=$(echo "$sum + $time" | bc -l)
    array+=($time)
    printf "bench %d: %.03f s\n" $i $time
done

# Sort array of results
IFS=$'\n' sorted=($(sort <<<"${array[*]}")); unset IFS

echo
echo "*** RESULTS ***"

# Max time
max=$(($NUM_RUNS-1))
printf "max    : %.03f s\n" "${sorted[$max]}"

# Min time
printf "min    : %.03f s\n" "${sorted[0]}"

# Mean time
mean=$(echo "$sum / $_arg_n" | bc -l)
printf "mean   : %4.03f s\n" $mean

# Median time
half=$(echo "$_arg_n / 2" | bc -l)
half=$(printf "%.0f" "$half")
printf "median : %.03f s\n" "${sorted[$half]}"

# ] <-- needed because of Argbash
