How to stress test your CPU on Linux

There are many reasons why you may want to stress test the CPU on your Linux system. You may want to see how your operating system and hardware perform when you are at full CPU utilization in order to spot software bugs or hardware failures. Alternatively, you may want to generate a lot of heat fast to troubleshoot a temperature-related issue with your machine; maximizing the CPU utilization will do that. Whatever the reason, there is a fast and easy way to accomplish that goal.

In this tutorial you will learn:

  • How to perform stress tests on the CPU using the yes stress test
  • How to perform stress tests on the CPU using the stress command
  • How to perform stress tests on the CPU using the s-tui command
How to stress test your CPU on Linux

How to stress test your CPU on Linux

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software yes, getconf, seq, bash, stress, s-tui
Other No root privileges required for the yes stress test. Privileged access to your Linux system as root or via the sudo command may be required to install other stress test packages.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Usage Scenario

The yes stress test detailed in this article can be very useful for troubleshooting purposes. Frequently, intermittent issues do not become reproducible until the system is under heavy load, sometimes for a prolonged period of time. You may find yourself in a situation where your machine, or one you are maintaining for a user, is experiencing unexpected shutdowns, kernel panics or other intermittent issues. In this situation you may want to attempt to reproduce the issue. In that case, you could run the following command to stress test the processor, making the issue more likely to occur, and therefore observable to you.

After you have taken steps to remedy the situation such as uninstalling software, reinstalling software including the operating system or replacing hardware components you could run the command again to determine whether or not it has resolved the issue.

Yes Stress Test

This one-liner will create a yes process to run on each processor core of the machine. yes prints the letter y repeatedly until it is killed. On an idle system, each yes process will utilize 100% of a CPU core. If the processor supports hyper-threading and it is enabled then it will create twice as many processes, as this is necessary to fully maximize the CPU utilization.

The benefit to this approach is that it requires only standard utilities that come out of the box on GNU/Linux systems, so no installation of additional programs or libraries are needed. Additionally, it does not require root privileges to run. To begin, enter the following command into your terminal.

$ for i in $(seq $(getconf _NPROCESSORS_ONLN)); do yes > /dev/null & done



Let’s break down exactly what this one-liner is doing. getconf _NPROCESSORS_ONLN obtains the number of CPU cores; including virtual ones for processors with hyper-threading. Running the command within $() places it’s output as an argument to the seq command.

seq $(getconf _NPROCESSORS_ONLN) prints a sequence of numbers from 1 up to the amount of virtual CPU cores present in the system. Running that within $() allows that sequence of numbers to be used in our bash for loop.

Finally, the for loop itself, for i in $(seq $(getconf _NPROCESSORS_ONLN)); do yes > /dev/null & done takes the sequence of numbers from 1 to the amount of virtual cores and for each one of them starts a yes process and redirects it’s output to /dev/null.

htop after running the command

htop after running the command

When running this command it is advisable to use top, htop or some other program to monitor the CPU utilization in order to verify that it is reaching 100%. We previously published an article on how to check and monitor cpu utilization on linux.

When you want to stop the yes processes and return to normal CPU utilization, simply enter the following into your terminal.

$ killall yes

Other CPU Stress Test Methods

Stress

Stress is a simple workload generator that imposes a configurable amount of stress on the system. In addition to being able to stress test the CPU, it is also able to perform memory, I/O and disk stress on a system.

On Arch Linux and Manjaro it can be installed with the following command.

$ pacman -S stress

On Debian, Ubuntu and Mint it can be installed with the following command.

$ sudo apt install stress

On RHEL based distros it can be installed with the following command after enabling the Extra Packages for Enterprise Linux (EPEL) Repository

$ sudo yum install stress

To perform a stress test with stress, simply enter the following command where the number used in --cpu is the amount of threads to start. To fully stress the CPU, this should be the total number of CPU cores or double that if the CPU supports hyper-threading. You can obtain the appropriate number to use by entering getconf _NPROCESSORS_ONLN. In our example we are performing the stress test on a quad core i7 which supports hyper-threading, so we use 8.

$ stress --cpu 8


S-tui

s-tui is a text user interface front-end for the stress command. In addition to running the stress test performed by stress, s-tui also monitors CPU temperature, frequency, power and utilization while displaying graphs corresponding to each value in the terminal. s-tui can be installed across all distributions by using pip. To do so, enter the following command.

$ pip install s-tui --user

To run the stress test enter the s-tui command into your terminal, then press the down arrow (or j key) and press enter to switch from monitor mode to stress mode. You will see a graphical representation similar to the following screenshot.

s-tui

s-tui

Conclusion

In this article we saw how to maximize the CPU utilization on your Linux system using the yes command within a bash for loop to perform a “yes stress test”. We then broke down each part of the command to see exactly what it was doing and how it worked. We discussed monitoring the CPU utilization to verify that it is reaching 100%, then we saw how to install and use stress and s-tui to perform a CPU stress test.

The benefit to using s-tui is that you are able to monitor performance without using any additional software. The benefit to using the “yes stress test” is that you are able to perform the stress test without having to install any additional software. If you are looking to run more extensive stress tests and benchmarking on your Linux system then our article on how to benchmark your linux system has you covered.



Comments and Discussions
Linux Forum