diff --git a/qa/common/util.sh b/qa/common/util.sh index 43cd757890..736cd9bb19 100755 --- a/qa/common/util.sh +++ b/qa/common/util.sh @@ -473,3 +473,38 @@ function kill_servers () { function collect_artifacts_from_subdir () { cp *.*log* core* ../ || true } + +# Sort an array +# Call with sort_array +# Example: remove_array_outliers array +sort_array() { + local -n arr=$1 + local length=${#arr[@]} + + if [ "$length" -le 1 ]; then + return + fi + + IFS=$'\n' sorted_arr=($(sort -n <<<"${arr[*]}")) + unset IFS + arr=("${sorted_arr[@]}") +} + +# Remove an array's outliers +# Call with remove_array_outliers +# Example: remove_array_outliers array 5 +remove_array_outliers() { + local -n arr=$1 + local percent=$2 + local length=${#arr[@]} + + if [ "$length" -le 1 ]; then + return + fi + + local trim_count=$((length * percent / 100)) + local start_index=$trim_count + local end_index=$((length - (trim_count*2))) + + arr=("${arr[@]:$start_index:$end_index}") +}