Refactor and enhance run_iperf function in runtest.sh for improved error handling and code reuse

This commit is contained in:
Yaro Kasear 2025-04-21 14:31:31 -05:00
parent 664eb96853
commit a97c7eadee

View file

@ -51,6 +51,48 @@ freq_to_channel() {
echo "$channel"
}
run_iperf() {
local target="$1"
local mode="$2"
local direction="$3"
local args=("-c" "$target" "-J" "-t" "10")
if [ "$mode" = "udp" ]; then
args+=("-u")
fi
if [ "$direction" = "down" ]; then
args+=("--reverse")
fi
local tmp_err
tmp_err=$(mktemp)
local result
result=$(iperf3 "${args[@]}" 2>"$tmp_err" | jq -r '
if .error then
"iperf3-error"
elif has("end") | not then
"no-end"
elif .end | has("sum_received") then
.end.sum_received.bits_per_second
elif .end | has("sum") then
.end.sum.bits_per_second
else
"unexpected-format"
end' || echo "execution-failed")
if [[ "$result" == "iperf3-error" || "$result" == "no-end" || "$result" == "unexpected-format" || "$result" == "execution-failed" ]]; then
echo "$(date -Iseconds),iperf $mode $direction to $target failed with '$result'" >> "$FAILURE_LOG"
echo "[stderr] $(cat "$tmp_err")" >> "$FAILURE_LOG"
echo "0"
else
echo "$result"
fi
rm -f "$tmp_err"
}
# Start test email
echo -e "Subject: Test ${BOOT_ID} Started\n\nThis is to inform you that the tests have commenced for test ${BOOT_ID}." | msmtp $RECIPIENT
@ -110,49 +152,6 @@ while [ "$COUNTER" -lt "$NUM_TESTS" ]; do
packet_loss=$(ping -c $PING_COUNT -q $PING_TARGET | grep -oP '\d+(?=% packet loss)')
jitter=$(ping -c $PING_COUNT $PING_TARGET | grep "time=" | awk '{print $(NF-1)}' | sed 's/time=//g' | awk '{sum+=$1; sumsq+=$1*$1} END {if (NR>1) print sqrt(sumsq/NR - (sum/NR)**2); else print 0}')
run_iperf() {
local target="$1"
local mode="$2"
local direction="$3"
local args=("-c" "$target" "-J" "-t" "10")
if [ "$mode" = "udp" ]; then
args+=("-u")
fi
if [ "$direction" = "down" ]; then
args+=("--reverse")
fi
local tmp_err
tmp_err=$(mktemp)
local result
result=$(iperf3 "${args[@]}" 2>"$tmp_err" | jq -r '
if .error then
"iperf3-error"
elif has("end") | not then
"no-end"
elif .end | has("sum_received") then
.end.sum_received.bits_per_second
elif .end | has("sum") then
.end.sum.bits_per_second
else
"unexpected-format"
end' || echo "execution-failed")
if [[ "$result" == "iperf3-error" || "$result" == "no-end" || "$result" == "unexpected-format" || "$result" == "execution-failed" ]]; then
echo "$(date -Iseconds),iperf $mode $direction to $target failed with '$result'" >> "$FAILURE_LOG"
echo "[stderr] $(cat "$tmp_err")" >> "$FAILURE_LOG"
echo "0"
else
echo "$result"
fi
rm -f "$tmp_err"
}
echo " Running iperf3 tests..."
# Run them all. These are in bits per second, convert as needed later.