Skip to content

Commit b02e223

Browse files
authored
Add utility functions for array manipulation (#6203)
* Add utility functions for outlier removal * Fix functions * Add newline to end of file
1 parent 8a823b2 commit b02e223

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

qa/common/util.sh

+35
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,38 @@ function kill_servers () {
473473
function collect_artifacts_from_subdir () {
474474
cp *.*log* core* ../ || true
475475
}
476+
477+
# Sort an array
478+
# Call with sort_array <array_name>
479+
# Example: remove_array_outliers array
480+
sort_array() {
481+
local -n arr=$1
482+
local length=${#arr[@]}
483+
484+
if [ "$length" -le 1 ]; then
485+
return
486+
fi
487+
488+
IFS=$'\n' sorted_arr=($(sort -n <<<"${arr[*]}"))
489+
unset IFS
490+
arr=("${sorted_arr[@]}")
491+
}
492+
493+
# Remove an array's outliers
494+
# Call with remove_array_outliers <array_name> <percent to trim from both sides>
495+
# Example: remove_array_outliers array 5
496+
remove_array_outliers() {
497+
local -n arr=$1
498+
local percent=$2
499+
local length=${#arr[@]}
500+
501+
if [ "$length" -le 1 ]; then
502+
return
503+
fi
504+
505+
local trim_count=$((length * percent / 100))
506+
local start_index=$trim_count
507+
local end_index=$((length - (trim_count*2)))
508+
509+
arr=("${arr[@]:$start_index:$end_index}")
510+
}

0 commit comments

Comments
 (0)