opexxx
5/26/2015 - 10:15 AM

throughput.sh

#!/bin/sh
# Idea snatched from http://kb.sp.parallels.com/en/115348
# Command for quick installation:
# url='https://gist.githubusercontent.com/andsens/573186933e80a74462bb/raw/throughput.sh'; curl -so throughput.sh $url || wget --quiet -O throughput.sh $url; chmod +x throughput.sh
server() {
	type pv > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		while true; do
			nc -l 1122 | pv --rate > /dev/null
			printf "\n"
		done
	else
		while true; do
			nc -l 1122 > /dev/null
			printf "\n"
		done
	fi
}

client() {
	[ -z $1 ] && usage
	local host=$1
	# In MB
	if [ -z $2 ]; then
		local size=128
	else
		local size=$2
	fi
	# Check if we are dealing with the BSD or Linux version
	# BSD version uses capital letters while Linux uses small ones.
	dd if=/dev/zero of=/dev/null count=0 bs=1M > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		size=${size}M
	else
		size=${size}m
	fi
	dd if=/dev/zero bs=${size} count=1 | nc $host 1122
}

usage() {
	printf "%s" "Usage:
	throughput.sh server
	throughput.sh client <host> [<size>]
"
	exit 64 # E_USAGE
}

[ -z $1 ] && usage
mode=$1
if [ $mode = 'server' ]; then
	server
fi
if [ $mode = 'client' ]; then
	client "$2" "$3"
fi