How to Impose High CPU Load and Stress Test on Linux

If you’re a System Administrator, it’s important to examine and monitor the status of your Linux systems when they are under stress of heavy workloads.

This can be a good way for System Administrators and Programmers to:

  • fine-tune activities on a system.
  • monitor operating system kernel interfaces.
  • test your Linux hardware components such as CPU, memory, disk devices, and many others to observe their performance under stress.
  • measure different power-consuming loads on a system.

In this guide, we will explore two important tools, stress, and stress-ng, for conducting stress tests on your Linux systems.

Linux Stress Testing Tools: stress and stress-ng

stress and stress-ng are essential tools for assessing and testing the performance of Linux systems under various conditions.

stress Tool

stress is a simple yet powerful tool designed to impose a configurable amount of CPU, memory, I/O, or disk stress on a Linux system. By simulating heavy workloads, allows administrators to observe how the system responds under pressure.

This tool is valuable for identifying potential weaknesses and ensuring that the system can handle demanding tasks without compromising performance.

stress-ng Tool

stress-ng, an extended version of stress, goes beyond the basic functionalities of its predecessor. It provides a wider range of stress tests, covering not only CPU, memory, I/O, and disk stress but also incorporating additional tests for things like inter-process communication, sockets, and various file operations.

Both stress and stress-ng contribute significantly to the proactive management of Linux systems, enabling administrators and programmers to optimize system configurations, identify potential issues, and enhance overall system reliability.

Important: It is highly recommended that you use these tools with root user privileges because they can stress your Linux machine so fast and to avoid certain system errors on poorly designed hardware.

How to Install ‘stress’ Tool in Linux

To install stress on Linux, use the following appropriate command for your specific Linux distribution.

sudo apt install stress         [On Debian, Ubuntu and Mint]
sudo yum install stress         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/stress  [On Gentoo Linux]
sudo apk add stress             [On Alpine Linux]
sudo pacman -S stress           [On Arch Linux]
sudo zypper install stress      [On OpenSUSE]    
sudo pkg install stress         [On FreeBSD]

The general syntax for using stress is:

sudo stress option argument

Some options that you can use with stress.

  • To spawn N workers spinning on sqrt() function, use the --cpu N option.
  • To spawn N workers spinning on sync() function, use the --io N option.
  • To spawn N workers spinning on malloc()/free() functions, use the --vm N option.
  • To allocate memory per vm worker, use the --vm-bytes N option.
  • Instead of freeing and reallocating memory resources, you can redirty memory by using the --vm-keep option.
  • Set sleep to N seconds before freeing memory by using the --vm-hang N option.
  • To spawn N workers spinning on write()/unlink() functions, use the --hdd N option.
  • You can set a timeout after N seconds by using the --timeout N option.
  • Set a wait factor of N microseconds before any work starts by using the --backoff N option.
  • To show more detailed information when running stress, use the -v option.
  • Use --help to view help for using stress or view the manpage.

How Do I Use stress on Linux systems?

1. To examine the effect of the command every time you run it, first run the uptime command and note down the load average.

Next, run the stress command to spawn 8 workers spinning on sqrt() with a timeout of 20 seconds. After running stress, again run the uptime command and compare the load average.

uptime
sudo stress --cpu 8 --timeout 20
uptime
Stress Test Linux System
Stress Test Linux System

2. To spawn 8 workers spinning on sqrt() with a timeout of 30 seconds, showing detailed information about the operation, run this command:

uptime
sudo stress --cpu 8 -v --timeout 30s
uptime
Executing CPU Stress Test with 8 Threads
Executing CPU Stress Test with 8 Threads

3. To spawn one worker of malloc() and free() functions with a timeout of 60 seconds, run the following command.

uptime
sudo stress --vm 1 --timeout 60s
uptime
Conducting Virtual Memory Stress Test
Conducting Virtual Memory Stress Test

4. To spawn 4 workers spinning on sqrt(), 2 workers spwaning on sync(), 2 workers on malloc()/free(), with a time out of 20 seconds and allocate a memory of 256MB per vm worker, run this command below.

uptime
sudo stress --cpu 4 --io 3 --vm 2 --vm-bytes 256M --timeout 20s
uptime
Multifaceted Stress Test: CPU, I/O, and Virtual Memory
Multifaceted Stress Test: CPU, I/O, and Virtual Memory

How to Install ‘stress-ng’ Tool in Linux

To install stress-ng on Linux, use the following appropriate command for your specific Linux distribution.

sudo apt install stress-ng         [On Debian, Ubuntu and Mint]
sudo yum install stress-ng         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/stress-ng  [On Gentoo Linux]
sudo apk add stress-ng             [On Alpine Linux]
sudo pacman -S stress-ng           [On Arch Linux]
sudo zypper install stress-ng      [On OpenSUSE]    
sudo pkg install stress-ng         [On FreeBSD]

The general syntax for using stress-ng is:

sudo stress-ng option argument

Some of the options that you can use with stress-ng:

  • To start N instances of each stress test, use the –all N option as follows.
  • To start N processes to exercise the CPU by sequentially working through all the different CPU stress testing methods, use the –cpu N option as follows.
  • To use a given CPU stress testing method, use the –cpu-method option. There are many methods available that you can use, to view the manpage to see all the methods to use.
  • To stop the CPU stress process after N bogo operations, use the –cpu-ops N option.
  • To start N I/O stress testing processes, use the –io N option.
  • To stop io stress processes after N bogo operations, use the –io-ops N option.
  • To start N vm stress testing processes, use the –vm N option.
  • To specify the amount of memory per vm process, use the –vm-bytes N option.
  • To stop vm stress processes after N bogo operations, use –vm-ops N options
  • Use the –hdd N option to start N harddisk exercising processes.
  • To stop hdd stress processes after N bogo operations, use the –hdd-ops N option.
  • You can set a timeout after N seconds by using the –timeout N option.
  • To generate a summary report after bogo operations, you can use –metrics or –metrics-brief options. The –metrics-brief displays non-zero metrics.
  • You can also start N processes that will create and remove directories using mkdir and rmdir by using the –dir N option.
  • To stop directory operations processes use –dir-ops N options.
  • To start N CPU consuming processes that will exercise the present nice levels, include the
  • –nice N option. When using this option, every iteration will fork off a child process that runs through all the different nice levels running a busy loop for 0.1 seconds per level, and then exits.
  • To stop nice loops, use the –nice-ops N option as follows.
  • To start N processes that change the file mode bits via chmod(2) and fchmod(2) on the same file, use the –chmod N option. Remember the greater the value for N then the more contention on the file. The stressor will work through all the combinations of mode bits that you specify with chmod(2).
  • You can stop chmod operations by the –chmod-ops N option.
  • You can use the -v option to display more information about ongoing operations.
  • Use -h to view help for stress-ng.

How Do I use ‘stress-ng’ in Linux systems?

1. To run 8 CPU stressors with a timeout of 60 seconds and a summary at the end of operations.

uptime
sudo stress-ng --cpu 8 --timeout 60 --metrics-brief
uptime
Performing CPU Stress Test with 8 Threads
Performing CPU Stress Test with 8 Threads

2. To run 4 FFT CPU stressors with a timeout of 2 minutes.

uptime
sudo stress-ng --cpu 4 --cpu-method fft --timeout 2m
uptime
Executing CPU Stress Test with 4 Threads
Executing CPU Stress Test with 4 Threads

3. To run 5 hdd stressors and stop after 100000 bogo operations, run this command.

uptime
sudo stress-ng --hdd 5 --hdd-ops 100000
uptime
Linux Hard Drive Stress Test
Linux Hard Drive Stress Test

4. To run 8 CPU stressors, 4 I/O stressors, and 1 virtual memory stressor using 1GB of virtual memory for one minute, run this command below.

uptime
sudo stress-ng --cpu 4 --io 4 --vm 1 --vm-bytes 1G --timeout 60s --metrics-brief
uptime
Stress Test: CPU, I/O, and Virtual Memory
Stress Test: CPU, I/O, and Virtual Memory
Summary

As recommended, these tools should be used with superuser privileges as they have certain effects on the system. These tools are good for general System Administration in Linux.

I hope this guide was useful and if you have any additional ideas on how to test the health status of your system using these tools or any other tools, do not hesitate to post a comment.

Always stay connected to Tecmint, learn, and share ideas for the benefit of open source.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

10 thoughts on “How to Impose High CPU Load and Stress Test on Linux”

  1. When I try to GPU burn test there are errors showing the:

    gpu_burn-drv.cpp:in function int main(int, char**)
    gpu_burn-drv.cpp:816:23: error: runtime_error is not a member of std throw std: : runtime_error("No cuda capable gpu found.\n)

    But when I install ubuntu on the same server GPU burn test is ok so I can’t understand what is the issue so anyone tells me what to do……

    Reply
  2. I am using this stress-ng image and applying chaos at K8’s level as I am building chaos as a service. I am planning to put CPU stress. I was expecting the CPU % to spike > 80%. My hypothesis is similar to your uptime values ~ 1- 2% and when I apply CPU stress it is hardly spiking up to 10%. I tried various params but this is the max spike I get. Can we ever make CPU stress > 80% ? as I see even your load averages pretty low.

    Reply
  3. --vm-bytes is NOT per vm worker but for all workers combined, so you can put almost all your memory if you boot without X.

    Reply
    • @Shashi,

      Yes, you can install stress-ng tool on FreeBSD system to imposes certain types of high CPU Load on your FreeBSD based Unix system.

      This stress-ng tool can be installed via ports as shown.

      # cd /usr/ports/sysutils/stress/
      # make install clean
      # pkg install sysutils/stress
      
      Reply
  4. The article states: “It is highly recommended that you use these tools with root user privileges”. The manual does not recommend this:

    Running stress-ng with root privileges will adjust out of memory set‐
    tings on Linux systems to make the stressors unkillable in low memory
    situations, so use this judiciously. With the appropriate privilege,
    stress-ng can allow the ionice class and ionice levels to be adjusted,
    again, this should be used with care.

    Reply

Leave a Reply to JLockard Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.