Skip to content

Instantly share code, notes, and snippets.

@tompurl
Created July 11, 2017 22:38
Show Gist options
  • Save tompurl/dc8ca0821ec52cf1ca65d2509a8e1234 to your computer and use it in GitHub Desktop.
Save tompurl/dc8ca0821ec52cf1ca65d2509a8e1234 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
USAGE="$0 [-n iteration_number] [-v] [-h] testfile destination_server"
die() {
echo "$@" 1>&2
echo $USAGE 1>&2
exit 1;
}
### Process options and arguments
number_of_iterations=5
verbose=0
while getopts ":n:vh" opt; do
case $opt in
h)
echo $USAGE
exit 0
;;
n)
number_of_iterations=$OPTARG
;;
v)
verbose=1
# Currently this does not do anything :-)
;;
\?)
die "Invalid option: -$OPTARG" >&2
;;
:)
die "Option -$OPTARG requires an argument." >&2
;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" ]; then
die "No testfile argument supplied"
fi
if [ ! -e "$1" ]; then
die "The testfile file doesn't seem to exist"
fi
if [ -z "$2" ]; then
die "No destination_server argument supplied"
fi
# Calculate net speed stats based on a file name
function calculate_statistics {
awk \
'
# First find every line with a KB value and convert it to MB
/.*KB\/s/ { throughput_denormalized = gensub(/(.*)KB\/s/, "\\1", "g", $4)
throughput_normalized = throughput_denormalized / 1024 }
# Next, if its a MB line just pull out the throughput
/.*MB\/s/ { throughput_normalized = gensub(/(.*)MB\/s/, "\\1", "g", $4) }
# Add it to the sum with each line read
{ sum += throughput_normalized }
# After evaluating each line, print your average
END { print "Average throughput in MB/s: " sum / NR }' $1
}
# Run a throughput test using scp N amount of times and then
# calculate stats.
#
# $1 => Test name. ie "download" or "upload"
# $2 => "From" path
# $3 => "To" path
# $4 => Temp dir path
# $5 => Iterations
function run_test {
echo
echo "Starting $1 test"
echo "============================="
echo
data_file="${4}/${1}.dat"
rm -f "$data_file" # Delete if it already exists
for i in $(seq 1 "$5"); do
echo "Starting run $i"
script -q -c "scp $2 $3" | strings| tail -1 | tee -a $data_file
done
# Noamalize data and calculate stats
echo
calculate_statistics $data_file
echo ---
echo
}
my_temp_dir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX")
run_test upload "$1" "$2":/tmp $my_temp_dir $number_of_iterations
run_test download "$2":/tmp/$(basename $1) . $my_temp_dir $number_of_iterations
rm -rf $my_temp_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment