Skip to content

Instantly share code, notes, and snippets.

@xenithorb
Created October 11, 2017 18:11
Show Gist options
  • Save xenithorb/b4b922b9f7dc98ca0f093bba33c62d80 to your computer and use it in GitHub Desktop.
Save xenithorb/b4b922b9f7dc98ca0f093bba33c62d80 to your computer and use it in GitHub Desktop.

Revisions

  1. xenithorb created this gist Oct 11, 2017.
    76 changes: 76 additions & 0 deletions paradd.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/bin/bash
    declare MBYTES=2048
    declare -a BLK_DEVS FILES
    #set -x

    _usage() {
    cat <<EOF
    Usage:
    $0 <-r|-w> [options ...]
    Wrapper for standard read/write block device tests using dd in parallel
    Options:
    -d Read device name (i.e. /dev/sda) : Can be used more than once
    -f Write file name and path (i.e. /tmp/testfile) : Can be used more than once
    -b Size in MiB, Default: 2048
    -w Write operation test
    -r Read operation test
    Examples:
    Test reading 1GiB of data from devices /dev/sda and /dev/sdb:
    $0 -r -d /dev/sda -d /dev/sdb -b 1024
    $0 -r -d sda -d sdb
    Test writing 2GiB of data to files ~/testfile and /tmp/testfile:
    $0 -w -f ~/testfile -f /tmp/testfile
    EOF
    }

    _drop_caches() {
    echo 3 > /proc/sys/vm/drop_caches
    }

    _dd_test_read() {
    _drop_caches
    local device="${1:?ERROR missing device name(s)}"
    local mbytes="${2:?ERROR Missing output filesize}"
    dd if="/dev/$1" of=/dev/null bs=1M count="$2" 2>&1 | grep copied
    }

    _dd_test_write() {
    _drop_caches
    local file="${1:?ERROR Missing filename(s)}"
    local mbytes="${2:?ERROR Missing output filesize}"
    dd if="/dev/zero" of="${file}" bs=1M count="${mbytes}" conv=fdatasync 2>&1 | grep copied
    rm -v "${file}"
    }

    while getopts ":d:b:rwhHf:" opt; do
    case $opt in
    d) BLK_DEVS+=( "${OPTARG//\/dev\//}" ) ;;
    f) FILES+=( "${OPTARG##\/dev*}" ) ;;
    b) MBYTES="$OPTARG" ;;
    w) OPERATION="write" ;;
    r) OPERATION="read" ;;
    [Hh]|*) _usage; exit 1 ;;
    esac
    done
    typeset -p FILES BLK_DEVS


    if [[ "$OPERATION" == "read" ]]; then
    for (( i=0; i<${#BLK_DEVS[@]}; i++ )); do
    ( _dd_test_read "${BLK_DEVS[i]}" "${MBYTES}" )&
    done
    elif [[ "$OPERATION" == "write" ]]; then
    for (( i=0; i<${#FILES[@]}; i++ )); do
    ( _dd_test_write "${FILES[i]}" "${MBYTES}" )&
    done
    else
    echo "Please specify either -r for read or -w for write testing"
    _usage
    fi
    wait