Skip to content

Commit 34cfbd3

Browse files
committed
De-factor the summarize_pings() function;
This makes both betterspeedtest.sh and netperfrunner.sh a single-file script again.
1 parent eac7700 commit 34cfbd3

File tree

3 files changed

+95
-36
lines changed

3 files changed

+95
-36
lines changed

betterspeedtest.sh

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,52 @@
2121
# Copyright (c) 2014-2022 - Rich Brown [email protected]
2222
# GPLv2
2323

24-
# include the summarize_pings() function from the lib directory
25-
. "./lib/summarize_pings.sh"
24+
# Process the ping times from the passed-in file, and summarize the results
25+
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
26+
# Use awk to build an array of those values, and print first & last (which are min, max)
27+
# and compute average.
28+
# If the number of samples is >= 10, also compute median, and 10th and 90th percentile readings
29+
30+
# Display the values as:
31+
# Latency: (in msec, 11 pings, 8.33% packet loss)
32+
# Min: 16.556
33+
# 10pct: 16.561
34+
# Median: 22.370
35+
# Avg: 21.203
36+
# 90pct: 23.202
37+
# Max: 23.394
38+
39+
summarize_pings() {
40+
41+
grep "time" < "$1" | cat | \
42+
sed 's/^.*time=\([^ ]*\) ms/\1/'| \
43+
# tee >&2 | \
44+
sort -n | \
45+
awk 'BEGIN {numdrops=0; numrows=0} \
46+
{ \
47+
# print ; \
48+
if ( $0 ~ /timeout/ ) { \
49+
numdrops += 1; \
50+
} else { \
51+
numrows += 1; \
52+
arr[numrows]=$1; sum+=$1; \
53+
} \
54+
} \
55+
END { \
56+
pc10="-"; pc90="-"; med="-"; \
57+
if (numrows == 0) {numrows=1} \
58+
if (numrows>=10) \
59+
{ # get the 10th pctile - never the first one
60+
ix=int(numrows/10); if (ix=1) {ix+=1}; pc10=arr[ix]; \
61+
# get the 90th pctile
62+
ix=int(numrows*9/10);pc90=arr[ix]; \
63+
# get the median
64+
if (numrows%2==1) med=arr[(numrows+1)/2]; else med=(arr[numrows/2]); \
65+
}; \
66+
pktloss = numdrops/(numdrops+numrows) * 100; \
67+
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] )\
68+
}'
69+
}
2670

2771
# Print a line of dots as a progress indicator.
2872

lib/summarize_pings.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
summarize_pings() {
2-
31
# Process the ping times from the passed-in file, and summarize the results
42
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
53
# Use awk to build an array of those values, and print first & last (which are min, max)
@@ -15,6 +13,8 @@ summarize_pings() {
1513
# 90pct: 23.202
1614
# Max: 23.394
1715

16+
summarize_pings() {
17+
1818
grep "time" < "$1" | cat | \
1919
sed 's/^.*time=\([^ ]*\) ms/\1/'| \
2020
# tee >&2 | \
@@ -43,4 +43,4 @@ sed 's/^.*time=\([^ ]*\) ms/\1/'| \
4343
pktloss = numdrops/(numdrops+numrows) * 100; \
4444
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] )\
4545
}'
46-
}
46+
}

netperfrunner.sh

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,54 @@
2424
# -p | --ping: Host to ping to measure latency (default - gstatic.com)
2525
# -n | --number: Number of simultaneous sessions (default - 5 sessions)
2626

27-
# Copyright (c) 2014 - Rich Brown [email protected]
27+
# Copyright (c) 2014-2022 - Rich Brown [email protected]
2828
# GPLv2
2929

30-
# Summarize the contents of the ping's output file to show min, avg, median, max, etc.
31-
# input parameter ($1) file contains the output of the ping command
32-
33-
summarize_pings() {
34-
35-
# Process the ping times, and summarize the results
36-
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
37-
# awk builds an array of those values, and prints first & last (which are min, max)
38-
# and computes average.
39-
# If the number of samples is >= 10, also computes median, and 10th and 90th percentile readings
40-
sed 's/^.*time=\([^ ]*\) ms/\1/' < $1 | grep -v "PING" | sort -n | \
41-
awk 'BEGIN {numdrops=0; numrows=0;} \
42-
{ \
43-
if ( $0 ~ /timeout/ ) { \
44-
numdrops += 1; \
45-
} else { \
46-
numrows += 1; \
47-
arr[numrows]=$1; sum+=$1; \
48-
} \
49-
} \
50-
END { \
51-
pc10="-"; pc90="-"; med="-"; \
52-
if (numrows == 0) {numrows=1} \
53-
if (numrows>=10) \
54-
{ ix=int(numrows/10); pc10=arr[ix]; ix=int(numrows*9/10);pc90=arr[ix]; \
55-
if (numrows%2==1) med=arr[(numrows+1)/2]; else med=(arr[numrows/2]); \
56-
}; \
57-
pktloss = numdrops/(numdrops+numrows) * 100; \
58-
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] )\
59-
}'
30+
# Process the ping times from the passed-in file, and summarize the results
31+
# grep to keep lines that have "time=", then sed to isolate the time stamps, and sort them
32+
# Use awk to build an array of those values, and print first & last (which are min, max)
33+
# and compute average.
34+
# If the number of samples is >= 10, also compute median, and 10th and 90th percentile readings
35+
36+
# Display the values as:
37+
# Latency: (in msec, 11 pings, 8.33% packet loss)
38+
# Min: 16.556
39+
# 10pct: 16.561
40+
# Median: 22.370
41+
# Avg: 21.203
42+
# 90pct: 23.202
43+
# Max: 23.394
44+
45+
summarize_pings() {
46+
47+
grep "time" < "$1" | cat | \
48+
sed 's/^.*time=\([^ ]*\) ms/\1/'| \
49+
# tee >&2 | \
50+
sort -n | \
51+
awk 'BEGIN {numdrops=0; numrows=0} \
52+
{ \
53+
# print ; \
54+
if ( $0 ~ /timeout/ ) { \
55+
numdrops += 1; \
56+
} else { \
57+
numrows += 1; \
58+
arr[numrows]=$1; sum+=$1; \
59+
} \
60+
} \
61+
END { \
62+
pc10="-"; pc90="-"; med="-"; \
63+
if (numrows == 0) {numrows=1} \
64+
if (numrows>=10) \
65+
{ # get the 10th pctile - never the first one
66+
ix=int(numrows/10); if (ix=1) {ix+=1}; pc10=arr[ix]; \
67+
# get the 90th pctile
68+
ix=int(numrows*9/10);pc90=arr[ix]; \
69+
# get the median
70+
if (numrows%2==1) med=arr[(numrows+1)/2]; else med=(arr[numrows/2]); \
71+
}; \
72+
pktloss = numdrops/(numdrops+numrows) * 100; \
73+
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] )\
74+
}'
6075
}
6176

6277
# ------- Start of the main routine --------

0 commit comments

Comments
 (0)