#!/bin/bash #$Id: diskbench,v 1.1 2022/10/23 21:22:31 RogerSeguin Exp $ ######################################################################## # Copyright (c) 2022 Roger Seguin # https://frogzie.duckdns.org # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ######################################################################## # Description: # Disk benchmarking tool # Use "dd" for writing and "hdparm" for reading measurement declare -r prg=$(basename $0) declare -r baseName="$prg.dat" declare blockSize_kiB=64 declare fileName="$baseName" declare sizeMiB=1024 usage() { cat << :end: 1>&2 $prg - Disk Performance Measurement Usage $prg [-b blockSize_kiB] [-f tmpfile] [sizeMiB] Options -b blockSize: write by blocks of indicated size in kibibytes (default: 64 kiB) -f file: temporary file used for disk benchmarking (default: "$baseName") Description Time write/read the benchmarking file tmpfile (default size: 1024 MiB) If tmpfile is a directory, "$baseName" will be used Needs to be run as "root" for retrieving read statistics tmpfile is removed after measurement :end: exit 1 } while getopts ":b:f:h" opt; do case $opt in b) blockSize_kiB="$OPTARG" ;; f) path="$OPTARG" l=${#path} [ "$(echo $path | cut -c$l-)" = "/" ] && \ path=$(echo $path | cut -c-$((l-1))) [ -d "$path" ] && path=$path/$baseName fileName=$path ;; h|\?) usage ;; esac done shift $((OPTIND-1)) [ $# -le 1 ] || usage [ $# -eq 1 ] && sizeMiB=$1 [[ $sizeMiB =~ ^[0-9]+$ ]] || usage blockSize_B=$((blockSize_kiB*1024)) iteration=$((sizeMiB*1024*1024/blockSize_B)) wstats=$(dd if=/dev/zero of=$fileName conv=fsync bs=$blockSize_B count=$iteration 2>&1 | grep -v ' records ') echo "Write: $wstats" if [ $(id -u) -eq 0 ]; then device=$(df --output=source $fileName | grep -v Filesystem) hdparm -tT $device else echo "Read statistics would require to be run as \"root\"" fi rm -f $fileName exit 0 #$Log: diskbench,v $ #Revision 1.1 2022/10/23 21:22:31 RogerSeguin #Initial revision #