Skip to content

Instantly share code, notes, and snippets.

@ricky9w
Last active November 15, 2022 14:45
Show Gist options
  • Save ricky9w/2c7facb51fc7e6d99e80aaf866662eb4 to your computer and use it in GitHub Desktop.
Save ricky9w/2c7facb51fc7e6d99e80aaf866662eb4 to your computer and use it in GitHub Desktop.
Bash script for a comprehensive stress testing on CPU, memory, system I/O and disk

Comprehensive Stress Testing

Comprehensive stress testing on CPU, memory, system I/O and disk

Dependencies

  • stress
  • jq
  • lm-sensors

For installation instructions please refer to:

Follow instructions here to have lm-sensors set up:

How to use lm-sensors? - Ask Ubuntu

Usage

sh stressme.sh | tee stress.log

This will do stress testing with stress and log hardware temperatures at 0.5 second intervals (may be affected by high system load).

parameters:

  • -c | --cpu : how many cpu stressors to run, default is 2
  • -m | --memory : how many memory stressors to run, default is 2
  • -i | --io : how many I/O stressors to run, default is 2
  • -d | --disk : how many disk stressors to run, default is 2
  • -t | --time : how long to conduct stress testing, default is 5m

These parameters are passed through to stress command, refer to its man page for more details:

stress(1): impose load on/stress test systems - Linux man page (die.net)

#!/bin/sh
## Comprehensive stress testing on CPU, memory, system I/O and disk
## Author: Richard Wang(ricky9wang@gmail.com)
## Version 2022/11/15
# Default values
CPU=2
MEM=2
IO=2
DISK=2
TIME="5m"
main() {
parse_parameters $@
stress -c $CPU -m $MEM -d $DISK -i $IO -t $TIME &
stress=$!
trap "kill -9 -$(ps -o pgid= $stress | grep -o '[0-9]*')" EXIT INT TERM
sleep 1
printf "%-8s\t%-8s\t%-8s\t%-8s\t%-8s\n" CPU_TEMP PCH_TEMP SSD_TEMP1 SSD_TEMP2
while kill -0 $stress 2>/dev/null; do
log_temperature
sleep 0.5
done
# Disable the trap on a normal exit
trap - EXIT INT TERM
}
# Log hardware temperatures
# Output as JSON format and parse it with jq
log_temperature() {
local cpu_temp="$(sensors -j 2>/dev/null | jq '."coretemp-isa-0000"."Package id 0"."temp1_input"')"
local pch_temp="$(sensors -j 2>/dev/null | jq '."pch_cannonlake-virtual-0"."temp1"."temp1_input"')"
local ssd_temp1="$(sensors -j 2>/dev/null | jq '."nvme-pci-0200"."Sensor 1"."temp2_input"')"
local ssd_temp2="$(sensors -j 2>/dev/null | jq '."nvme-pci-0200"."Sensor 2"."temp3_input"')"
printf "%-8s\t%-8s\t%-8s\t%-8s\n" $cpu_temp $pch_temp $ssd_temp1 $ssd_temp2
}
# Parse user output
parse_parameters() {
while [ $# -gt 0 ]; do
case $1 in
-c|--cpu)
CPU="$2"
shift
shift
;;
-m|--memory)
MEM="$2"
shift
shift
;;
-i|--io)
IO="$2"
shift
shift
;;
-d|--disk)
DISK="$2"
shift
shift
;;
-t|--time)
TIME="$2"
shift
shift
;;
*)
echo "Unknown flag $1"
exit 1
;;
esac
done
}
main "$@"; exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment