Skip to content

Instantly share code, notes, and snippets.

@e7d
Last active June 21, 2021 12:01
Show Gist options
  • Save e7d/3b7ae1cad7cc13f41fb86b1fc7c10cc8 to your computer and use it in GitHub Desktop.
Save e7d/3b7ae1cad7cc13f41fb86b1fc7c10cc8 to your computer and use it in GitHub Desktop.
#!/bin/sh
maxBlockSize=1073741824 # 1 GB
maxBlockCount=1024
testSize=1073741824 # 1 GB
testDir=$1
if [ -z "$1" ]; then
testDir="/tmp"
fi
testFile="${testDir}/dd.dat"
testResultFile="${testDir}/ddr.txt"
writeFlag="oflag=direct"
readFlag="iflag=direct"
echo "Write speed benchmark..."
printf "%8s | %8s | %12s | %15s | %15s \n" "Size" "Blocks" "Duration" "Bandwidth" "Throughput"
printf "%s\n" "---------|----------|--------------|-----------------|-----------------"
blockSize=512 # 512 B
while [ $blockSize -le $maxBlockSize ]
do
byteSize=$(numfmt --to=iec --suffix=B $blockSize)
blockCount=$(($testSize / $blockSize))
blockCount=$(($blockCount < $maxBlockCount ? $blockCount : $maxBlockCount))
printf "%8s | %8s | " "$byteSize" "$blockCount"
sync; dd if=/dev/zero of=$testFile bs=$blockSize count=$blockCount $writeFlag >$testResultFile 2>&1; sync
result=$(cat $testResultFile)
duration=$(echo $result | grep -P '([\d,\.])+(?= s)' -o | tr ',' '.')
bandwidth=$(echo $result | grep -P '([\d,\.])+ [\w]?B/s' -o)
throughput=$(echo "scale=2; $blockCount / $duration" | bc)
#echo "$byteSize = ${duration} s, $bandwidth, $throughput IO/s"
printf "%12s | %15s | %15s \n" "$duration s" "$bandwidth" "$throughput IO/s"
blockSize=$(($blockSize * 2))
done
echo "Read speed benchmark..."
printf "%8s | %8s | %12s | %15s | %15s \n" "Size" "Blocks" "Duration" "Bandwidth" "Throughput"
printf "%s\n" "---------|----------|--------------|-----------------|-----------------"
blockSize=512 # 512 B
while [ $blockSize -le $maxBlockSize ]
do
byteSize=$(numfmt --to=iec --suffix=B $blockSize)
blockCount=$(($testSize / $blockSize))
blockCount=$(($blockCount < $maxBlockCount ? $blockCount : $maxBlockCount))
printf "%8s | %8s | " "$byteSize" "$blockCount"
dd if=$testFile of=/dev/null bs=$blockSize count=$blockCount $readFlag >$testResultFile 2>&1
result=$(cat $testResultFile)
duration=$(echo $result | grep -P '([\d,\.])+(?= s)' -o | tr ',' '.')
bandwidth=$(echo $result | grep -P '([\d,\.])+ [\w]?B/s' -o)
throughput=$(echo "scale=2; $blockCount / $duration" | bc)
#echo "$byteSize = ${duration} s, $bandwidth, $throughput IO/s"
printf "%12s | %15s | %15s \n" "$duration s" "$bandwidth" "$throughput IO/s"
blockSize=$(($blockSize * 2))
done
rm $testFile $testResultFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment