#!/bin/bash
# ddBench v2.0 - A simple SSD/HDD sequential read/write Benchmark
# 'sync' and 'drop_caches' will be performed before each cycle.
# 'ddbench.x*' will be erased after each cycle.
#
# sed -i "s/$(echo -ne "\x0d")$//" ddbench.sh
#
echo
echo "ddBench v2.0 - A simple SSD/HDD sequential read/write Benchmark"
echo "Starting now, paste your results starting below this line:"
echo
cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo )
cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
freq=$( awk -F: ' /cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo )
tram=$( free -m | awk 'NR==2 {print $2}' )
swap=$( free -m | awk 'NR==4 {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" 
echo

function clean()
{
	rm ddbench.x* 2>/dev/null
	n1=$(date +%s.%N | sed -r 's/\.(...).*/\1/')
	sync && echo 3 > /proc/sys/vm/drop_caches
	n2=$(date +%s.%N | sed -r 's/\.(...).*/\1/')
	echo "SYNC: $(( n2 - n1 )) msec"
	echo
}
function bench()
{
	clean
	echo "$1, $2 times"
	dd if=/dev/zero of=ddbench.x conv=fdatasync bs=$1 count=$2
}
function mbench()
{
	clean
	n=$3
	echo "$1, $2 times, $n parallel"
	while [ $n -gt 0 ]
	do
		dd if=/dev/zero of=ddbench.x$n conv=fdatasync bs=$1 count=$2 2> ddbench.x${n}o &
		n=$(( n - 1 ))
	done
	wait
	cat  ddbench.*o
}

bench 4k 262144
bench 8k 131072
bench 16k 65536
bench 32k 32768
bench 64k 16384
bench 128k 8192
bench 256k 4096
mbench 16k 65536 4
mbench  8k 65536 8
clean

echo
echo "Done. Paste your results above this line."
echo
