[CentOS] Benchmark Disk IO

Thu May 6 14:15:25 UTC 2010
Alan McKay <alan.mckay at gmail.com>

There is a dd test that floats around the PostgreSQL lists, so I wrote
this simple script to automate it - use at your own risk!

#!/bin/bash

# do something which parses command line parameters

DEFAULT_BLOCK=8
DEFAULT_PATH=/data/tmp
DEFAULT_FILE=ddfile

helpme()
{
        echo "Usage: $0 [RAM=x] [PATH=/foo/bar] [FILE=foo] [BLOCKSIZE=X]"
        echo "Runs a dd test on disk subsystem to benchmark I/O speed"
        echo "  BLOCKSIZE - given in K.  Should be no need to change"
        echo "    default is 8 which is what PostgreSQL uses"
        echo "  RAM - must be specified in Gigs"
        echo "    default is to use output of 'free' command"
        echo "  PATH - where do you want the files created?  i.e.
which filesystem do you want tested?"
        echo "    default is $DEFAULT_PATH"
        echo "  FILE - file name to create in test.  Really makes no
difference."
        echo "    default is $DEFAULT_FILE"
        exit 1
}

default_ram()
{
        MYB=`free | grep Mem: | awk '{print $2}'`
        ((MYG=MYB/1000/1000))
        export MYGIGS=$MYG
        echo "RAM not specified - assuming ${MYB}/${MYGIGS}G"
}

default_path()
{
        export  MYPATH=$DEFAULT_PATH
        echo "PATH not specified - using default $MYPATH"
}

default_file()
{
        export  MYFILE=$DEFAULT_FILE
        echo "FILE not specified - using default $MYFILE"
}

default_block()
{
        export  MYBLOCK=$DEFAULT_BLOCK
        echo "BLOCKSIZE not specified - using default $MYBLOCK"
}

RAMGIVEN=0
PATHGIVEN=0
FILEGIVEN=0
BLOCKGIVEN=0

while [ $# -gt 0 ]
do
        MYVAR=`echo $1 | awk -F= '{print $1}'`
        MYVAL=`echo $1 | awk -F= '{print $2}'`

        case $MYVAR in
                BLOCKSIZE)
                        MYBLOCK=$MYVAL
                        BLOCKGIVEN=1
                        ;;

                RAM)
                        MYGIGS=$MYVAL
                        RAMGIVEN=1
                        ;;

                PATH)
                        MYPATH=$MYVAL
                        PATHGIVEN=1
                        ;;

                FILE)
                        MYFILE=$MYVAL
                        FILEGIVEN=1
                        ;;

                -h|-help|--h|--help)
                        helpme
                        ;;

                *)
                        echo "ERROR : unknown parameter [${MYVAR}]"
                        ;;
        esac

        shift
done

[[ $RAMGIVEN -eq 0 ]]   && default_ram
[[ $PATHGIVEN -eq 0 ]]  && default_path
[[ $FILEGIVEN -eq 0 ]]  && default_file
[[ $BLOCKGIVEN -eq 0 ]]  && default_block

PATH_EXISTS=0
[[ -d $MYPATH ]]        && PATH_EXISTS=1

echo "[$MYGIGS][$MYPATH][$MYFILE][$PATH_EXISTS]"


mkdir -p $MYPATH
pushd $MYPATH

df .

FREESPACE=`df . | tail -1 | awk '{print $4}'`

BLOCKSPERGIG=125000

((ONCERAM=BLOCKSPERGIG*MYGIGS))
((TWICERAM=2*BLOCKSPERGIG*MYGIGS))
((THRICERAM=3*BLOCKSPERGIG*MYGIGS*MYBLOCK))

        echo "Have:$FREESPACE, Need:$THRICERAM (3x RAM)"

if [ $THRICERAM -ge $FREESPACE ]
then
        echo "ERROR: not enough room on disk"
        echo "Have:$FREESPACE, Need:$THRICERAM (3x RAM)"
        exit 2
fi


DDCOM="dd if=/dev/zero of=${MYFILE} bs=8k count=${TWICERAM} && sync"
echo $DDCOM

time sh -c "$DDCOM"
DDCOM="dd if=/dev/zero of=${MYFILE}2 bs=8K count=${ONCERAM}"
echo $DDCOM
$DDCOM

DDCOM="dd if=${MYFILE} of=/dev/null bs=8k"
echo $DDCOM
time $DDCOM

rm -f ${MYFILE}
rm -f ${MYFILE}2
[[ $PATH_EXISTS -eq 0 ]]        && rmdir $MYPATH

popd




-- 
“Don't eat anything you've ever seen advertised on TV”
         - Michael Pollan, author of "In Defense of Food"