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

As a System Administrator, examining and monitoring how your Linux systems perform under heavy workloads is crucial for maintaining optimal performance and reliability.

Stress testing provides System Administrators and Programmers with valuable insights to:

  • Fine-tune system activities and optimize configurations.
  • Monitor operating system kernel interfaces under load.
  • Test Linux hardware components such as CPU, memory, disk devices, and more to observe their performance under stress.
  • Measure different power-consuming loads and their impact on the system.

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 and, 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

Here are some key options 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 to Use stress on Linux Systems

Here are a few example commands to get started with stress:

1. Basic CPU Stress Test

To examine the effect of stress testing, first run the uptime command to note the load average.

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

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

2. CPU Stress Test with Verbose Output

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. Virtual Memory Stress Test

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. Combined Stress Test (CPU, I/O, and Memory)

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 to Use ‘stress-ng’ on Linux Systems

The following are a few stress-ng test example commands to get started:

1. CPU Stress Test with Metrics

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. Specific CPU Method Test (FFT)

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. Hard Drive Stress Test

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. Combined Stress Test (CPU, I/O, and Virtual Memory)

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

Real-World Use Cases for Stress Testing in Linux

Understanding when and why to use stress testing tools can help you maintain better system health.

1. Load Testing Before New Server Deployment

Before putting a new server into production, run comprehensive stress tests to ensure it can handle expected workloads, which will help you identify hardware issues early:

sudo stress-ng --cpu 0 --vm 2 --vm-bytes 80% --timeout 10m --metrics-brief

The --cpu 0 option automatically uses all available CPU cores, while --vm-bytes 80% uses 80% of available memory.

2. Testing Load After Hardware Upgrades

After upgrading RAM, CPU, or storage, verify that the new hardware is functioning correctly:

sudo stress-ng --cpu 0 --io 4 --hdd 2 --timeout 5m --verify

The --verify option checks that the stress test operations are producing correct results.

3. Reproducing Production Load

Simulate production workload patterns to test system stability before deploying new applications:

sudo stress-ng --cpu 4 --cpu-load 75 --vm 2 --vm-bytes 2G --timeout 30m

The --cpu-load 75 option keeps CPU usage at approximately 75% rather than maxing it out.

Summary

As recommended, these tools should be used with superuser privileges due to their significant impact on system resources.

Both stress and stress-ng are excellent tools for general System Administration tasks in Linux environments.

Understanding how your systems perform under stress helps you make informed decisions about capacity planning, hardware upgrades, and system optimization.

I hope this guide was useful. If you have any additional ideas on how to test the health status of your system using these tools or any other stress testing utilities, feel free to share your thoughts in the comments below.

Similar Posts