Advertisement
peetaur

dddiskspeedtest.bash

Aug 8th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.22 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Author: Peter Maloney
  4. # License: GPLv2
  5. #
  6. # Tests the read speed at random spots on the disk (does not use the entire surface, just many points) to find if there are some slow spots.
  7. # To make it scan the whole disk, lower the "increment" variable to a number greater than count.
  8. #
  9. # Usage:
  10. #   $0 sdb,sdc
  11. # or
  12. #   $0 sdb,sdc mylog
  13.  
  14. # ========================
  15. # Command Line Input
  16. # ========================
  17.  
  18. # Comma separated device names without /dev/
  19. # eg. sdb,sdc
  20. disks="$1"
  21.  
  22. # Optional. The prefix for the log file. Default: "dddst"
  23. # eg. A value of "mylog" would mean disk sda gets stored in mylog-sda.csv
  24. logFilePrefix="$2"
  25.  
  26.  
  27. # ========================
  28. # Constants
  29. # ========================
  30.  
  31. defaultLogFilePrefix=dddst
  32. # the number of blocks to seek (including the $count read previously) for the next read
  33. increment=1000
  34. # the count of blocks to read
  35. count=20
  36. # NOTE: changing this means the totalMiB won't work... unsupported
  37. bs=1M
  38. # how many times to read the same spot for an average
  39. avg=1
  40.  
  41. # ========================
  42. # Command line handling
  43. # ========================
  44.  
  45. if [ -z "$logFilePrefix" ]; then
  46.     logFilePrefix=dddst
  47. fi
  48.  
  49. # ========================
  50. # Functions
  51. # ========================
  52.  
  53. finddisksize() {
  54.     disk="$1"
  55.     parted /dev/${disk} unit B print | grep Disk | egrep -o "[0-9]+"
  56. }
  57.  
  58. testdisk() {
  59.     IFS=','
  60.     for disk in ${disks}; do
  61.         echo "${disk}"
  62.         logFile="${logFilePrefix}-${disk}.csv"
  63.         logFilePercent="${logFilePrefix}-${disk}-percent.csv"
  64.         rm ${logFile} 2>&1
  65.  
  66.         totalBytes=$(finddisksize $disk)
  67.         totalMiB=$(($totalBytes/1024/1024))
  68.         echo "Total size: $totalMiB B"
  69.        
  70.         skip=0
  71.         while [ "${skip}" -le "$totalMiB" ]; do
  72.             #NOTE: relies on result being in MB/s
  73.             if [ "$avg" -ge 1 ]; then
  74.                 runCount=0
  75.                 speedSum=0
  76.                
  77.                 while [ "$runCount" -lt "$avg" ]; do
  78.                     speed1=$(dd if=/dev/${disk} of=/dev/null bs=${bs} count=${count} skip=${skip} iflag=direct 2>&1 \
  79.                         | egrep -o "[0-9\.]+ MB/s" | egrep -o "[0-9\.]+")
  80.                     if [ "$?" != 0 -o -z "$speed1" ]; then
  81.                         break
  82.                     fi
  83.                     speedSum=$(echo "${speed1}+${speedSum}" | bc)
  84.                     let runCount++
  85.                 done
  86.                 speed=$(echo "scale=1; ${speedSum}/$runCount" | bc)
  87.             else
  88.                 speed=$(dd if=/dev/${disk} of=/dev/null bs=${bs} count=${count} skip=${skip} iflag=direct 2>&1 \
  89.                     | egrep -o "[0-9\.]+ MB/s" | egrep -o "[0-9\.]+")
  90.                 if [ "$?" != 0 -o -z "$speed" ]; then
  91.                     break
  92.                 fi
  93.             fi
  94.            
  95.             progress=$(echo "scale=2; ${skip}/(${totalMiB}/100)" | bc)
  96.            
  97.             printf "%6.2f%% : skip = %7d, speed = %.1f MB/s\n" $progress $skip $speed
  98.             echo "${skip},${speed}" >> ${logFile}
  99.             echo "${progress},${speed}" >> ${logFilePercent}
  100.             let skip+=${increment}
  101.         done
  102.     done
  103. }
  104.  
  105. # ========================
  106. # Main
  107. # ========================
  108.  
  109. testdisk "$disks"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement