Skip to content

Instantly share code, notes, and snippets.

@spiderr
Forked from codingtony/speedtest.sh
Last active January 1, 2018 20:47
Show Gist options
  • Save spiderr/404ed58a68d8b5064833968e975320eb to your computer and use it in GitHub Desktop.
Save spiderr/404ed58a68d8b5064833968e975320eb to your computer and use it in GitHub Desktop.
Simple script to test hard disk speed with dd
#!/bin/bash
# by TB : 2012-10-12
if [ "$USER" != "root" ]
then
echo "You must be root to execute this script (in order to instruct the kernel to drop the cache)!"
exit 1
fi
if [ -z "$1" ]
then
TESTFILE=`mktemp -p '.' --suffix '.speedtest'`
else
DIR="$1"
[ ! -d "$DIR" ] && { echo "$DIR must be a directory"; exit 1; }
echo "Are you sure you want to use $DIR as a test file directory. CTRL-C to abort" && read
TESTFILE=$(mktemp --tmpdir=$DIR)
fi
function do_dd_test {
echo -n "$1 ==>"
shift
#dd $@ 2>&1 | tail -1 | cut -d, -f3
DD_OUTPUT=`dd $@ 2>&1`
BITS=1000;
DD_HUMANIZED=$(echo "$DD_OUTPUT" | sed -r '/.*records /d' | tr -d '()' | awk '{sub(/.*bytes /, $1/'$BITS'/'$BITS'" MB "); sub(/in .* secs/, "in "$5/60" mins "); sub(/mins .*/, "mins (" $7/'$BITS'/'$BITS'" MB/sec)"); printf "%s Gb/s %s MB/s", $1/$6*.008, $1/$6}');
echo $DD_HUMANIZED
}
trap "{ cleanup; exit 1; }" SIGKILL SIGTERM SIGINT
echo "$d Test `date`"
echo "===================="
for DD_SIZE in "bs=64k count=16k" "bs=1G count=1"; do
#Drop cache
echo 3 > /proc/sys/vm/drop_caches
do_dd_test "WRITE TEST $DD_SIZE" if=/dev/zero of=${TESTFILE} ${DD_SIZE} conv=fdatasync
#Drop cache
echo 3 > /proc/sys/vm/drop_caches
do_dd_test "READ TEST $DD_SIZE (W/O CACHE)" if=${TESTFILE} of=/dev/null ${DD_SIZE}
do_dd_test "READ TEST $DD_SIZE (WITH CACHE)" if=${TESTFILE} of=/dev/null ${DD_SIZE}
rm ${TESTFILE};
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment