Script to Stress CPU and Log Temperatures


I keep tinkering with computers, be it Servers, Desktops, Laptops or Single Board Computers like the Raspberry Pi. Of keen interest to me is the thermal performance of these devices. Most new devices implement thermal throttling and start stifling CPU performance if the temperature of the CPU touches the maximum levels. Detecting such incidences is important and executing benchmarking tests is an easy method to simulate CPU stress.

I needed a program that would run CPU stressing software in an unending loop and log the results to a CSV file which I could use later to create plots etc. Bash scripting makes it relatively easy to create such a program. My sample script runs the sysbench program to stress the CPU and logs the program execution time along with CPU temperature. It is of-course possible to run any other stress generator program suitable for your requirements and to take measurements of other system parameters (where supported) such as GPU Temperature, HDD Temperature, CPU Frequency, CPU Voltage.

Stress testing takes a severe toll on your device. Devices with weakness in one or more component will certainly fail at some point in these looping tests. While it may be your intention, do beware of the consequences and do not test all sub-systems of your device unnecessarily.

Download Link: http://www.rajib.com/wp-content/uploads/2018/08/linux-script-stressme.zip

Source Code:

#!/bin/sh
# Script by Rajib Ghosh

# Check if BC installed. If not, exit.
if [ ! -f /usr/bin/bc ]
then
echo "Cannot find 'bc' calculator package."
exit
fi

myLoopCount=0
myExecTime=0
myCPUTemp=$(echo "scale=2; "`cat /sys/class/thermal/thermal_zone0/temp`"/1000" | bc -l)
mySysTime=`date +%s`
myLogFile="stressme-"$mySysTime".log"

echo "Beginning loop. Press ^C to exit"
echo "SysTime: "$mySysTime "| Loop: "$myLoopCount "| CPUTemp: "$myCPUTemp "| ExecTime: "$myExecTime
echo "SysTime,LoopCount,CPUTemp,ExecTime" > $myLogFile

while [ 1 ]
do
myLoopCount=$((myLoopCount+1))
myCPUTemp=$(echo "scale=2; "`cat /sys/class/thermal/thermal_zone0/temp`"/1000" | bc -l)
mySysTime=`date +%s`

# Replace the commands below with whatever you want
sysbench --num-threads=4 --test=cpu run --cpu-max-prime=1000 > /dev/null
# End commands section

myExecTime=$(expr `date +%s` - $mySysTime)
echo $mySysTime","$myLoopCount","$myCPUTemp","$myExecTime >>$myLogFile
echo "SysTime: "$mySysTime "| Loop: "$myLoopCount "| CPUTemp: "$myCPUTemp "| ExecTime: "$myExecTime
done

The vcgencmd command available on Raspbian reveals a lot of system information such as CPU Core Frequency, CPU Voltage etc. The lm-sensors package also provides a wealth of sensor information.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.