|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This is a policy benchmark script |
| 4 | +# |
| 5 | +# ARG_POSITIONAL_SINGLE([agent],[path to agent],[]) |
| 6 | +# ARG_POSITIONAL_SINGLE([policy],[path to policy file],[]) |
| 7 | +# ARG_POSITIONAL_SINGLE([n],[number of times to run policy],[]) |
| 8 | +# ARG_HELP([Policy Benchmark Script]) |
| 9 | +# ARGBASH_GO() |
| 10 | +# needed because of Argbash --> m4_ignore([ |
| 11 | +### START OF CODE GENERATED BY Argbash v2.9.0 one line above ### |
| 12 | +# Argbash is a bash code generator used to get arguments parsing right. |
| 13 | +# Argbash is FREE SOFTWARE, see https://argbash.io for more info |
| 14 | +# Generated online by https://argbash.io/generate |
| 15 | + |
| 16 | + |
| 17 | +die() |
| 18 | +{ |
| 19 | + local _ret="${2:-1}" |
| 20 | + test "${_PRINT_HELP:-no}" = yes && print_help >&2 |
| 21 | + echo "$1" >&2 |
| 22 | + exit "${_ret}" |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +begins_with_short_option() |
| 27 | +{ |
| 28 | + local first_option all_short_options='h' |
| 29 | + first_option="${1:0:1}" |
| 30 | + test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0 |
| 31 | +} |
| 32 | + |
| 33 | +# THE DEFAULTS INITIALIZATION - POSITIONALS |
| 34 | +_positionals=() |
| 35 | +# THE DEFAULTS INITIALIZATION - OPTIONALS |
| 36 | + |
| 37 | + |
| 38 | +print_help() |
| 39 | +{ |
| 40 | + printf '%s\n' "Policy Benchmark Script" |
| 41 | + printf 'Usage: %s [-h|--help] <agent> <policy> <n>\n' "$0" |
| 42 | + printf '\t%s\n' "<agent>: absolute path to agent" |
| 43 | + printf '\t%s\n' "<policy>: absolute path to policy file" |
| 44 | + printf '\t%s\n' "<n>: number of times to run policy" |
| 45 | + printf '\t%s\n' "-h, --help: Prints help" |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +parse_commandline() |
| 50 | +{ |
| 51 | + _positionals_count=0 |
| 52 | + while test $# -gt 0 |
| 53 | + do |
| 54 | + _key="$1" |
| 55 | + case "$_key" in |
| 56 | + -h|--help) |
| 57 | + print_help |
| 58 | + exit 0 |
| 59 | + ;; |
| 60 | + -h*) |
| 61 | + print_help |
| 62 | + exit 0 |
| 63 | + ;; |
| 64 | + *) |
| 65 | + _last_positional="$1" |
| 66 | + _positionals+=("$_last_positional") |
| 67 | + _positionals_count=$((_positionals_count + 1)) |
| 68 | + ;; |
| 69 | + esac |
| 70 | + shift |
| 71 | + done |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +handle_passed_args_count() |
| 76 | +{ |
| 77 | + local _required_args_string="'agent', 'policy' and 'n'" |
| 78 | + 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 |
| 79 | + 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 |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +assign_positional_args() |
| 84 | +{ |
| 85 | + local _positional_name _shift_for=$1 |
| 86 | + _positional_names="_arg_agent _arg_policy _arg_n " |
| 87 | + |
| 88 | + shift "$_shift_for" |
| 89 | + for _positional_name in ${_positional_names} |
| 90 | + do |
| 91 | + test $# -gt 0 || break |
| 92 | + eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1 |
| 93 | + shift |
| 94 | + done |
| 95 | +} |
| 96 | + |
| 97 | +parse_commandline "$@" |
| 98 | +handle_passed_args_count |
| 99 | +assign_positional_args 1 "${_positionals[@]}" |
| 100 | + |
| 101 | +# OTHER STUFF GENERATED BY Argbash |
| 102 | + |
| 103 | +### END OF CODE GENERATED BY Argbash (sortof) ### ]) |
| 104 | +# [ <-- needed because of Argbash |
| 105 | + |
| 106 | +# Collect only real result from `time` command |
| 107 | +TIMEFORMAT=%R |
| 108 | + |
| 109 | +sum=0 |
| 110 | +array=() |
| 111 | +for (( i=0; i<$_arg_n; i++)) |
| 112 | +do |
| 113 | + time=$(time ($_arg_agent -KIf $_arg_policy &>/dev/null) 2>&1) |
| 114 | + sum=$(echo "$sum + $time" | bc -l) |
| 115 | + array+=($time) |
| 116 | + printf "bench %d: %.03f s\n" $i $time |
| 117 | +done |
| 118 | + |
| 119 | +# Sort array of results |
| 120 | +IFS=$'\n' sorted=($(sort <<<"${array[*]}")); unset IFS |
| 121 | + |
| 122 | +echo |
| 123 | +echo "*** RESULTS ***" |
| 124 | + |
| 125 | +# Max time |
| 126 | +max=$(($NUM_RUNS-1)) |
| 127 | +printf "max : %.03f s\n" "${sorted[$max]}" |
| 128 | + |
| 129 | +# Min time |
| 130 | +printf "min : %.03f s\n" "${sorted[0]}" |
| 131 | + |
| 132 | +# Mean time |
| 133 | +mean=$(echo "$sum / $_arg_n" | bc -l) |
| 134 | +printf "mean : %4.03f s\n" $mean |
| 135 | + |
| 136 | +# Median time |
| 137 | +half=$(echo "$_arg_n / 2" | bc -l) |
| 138 | +half=$(printf "%.0f" "$half") |
| 139 | +printf "median : %.03f s\n" "${sorted[$half]}" |
| 140 | + |
| 141 | +# ] <-- needed because of Argbash |
0 commit comments