summaryrefslogtreecommitdiff
path: root/automated/linux/device-read-perf/device-read-perf.sh
blob: dd00a2dce7bad208ef90b8eeb7dbb77fbdc50cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh -e

. ../../lib/sh-test-lib
OUTPUT="$(pwd)/output"
RESULT_FILE="${OUTPUT}/result.txt"
export RESULT_FILE

usage() {
    echo "Usage: $0 [-d <sda|mmcblk0>] [-s <true|false>]" 1>&2
    exit 1
}

while getopts "d:s:" o; do
  case "$o" in
    d) device_list="${OPTARG}" ;;
    s) SKIP_INSTALL="${OPTARG}" ;;
    *) usage ;;
  esac
done

! check_root && error_msg "You need to be root to run this script."
[ -d "${OUTPUT}" ] && mv "${OUTPUT}" "${OUTPUT}_$(date +%Y%m%d%H%M%S)"
mkdir -p "${OUTPUT}"

install_deps "hdparm" "${SKIP_INSTALL}"

# Test all block devices if device not specified.
if [ -z "${device_list}" ]; then
    if lsblk | egrep "^(sd|hd|mmcblk)[a-z0-9] "; then
        device_list=$(lsblk \
                      | egrep "^(sd|hd|mmcblk)[a-z0-9] " \
                      | awk '{print $1}')
    else
        error_msg "Block device NOT found"
    fi
fi

# Test run.
# 'hdparm -t' should be repeated 2-3 times for meaningful result.
for device in ${device_list}; do
    echo
    sum=0
    for i in $(seq 3); do
       info_msg "Running iteration $i on /dev/${device}"
       output_file="${OUTPUT}/${device}-iteration$i-output.txt"
       hdparm -t /dev/"${device}" > "${output_file}" 2>&1
       result=$(grep "reads" "${output_file}" | awk '{print $(NF-1)}')
       units=$(grep "reads" "${output_file}" | awk '{print substr($NF, 1, 2)}')

       # Convert result to MB when units isn't MB.
       result=$(convert_to_mb "${result}" "${units}")

       echo "Device read timings: ${result} MB/sec"
       sum=$(echo "${sum} ${result}" | awk '{print $1+$2}')
    done

    result_avg=$(echo "${sum}" | awk '{print $1/3}')
    echo "${device} average read timings: ${result_avg}"
    add_metric "${device}-read-perf" "pass" "${result_avg}" "MB/sec"
done