Skip to content

Commit

Permalink
Add utility functions for array manipulation (#6203)
Browse files Browse the repository at this point in the history
* Add utility functions for outlier removal

* Fix functions

* Add newline to end of file
  • Loading branch information
dyastremsky authored Aug 17, 2023
1 parent 8a823b2 commit b02e223
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions qa/common/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,38 @@ function kill_servers () {
function collect_artifacts_from_subdir () {
cp *.*log* core* ../ || true
}

# Sort an array
# Call with sort_array <array_name>
# 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 <array_name> <percent to trim from both sides>
# 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}")
}

0 comments on commit b02e223

Please sign in to comment.