CPU and memory (VM) stress testing on a Linux/Unix with Stress-ng

by time news

A system administrator can try any of the following tools to put given subsystems under a specific load. One can do a CPU stress test on another Linux subsystem. Cases where this is useful include those where a system administrator wants to perform tuning activities, a kernel or libc programmer wants to assess denial-of-service possibilities, fully test their systems under high load, and monitor health and further. This is also useful for system administrators, system builders, and overclockers who want to test their hardware under high load and monitor stability and thermal environment.

walkthrough details
Difficulty levelAdvanced
root privilegesYeah
Requirementsestrés y estrés-ng
Est. time to read8 minutes

Tools for stress testing CPU and memory (VM) on Linux/Unix

  1. stress: It is a simple workload generator for POSIX systems. It imposes a configurable amount of CPU, memory, I/O, and disk stress on the system. It is written in C and is free software licensed under the GPLv2 license. It is not a benchmark, but a tool designed for that purpose.
  2. stress-of: It is an updated version of the stress tool and will stress test a server for the following features:
    1. cpu compute
    2. cache hit
    3. Unit Stress
    4. I/O timings
    5. virtual machine stress
    6. Estres de Sockets
    7. context switch
    8. Process of creation and completion.
    9. Includes 60+ different stress tests, 50+ CPU-specific stress tests that exercise floating point, integer, bit manipulation, and control flow, 20+ virtual memory stress tests.

Warning: It is recommended to run the following tools with root privileges to avoid out of memory and other errors. Also, keep in mind that the tools will stress your server resources quickly, so use the following command wisely.

Getting started with the stress tool on Linux

This program is supposed to be easy to use and recommended for new system administrators. The tool is known to work on x86 Linux and FreeBSD/OpenBSD, powerpc AIX and Linux, SPARC Solaris, Compaq Alpha Tru64 UNIX and many others.

Install the package of stress in a Linux

You can install stress as part of the Linux or Unix distribution.

Install stress on CentOS, RHEL and Fedora Linux

First, enable the EPEL repository and then type the following yum command to install the same:

sudo yum install stress

Install stress on a Debian and Ubuntu Linux

Type the following apt-get command to install the same:

apt-get install stress

Install stress on a FreeBSD Unix system

Type the following pkg command to install the stress tool using the binary method:

pkg install stress
## O ##
pkg install sysutils/stress

Install stress on an OpenBSD Unix system

Type the following pkg_add command to install the stress tool using the binary method:

## si la ruta de instalación no está configurada en /etc/pkg.conf de la siguiente manera ##
## installpath = http://mirror.esc7.net/pub/OpenBSD/%c/packages/%a/ use ## 
## PKG_PATH ##
export PKG_PATH=http://ftp.usa.openbsd.org/pub/OpenBSD/`uname -r`/packages/`arch -s` 
pkg_add stress

How do I use the stress tool on Linux?

First, note the current system load averages by typing the following command:

# uptime

Then run any of the following commands to see the load on the screen:

# watch uptime

Or use the tload command:

# tload

The syntax is as follows:

stress [OPCIÓN]
## Estrés usando una tarea vinculada a la CPU
stress -c 4
## Estrés usando una tarea enlazada a IO
stress -i 2

For example, a load average of four is imposed on the system by specifying two CPU-bound processes, one I/O-bound process, and one memory-allocation process as follows:

# uptime

# stress -c 2 -i 1 -m 1 --vm-bytes 128M -t 10s

# uptime

Sample outputs:

[root@localhost ]# uptime                v
07:44:52 up 6:15, 1 user, load average: 0.84, 0.71, 0.59
[root@localhost ]# stress -c 2 -i 1 -m 1 --vm-bytes 128M -t 10s

strees: info: [25326] dispatching hogs: 2 cpu, 1 io, 1 vm 0 hdd
strees: info: [25326] successful run completed in 13s

[root@localhost ]# uptime
07:44:52 up 6:15, 1 user, load average: 1.23, 0.71, 0.59
^

Fig.04: An average load of four is imposed on the system

Where,

  • -c 2: spawn two workers spinning in sqrt()
  • -i 1: spawn a worker spinning in sync()
  • -m 1: Spawn a spinning worker in malloc()/free()
  • --vm-bytes 128M: Malloc 128 MB per virtual machine worker (default is 256 MB)
  • -t 10s: Waiting time after ten seconds
  • -v: Be detailed

Getting started with stress-ng on Linux and Unix

The tool stress-of will test a Linux computer system in several selectable ways.

Install stress-ng on a Linux or Unix-like system

Type the following command to download the stress-ng tarball using the wget command:

$ cd /tmp
$ wget http://kernel.ubuntu.com/~cking/tarballs/stress-ng/stress-ng-0.09.34.tar.xz

coat the package

$ tar xvf stress-ng-0.09.34.tar.xz

Compile stress-ng, execute:

$ cd stress-ng-0.09.34
$ make

Sample outputs:

hacer -f Makefile.config
make[1]: Ingresando al directorio '/tmp/stress-ng-0.09.34'
configuración automática: usando -lcrypt
configuración automática: usando pthread spinlock
configuración automática: usando -lrt
configuración automática: usando -lz
configuración automática: usando -ldl
configuración automática: usando -lpthread
configuración automática: usando wchar.h
configuración automática: usando grp.h
configuración automática: usando sys/xattr.h
configuración automática: usando sys/syscall.h
.....
...
.....
CC mwc.c
CC parse-opts.c
CC sin memoria.c
CC net.c
CC programado.c
Configuración de CC.c
CC zona termal.c
Cuña CC.c
CC perf.c
CC thrash.c
CC tiempo.c
CC estrés-ng.c
CC estrés-personalidad.c
LD estrés-ng
make[1]: dejando el directorio '/tmp/stress-ng-0.09.34'

You can also install it using the snap command or the apt-get command:

$ sudo apt install stress-ng

How do I use the stress-ng tool on Linux?

The syntax is:

stress-ng [opciones]
stress-ng -c 2
stress-ng -c 4 -t 10 -v
stress-ng -c 4 --metrics-brief

examples

Always write down the output of the uptime command before starting it:

uptime

Let’s look at some examples of stress-ng.

Unix/Linux CPU stress test

Let’s start N workers exercising the CPU by working sequentially through all the different methods of CPU stress:

uptime
stress-ng --cpu 4 --timeout 60s --metrics-brief

Unix/Linux CPU stress test

For N disk boot workers to continually write, read, and delete temporary files:

stress-ng --disk 2 --timeout 60s --metrics-brief

option can be passed --I N to the stress-ng command to send the buffer cache to disk:

stress-ng --disk 2 --io 2 --timeout 60s --metrics-brief

Unix/Linux memory stress test

Let’s populate memory. Use mmap N bytes per virtual machine worker, default is 256 MB. You can specify the size as a % of total available memory or in units of Bytes, KBytes, MBytes, and GBytes using the suffix b,k,mog:

stress-ng --vm 2 --vm-bytes 1G --timeout 60s

Begin to --vm 2N workers (2 workers) continually calling mmap/munmap and writing to allocated memory. Note that this can cause systems to trigger the OOM killer kernel on Linux systems if there is not enough physical memory and swap available.

putting it all together

To run for 60 seconds with 4 cpu stressors, 2 io stressors, and 1 vm stressor using 1 GB of virtual memory, enter:

stress-ng --cpu 4 --io 2 --vm 1 --vm-bytes 1G --timeout 60s --metrics-brief

In this example, run 16 CPU stressors and stop after 900000 operations:

stress-ng --cpu 16 --cpu-ops 900000
stress-ng --cpu 16 --cpu-ops 900000 --timeout 16

Sample outputs:

stress-ng: info: [30367] despachando cerdos: 16 cpu
stress-ng: info: [30367] ejecución exitosa completada en 60,17 s (1 min, 0,17 s)

To run 4 concurrent instances of all stressors sequentially one by one, each for 6 minutes, and summaries with performance metrics at the end:

stress-ng --sequential 4 --timeout 6m --metrics

To run 2 FFT CPU stressors, stop after 5000 bogo operations and produce a summary for the FFT results only:

stress-ng --cpu 2 --cpu-method fft --cpu-ops 5000 --metrics-brief

To run CPU stressors on all online CPUs running with all available CPU stressors for 2 hours:

stress-ng --cpu 0 --cpu-method all -t 2h

To run 2 instances of all stressors for 10 minutes:

stress-ng --all 2 --timeout 10m

To run 128 stressors that are randomly chosen from all available stressors:

stress-ng --random 128

To run 64 instances of all the different CPU stressors and check that the calculations are correct for 5 minutes with a bogo summary of operations at the end:

stress-ng --cpu 64 --cpu-method all --verify -t 5m --metrics-brief

To run all stressors one by one for 5 minutes, with the number of instances of each stressor matching the number of CPUs online:

stress-ng --sequential 0 -t 5m

To run all the stressors in the io class one by one for 1 minute each, with 8 instances of each stressor running simultaneously, and display the overall time utilization statistics at the end of the run:

stress-ng --sequential 8 --class io -t 1m --times

Should I run stess-ng with root access?

From the man page:

Running stress-ng with root privileges will adjust memory settings on Linux systems so that stressors cannot be removed in low memory situations, so use it wisely. With the appropriate privilege, stress-ng can allow the ionice class and ionice levels to be adjusted, again this should be used with care. However, some options require root privileges to modify various controls on the /sys interface. See the stess-ng command man page for more information.

Conclusion

You have just learned how to use the stess command and stress-ng to impose a high CPU load on Linux and Unix-like systems.

Pin It

You may also like

Leave a Comment