dd-test (2600B)
1 #!/bin/sh 2 # IBS 3 TEST_FILE=${1:-dd_ibs_testfile} 4 [ -e "$TEST_FILE" ]; TEST_FILE_EXISTS=$? 5 TEST_FILE_SIZE=134217728 6 7 # Exit if file exists 8 if [ -e "$TEST_FILE" ]; then 9 echo "Test file $TEST_FILE exists, aborting." 10 exit 1 11 fi 12 13 # Create test file 14 echo 'Generating test file...' 15 BLOCK_SIZE=65536 16 COUNT=$((TEST_FILE_SIZE / BLOCK_SIZE)) 17 dd if=/dev/urandom of="$TEST_FILE" bs=$BLOCK_SIZE count=$COUNT > /dev/null 2>&1 18 19 stats="" 20 21 # Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M 22 for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 23 do 24 # Read test file out to /dev/null with specified block size 25 DD_RESULT=$(dd if="$TEST_FILE" of=/dev/null bs=$BLOCK_SIZE 2>&1 1>/dev/null) 26 27 # Extract transfer rate 28 TRANSFER_RATE=$(echo "$DD_RESULT" | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?') 29 stats="$stats 30 $BLOCK_SIZE:$TRANSFER_RATE" 31 done 32 33 # Clean up the test file if we created one 34 [ $TEST_FILE_EXISTS -ne 0 ] && rm "$TEST_FILE" 35 36 printf "Optimal ibs: " 37 printf "%s" "$stats" | awk -F ':' 'NF > 1 { sub("B/s", "", $2); gsub(" ", "", $2); gsub(" ", "", $1); print $1 ":" $2 }' | numfmt -d ':' --field 2 --from auto | sort -t ':' -k 2rn | head -n 1 | numfmt -d ':' --field 2 --to iec 38 39 # OBS 40 TEST_FILE=${1:-dd_obs_testfile} 41 [ -e "$TEST_FILE" ]; TEST_FILE_EXISTS=$? 42 TEST_FILE_SIZE=134217728 43 44 stats="" 45 46 # Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M 47 for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 48 do 49 # Calculate number of segments required to copy 50 COUNT=$((TEST_FILE_SIZE / BLOCK_SIZE)) 51 52 if [ $COUNT -le 0 ]; then 53 echo "Block size of $BLOCK_SIZE estimated to require $COUNT blocks, aborting further tests." 54 break 55 fi 56 57 # Create a test file with the specified block size 58 DD_RESULT=$(dd if=/dev/zero of="$TEST_FILE" bs=$BLOCK_SIZE count=$COUNT 2>&1 1>/dev/null) 59 60 # Extract the transfer rate from dd's STDERR output 61 TRANSFER_RATE=$(echo "$DD_RESULT" | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?') 62 63 # Clean up the test file if we created one 64 [ $TEST_FILE_EXISTS -ne 0 ] && rm "$TEST_FILE" 65 66 # Output the result 67 stats="$stats 68 $BLOCK_SIZE:$TRANSFER_RATE" 69 done 70 71 printf "Optimal obs: " 72 printf "%s" "$stats" | awk -F ':' 'NF > 1 { sub("B/s", "", $2); gsub(" ", "", $2); gsub(" ", "", $1); print $1 ":" $2 }' | numfmt -d ':' --field 2 --from auto | sort -t ':' -k 2rn | head -n 1 | numfmt -d ':' --field 2 --to iec