#!/bin/bash
# last update: 5-28-2014
# FILENAME: benchmass.sh (I put mine in ~/benchmass/benchmass.sh)
# USAGE: ./benchmass.sh
# PRE-REQ: hardinfo (if you use it), sysbench, bc
# TO INSTALL:
# apt-get update
# apt-get install -y sysbench bc
# apt-get install -y hardinfo
# ====DOWNLOAD AND LINK:====
# URL: http://ram.kossboss.com/scripts/benchmass.sh
# DOWNLOAD: wget http://ram.kossboss.com/scripts/benchmass.sh
# ====EXAMPLE:====
# cd ~ && mkdir benchmass; cd benchmass && wget http://ram.kossboss.com/scripts/benchmass.sh && chmod +x benchmass.sh && DS1=`date +%s` && mkdir bmtest${DS1}; cd bmtest${DS1} && ~/benchmass/benchmass.sh
# ====WHAT THIS SCRIPT DOES:====
# will run series of tests and generate an overall output file - called BENCHMASS-stuff.out
# with small output files on the side (these will be subsets of the big output file - so in the end you can just keep the big output file as it will have everything)
# the small output files are from the test at the end which test file io at different settings - called BENCHMASS-stuff-SBLONG-stuff.txt
# all output files will dump files to current directory
# ====TESTS WHICH ARE RUN:====
# - general system info
# - download speed
# - cache speed
# - bc pi test (to 5000 digits) - cpu test
# - bc 2^2^20 calculation test - cpu test
# - sysbench test of threads cpu mutex memory with 1,4,8,16 threads (thats 16 tests)
# - systench test of fileio with 256M 16G file, with modes seqwr seqrd rndrd rndwr rndrw, with threads 1 4 8 16 (thats 2 * 5 * 4 = 40 tests)
# ====OUTPUT FORMAT:====
# - each output file is seperated with ### for big sections, === medium sections, --- smaller sections, and - - - smallest sections
# - there are no empty newlines (thanks to |& grep .)
# - BENCHMASS-stuff.out: has all of the output that you will see on the screen
# - BENCHMASS-stuff-SBLONG-stuff.txt: is a subset of BENCHMASS-stuff.out (its the fileio sysbench file - there should be 40 of these files - 1 for each test)
#
############################################
############################################
############ SCRIPT BEGINS HERE ############
############################################
############################################

######################
### MAIN VARIABLES ###
######################

MIDDLE=$(echo "`date +%s`s-`hostid`")
FILEOUT0=BENCHMASS-${MIDDLE}.out 

# NOTE ABOUT OUTPUT: Every output needs to go thru FILEOUT0 (via "tee -a" or >>)
# DONT CHANGE SEPERATOR VARIABLES SO THAT EVERYONE HAS SAME LOOKING RESULTS

SEPERATOR1="#########################################################################################"     # TO USE: ${SEPERATOR1}
SEPERATOR2="========================================================================"     # TO USE: ${SEPERATOR2}
SEPERATOR3="++++++++++++++++++++++++++++++++++++++++++++++++++++++"     # TO USE: ${SEPERATOR3}
SEPERATOR4="----------------------------------------"     # TO USE: ${SEPERATOR4}

###################################
### VARIABLE FOR SYSBENCH TESTS ###
###################################

# Leave these as defaults unless you absolutely have to (like your system doesnt have 16 GB of free space, so you can change the test to 256M 4G instead of 256M 16G)
# NOTE: if you add more numbers like 256 1G 4G - you will increase the size of the main output file, the number of tests will rise greatly (multiplicatively), thus the number of outputfiles (small ones) will go up to the same number

THREADSSYS="1 4 8 16"
SIZEFILESSYS="256M 16G"

################
### hardinfo ###
################

# hardinfo -a |& grep .

################
### bench.sh ###
################
# http://www.slashgeek.net/2012/06/02/how-to-do-a-quick-benchmark-for-your-linux-server/

(echo "${SEPERATOR1} - START DATE: [`date +%s` s] - [`date`]"
cname=$(cat /proc/cpuinfo|grep name|head -1|awk '{ $1=$2=$3=""; print }')
cores=$(cat /proc/cpuinfo|grep MHz|wc -l)
freq=$(cat /proc/cpuinfo|grep MHz|head -1|awk '{ print $4 }')
tram=$(free -m | awk 'NR==2'|awk '{ print $2 }')
swap=$(free -m | awk 'NR==4'| awk '{ print $2 }')
up=$(uptime|awk '{ $1=$2=$(NF-6)=$(NF-5)=$(NF-4)=$(NF-3)=$(NF-2)=$(NF-1)=$NF=""; print }')
echo "CPU model : $cname"
echo "Number of cores : $cores"
echo "CPU frequency : $freq MHz"
echo "Total amount of ram : $tram MB"
echo "Total amount of swap : $swap MB"
echo "System uptime : $up")  |& grep . | tee -a ${FILEOUT0}

######################################
### SMALLER bench.sh - 1 download ####
######################################

(echo "${SEPERATOR2} - DATE: [`date +%s` s] - [`date`]" 
echo "DOWNLOADING START" 
cache=$((wget -O /dev/null http://cachefly.cachefly.net/100mb.test) 2>&1 | tail -2 | head -1 | awk '{print $3 $4 }')
echo "${SEPERATOR2} - DATE: [`date +%s` s] - [`date`]" 
echo "DOWNLOADING END - START IO TEST"
io=$((dd if=/dev/zero of=test_$$ bs=64k count=16k conv=fdatasync &&rm -f test_$$) 2>&1 | tail -1| awk '{ print $(NF-1) $NF }')
echo "${SEPERATOR2} - DATE: [`date +%s` s] - [`date`]"
echo "Download speed 100MB : $cache "
echo "I/O speed 100MB: $io")  |& grep . | tee -a ${FILEOUT0}

#########################################
### BIGGEST bench.sh - more downloads ###
#########################################

# TO MAKE IT WORK: remove the ###### from the lines to make it work

# http://www.chrisreeves.co.nz/downloads/bench.sh
###### cachefly=$( wget -O /dev/null http://cachefly.cachefly.net/100mb.test 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from CacheFly: $cachefly "
###### coloatatl=$( wget -O /dev/null http://speed.atl.coloat.com/100mb.test 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Coloat, Atlanta GA: $coloatatl "
###### sldltx=$( wget -O /dev/null http://speedtest.dal05.softlayer.com/downloads/test100.zip 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Softlayer, Dallas, TX: $sldltx "
###### linodejp=$( wget -O /dev/null http://speedtest.tokyo.linode.com/100MB-tokyo.bin 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Linode, Tokyo, JP: $linodejp "
###### i3d=$( wget -O /dev/null http://mirror.i3d.net/100mb.bin 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from i3d.net, Rotterdam, NL: $i3d"
#linodeuk=$( wget -O /dev/null http://speedtest.london.linode.com/100MB-london.bin 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
#echo "Download speed from Linode, London, UK: $linodeuk "
###### leaseweb=$( wget -O /dev/null http://mirror.leaseweb.com/speedtest/100mb.bin 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Leaseweb, Haarlem, NL: $leaseweb "
###### slsg=$( wget -O /dev/null http://speedtest.sng01.softlayer.com/downloads/test100.zip 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Softlayer, Singapore: $slsg "
###### slwa=$( wget -O /dev/null http://speedtest.sea01.softlayer.com/downloads/test100.zip 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Softlayer, Seattle, WA: $slwa "
###### slsjc=$( wget -O /dev/null http://speedtest.sjc01.softlayer.com/downloads/test100.zip 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Softlayer, San Jose, CA: $slsjc "
###### slwdc=$( wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test100.zip 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}' )
###### echo "Download speed from Softlayer, Washington, DC: $slwdc "
###### io=$( ( dd if=/dev/zero of=test_$$ bs=64k count=16k conv=fdatasync && rm -f test_$$ ) 2>&1 | awk -F, '{io=$NF} END { print io}' )
###### echo "I/O speed : $io"

### bc TEST ###
# http://tuxshell.blogspot.com/2009/08/bc-as-cpu-benchmark.html
# http://www.commandlinefu.com/commands/view/229/quick-integer-cpu-benchmark

(echo "${SEPERATOR1} - DATE: [`date +%s` s] - [`date`]"
echo "${SEPERATOR2} - DATE: [`date +%s` s] - [`date`]"
echo "${SEPERATOR3} - DATE: [`date +%s` s] - [`date`]"
echo "BC TEST: PI TO THE 5000 DIGITS - CPU TEST"
time echo "scale=5000; a(1)*4" | bc -l > /dev/null
echo "${SEPERATOR3} - DATE: [`date +%s` s] - [`date`]"
echo "BC TEST: 2^2^20 DIGITS - CPU TEST"
time echo "2^2^20" | bc  > /dev/null) |& grep . | tee -a ${FILEOUT0}

#######################
### SYS BENCH short ###
#######################

# LINK: self

(echo "${SEPERATOR1} - DATE: [`date +%s` s] - [`date`]"
for i in threads cpu mutex memory; do
echo "${SEPERATOR2} - DATE: [`date +%s` s] - [`date`]"
for t in ${THREADSSYS}; do
echo "${SEPERATOR3} - DATE: [`date +%s` s] - [`date`]"
echo "TEST $i - THREADS $t"
sysbench --test=$i --num-threads=$t run
done
done)  |& grep . | tee -a ${FILEOUT0}

######################
### SYS BENCH long ###
######################

# LINK: https://gist.github.com/chetan/712484

# set -u
# set -x
# set -e

(echo "${SEPERATOR1} - DATE: [`date +%s` s] - [`date`]"
for size in ${SIZEFILESSYS}; do
		echo "${SEPERATOR2} - DATE: [`date +%s` s] - [`date`]"
        for mode in seqwr seqrd rndrd rndwr rndrw; do
			 echo "${SEPERATOR3} - DATE: [`date +%s` s] - [`date`]"
             sysbench --test=fileio --file-num=1 --file-total-size=$size prepare
             for threads in ${THREADSSYS}; do
					FILEOUT1="BENCHMASS-${MIDDLE}-SBLONG-sz-$size-md-$mode-t-$threads.txt"
					echo "${SEPERATOR4} - DATE: [`date +%s` s] - [`date`]"
                    echo PARAMS $size $mode $threads |& tee -a ${FILEOUT1}
                    sysbench --test=fileio --file-total-size=$size --file-test-mode=$mode --max-time=60 --max-requests=10000000 --num-threads=$threads --init-rng=on --file-num=1 --file-extra-flags=direct --file-fsync-freq=0 run |& tee -a ${FILEOUT1}
             done
            sysbench --test=fileio --file-total-size=$size cleanup
        done
done)  |& grep . | tee -a ${FILEOUT0}

echo "${SEPERATOR1} - FINISH DATE: [`date +%s` s] - [`date`]" |& grep . | tee -a ${FILEOUT0}

