Refactor run_iperf function to improve logging and streamline output handling

This commit is contained in:
Yaro Kasear 2025-04-23 12:56:22 -05:00
parent 0818da0351
commit e75b3807f9

View file

@ -76,15 +76,13 @@ run_iperf() {
local tmp_json local tmp_json
tmp_json=$(mktemp) tmp_json=$(mktemp)
local result
sleep 1s sleep 1s
SECONDS=0 SECONDS=0
echo "Running iperf3 $mode $direction to $target..." echo "Running iperf3 $mode $direction to $target..." >&2
result=$(iperf3 "${args[@]}" -J >"$tmp_json" 2>"$tmp_err") iperf3 "${args[@]}" -J >"$tmp_json" 2>"$tmp_err"
echo "This took $SECONDS seconds." echo "This took $SECONDS seconds." >&2
echo "Parsing iperf3 output..." echo "Parsing iperf3 output..." >&2
SECONDS=0 SECONDS=0
parsed=$(jq -r ' parsed=$(jq -r '
if .error then if .error then
@ -98,7 +96,7 @@ run_iperf() {
else else
"unexpected-format" "unexpected-format"
end' "$tmp_json" || echo "execution-failed") end' "$tmp_json" || echo "execution-failed")
echo "This took $SECONDS seconds." echo "This took $SECONDS seconds." >&2
if [[ "$parsed" == "iperf3-error" || "$parsed" == "no-end" || "$parsed" == "unexpected-format" || "$parsed" == "execution-failed" ]]; then if [[ "$parsed" == "iperf3-error" || "$parsed" == "no-end" || "$parsed" == "unexpected-format" || "$parsed" == "execution-failed" ]]; then
timestamp=$(date -Iseconds) timestamp=$(date -Iseconds)
@ -173,7 +171,7 @@ for ((COUNTER = 1; COUNTER <= NUM_TESTS; COUNTER++)); do
echo " Running ping test..." echo " Running ping test..."
packet_loss=$(ping -c "$PING_COUNT" -q "$PING_TARGET" | grep -oP '\d+(?=% packet loss)') 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}') 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}')
echo "This took $SECONDS seconds." echo " This took $SECONDS seconds."
echo " Running iperf3 tests..." echo " Running iperf3 tests..."
LocalTCPUp=$(run_iperf "$IPERF_LOCAL_TARGET" tcp up) LocalTCPUp=$(run_iperf "$IPERF_LOCAL_TARGET" tcp up)