Skip to content

Commit

Permalink
De-factor the summarize_pings() function;
Browse files Browse the repository at this point in the history
This makes both betterspeedtest.sh and netperfrunner.sh a single-file script again.
  • Loading branch information
richb-hanover committed Jan 1, 2022
1 parent eac7700 commit 34cfbd3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 36 deletions.
48 changes: 46 additions & 2 deletions betterspeedtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,52 @@
# Copyright (c) 2014-2022 - Rich Brown [email protected]
# GPLv2

# include the summarize_pings() function from the lib directory
. "./lib/summarize_pings.sh"
# Process the ping times from the passed-in file, and summarize the results
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
# Use awk to build an array of those values, and print first & last (which are min, max)
# and compute average.
# If the number of samples is >= 10, also compute median, and 10th and 90th percentile readings

# Display the values as:
# Latency: (in msec, 11 pings, 8.33% packet loss)
# Min: 16.556
# 10pct: 16.561
# Median: 22.370
# Avg: 21.203
# 90pct: 23.202
# Max: 23.394

summarize_pings() {

grep "time" < "$1" | cat | \
sed 's/^.*time=\([^ ]*\) ms/\1/'| \
# tee >&2 | \
sort -n | \
awk 'BEGIN {numdrops=0; numrows=0} \
{ \
# print ; \
if ( $0 ~ /timeout/ ) { \
numdrops += 1; \
} else { \
numrows += 1; \
arr[numrows]=$1; sum+=$1; \
} \
} \
END { \
pc10="-"; pc90="-"; med="-"; \
if (numrows == 0) {numrows=1} \
if (numrows>=10) \
{ # get the 10th pctile - never the first one
ix=int(numrows/10); if (ix=1) {ix+=1}; pc10=arr[ix]; \
# get the 90th pctile
ix=int(numrows*9/10);pc90=arr[ix]; \
# get the median
if (numrows%2==1) med=arr[(numrows+1)/2]; else med=(arr[numrows/2]); \
}; \
pktloss = numdrops/(numdrops+numrows) * 100; \
printf("\n Latency: (in msec, %d pings, %4.2f%% packet loss)\n Min: %4.3f \n 10pct: %4.3f \n Median: %4.3f \n Avg: %4.3f \n 90pct: %4.3f \n Max: %4.3f\n", numrows, pktloss, arr[1], pc10, med, sum/numrows, pc90, arr[numrows] )\
}'
}

# Print a line of dots as a progress indicator.

Expand Down
6 changes: 3 additions & 3 deletions lib/summarize_pings.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
summarize_pings() {

# Process the ping times from the passed-in file, and summarize the results
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
# Use awk to build an array of those values, and print first & last (which are min, max)
Expand All @@ -15,6 +13,8 @@ summarize_pings() {
# 90pct: 23.202
# Max: 23.394

summarize_pings() {

grep "time" < "$1" | cat | \
sed 's/^.*time=\([^ ]*\) ms/\1/'| \
# tee >&2 | \
Expand Down Expand Up @@ -43,4 +43,4 @@ sed 's/^.*time=\([^ ]*\) ms/\1/'| \
pktloss = numdrops/(numdrops+numrows) * 100; \
printf("\n Latency: (in msec, %d pings, %4.2f%% packet loss)\n Min: %4.3f \n 10pct: %4.3f \n Median: %4.3f \n Avg: %4.3f \n 90pct: %4.3f \n Max: %4.3f\n", numrows, pktloss, arr[1], pc10, med, sum/numrows, pc90, arr[numrows] )\
}'
}
}
77 changes: 46 additions & 31 deletions netperfrunner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,54 @@
# -p | --ping: Host to ping to measure latency (default - gstatic.com)
# -n | --number: Number of simultaneous sessions (default - 5 sessions)

# Copyright (c) 2014 - Rich Brown [email protected]
# Copyright (c) 2014-2022 - Rich Brown [email protected]
# GPLv2

# Summarize the contents of the ping's output file to show min, avg, median, max, etc.
# input parameter ($1) file contains the output of the ping command

summarize_pings() {

# Process the ping times, and summarize the results
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
# awk builds an array of those values, and prints first & last (which are min, max)
# and computes average.
# If the number of samples is >= 10, also computes median, and 10th and 90th percentile readings
sed 's/^.*time=\([^ ]*\) ms/\1/' < $1 | grep -v "PING" | sort -n | \
awk 'BEGIN {numdrops=0; numrows=0;} \
{ \
if ( $0 ~ /timeout/ ) { \
numdrops += 1; \
} else { \
numrows += 1; \
arr[numrows]=$1; sum+=$1; \
} \
} \
END { \
pc10="-"; pc90="-"; med="-"; \
if (numrows == 0) {numrows=1} \
if (numrows>=10) \
{ ix=int(numrows/10); pc10=arr[ix]; ix=int(numrows*9/10);pc90=arr[ix]; \
if (numrows%2==1) med=arr[(numrows+1)/2]; else med=(arr[numrows/2]); \
}; \
pktloss = numdrops/(numdrops+numrows) * 100; \
printf(" Latency: (in msec, %d pings, %4.2f%% packet loss)\n Min: %4.3f \n 10pct: %4.3f \n Median: %4.3f \n Avg: %4.3f \n 90pct: %4.3f \n Max: %4.3f\n", numrows, pktloss, arr[1], pc10, med, sum/numrows, pc90, arr[numrows] )\
}'
# Process the ping times from the passed-in file, and summarize the results
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
# Use awk to build an array of those values, and print first & last (which are min, max)
# and compute average.
# If the number of samples is >= 10, also compute median, and 10th and 90th percentile readings

# Display the values as:
# Latency: (in msec, 11 pings, 8.33% packet loss)
# Min: 16.556
# 10pct: 16.561
# Median: 22.370
# Avg: 21.203
# 90pct: 23.202
# Max: 23.394

summarize_pings() {

grep "time" < "$1" | cat | \
sed 's/^.*time=\([^ ]*\) ms/\1/'| \
# tee >&2 | \
sort -n | \
awk 'BEGIN {numdrops=0; numrows=0} \
{ \
# print ; \
if ( $0 ~ /timeout/ ) { \
numdrops += 1; \
} else { \
numrows += 1; \
arr[numrows]=$1; sum+=$1; \
} \
} \
END { \
pc10="-"; pc90="-"; med="-"; \
if (numrows == 0) {numrows=1} \
if (numrows>=10) \
{ # get the 10th pctile - never the first one
ix=int(numrows/10); if (ix=1) {ix+=1}; pc10=arr[ix]; \
# get the 90th pctile
ix=int(numrows*9/10);pc90=arr[ix]; \
# get the median
if (numrows%2==1) med=arr[(numrows+1)/2]; else med=(arr[numrows/2]); \
}; \
pktloss = numdrops/(numdrops+numrows) * 100; \
printf("\n Latency: (in msec, %d pings, %4.2f%% packet loss)\n Min: %4.3f \n 10pct: %4.3f \n Median: %4.3f \n Avg: %4.3f \n 90pct: %4.3f \n Max: %4.3f\n", numrows, pktloss, arr[1], pc10, med, sum/numrows, pc90, arr[numrows] )\
}'
}

# ------- Start of the main routine --------
Expand Down

0 comments on commit 34cfbd3

Please sign in to comment.