286

I’m currently in the process of debugging a Cacti installation and want to create CPU load to debug my CPU utilization graphs.

I tried to simply run cat /dev/zero > /dev/null, which works great but only utilizes 1 core:

enter image description here

Is there a better method of testing/maxing-out system resources under load?

Related: How can I produce high CPU load on Windows?

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311

17 Answers17

314

Try stress It's pretty much an equivalent of the Windows consume.exe:

oliver$ ./stress --cpu 3
stress: info: [18472] dispatching hogs: 3 cpu, 0 io, 0 vm, 0 hdd
Mitesh Shah
  • 3,156
153

No need to install any extra package, your good old shell is able to do it alone.

This one-liner will load your four cores1 at 100%:

for i in 1 2 3 4; do while : ; do : ; done & done

How it works is quite simple, it starts four endless loops. Each of them is repeating the null instruction (:). Each loop is able to load a CPU core at 100%.

If you use bash, ksh93 and other shells supporting ranges, (i.e. not dash or older ksh), you can use this non portable syntax:

for i in {1..4}; do ...

Replace 4 with the number of CPUs you'd like to load if different from 4.

Assuming you had no background job already running when you launched one of these loops, you can stop the load generation with that command:

for i in 1 2 3 4; do kill %$i; done

Answering @underscore_d's comment, here is an enhanced version that simplify a lot stopping the load and that also allow specifying a timeout (default 60 seconds.) A Control-C will kill all the runaway loops too. This shell function works at least under bash and ksh.

# Usage: lc [number_of_cpus_to_load [number_of_seconds] ]
lc() {
  (
    pids=""
    cpus=${1:-1}
    seconds=${2:-60}
    echo loading $cpus CPUs for $seconds seconds
    trap 'for p in $pids; do kill $p; done' 0
    for ((i=0;i<cpus;i++)); do while : ; do : ; done & pids="$pids $!"; done
    sleep $seconds
  )
}

1Note that with CPUs supporting more than one thread per core (Hyper-threading), the OS will dispatch the load to all virtual CPUs. In that case, the load behavior is implementation dependent (each thread might be reported as 100% busy or not)..

jlliagre
  • 14,369
68

One alternative way would be

openssl speed -multi $(grep -ci processor /proc/cpuinfo)

or (if nproc is present)

openssl speed -multi $(nproc --all)

OpenSSL is almost always present on nowadays distros, so no extra packages needed.

rkosegi
  • 844
25

I made a simple python script which does the same. You can control the number of cpu cores you want to load. The good thing about this is that it won't consume any other resource besides the cpu. (I think mark johnson's idea would consume a lot of I/O resources, which is undesired here.)

from multiprocessing import Pool

def f(x):
    # Put any cpu (only) consuming operation here. I have given 1 below -
    while True:
        x * x

# decide how many cpus you need to load with.
no_of_cpu_to_be_consumed = 3

p = Pool(processes=no_of_cpu_to_be_consumed)
p.map(f, range(no_of_cpu_to_be_consumed))

Just run this script from the terminal $ python temp1.py. You need to kill the script when you are done.

Here, is my cpu consumption output when I load 3 of my cores.

Script temp1.py creates three processes (PIDs - 9377, 9378, 9379) which load 3 of my cores

24

Start two

sha1sum /dev/zero &

commands for every core in your system.

To stop

killall sha1sum

or

kill sha1sum
ecabuk
  • 391
11

I've been developing stress-ng, an updated stress tool that can stress a wide range of aspects of a Linux system. For more information, see http://kernel.ubuntu.com/~cking/stress-ng/

Usage is similar to stress

$ stress-ng --cpu 4 --vm 2 --fork 8 --switch 4 --timeout 1m
stress-ng: info:  [32254] dispatching hogs: 4 cpu, 8 fork, 4 switch, 2 vm
stress-ng: info:  [32254] cache allocate: default cache size: 8192K

Install with

sudo apt-get install stress-ng
Matt
  • 153
7

I usually take the cpuburn suite:

sudo apt-get install cpuburn
for i in {1..4}; do burnK7 & done

Replace 4 with the number of cores / HT-threads you have or want to stress.

Note: This stresses as much chip area as possible at the same time, it's programmed to generate maximum power dissipation. I had to write this post a second time, somehow my machine didn't like it :-(

You could also do cpuburn in sequences:

burnP6 & burnP6 & burnP6 & burnP6 & 
[1] 28520
[2] 28521
[3] 28522
[4] 28523

And when you want to stop them:

killall burnP6

You could also multiply burnP6 & to match the number of CPU cores on your system.

ce4
  • 933
3

Here's the way I use and there's no need to install anything extra.

For example to start with 4 processes,

nproc | xargs seq | xargs -n1 -P4 md5sum /dev/zero

You can change the number of processes by the option "-P" above.

yichun
  • 31
3

You can run that command as many times as you want, and it will take up a different core each time:

$ CORES=1
$ for i in `seq 1 $CORES`; do cat /dev/zero > /dev/null &
> done
[1] 8388
Christian Mann
  • 691
  • 8
  • 21
2

I combined both +jlliagre and +ecabuk.

#!/bin/bash
lc() {
    nowMs=$(date +%s)
    (
        pids=""
        cpus=${1:-1}
        seconds=${2:-60}
        echo "[$(date)] loading $cpus CPUs for $seconds seconds"
        echo "[$(date)] Expected completion: [$(date --date=@$(expr $nowMs + $seconds))]"
        trap 'for p in $pids; do kill $p; done' 0
        for ((i=0;i<cpus;i++)); do
            sha1sum /dev/zero &
            pids="$pids $!";
        done
        sleep $seconds
    )
    echo "[$(date)] Done"
}

lc $@
TJR
  • 193
2

pxz is a parallel implementation of xz.

pxz -9e /dev/zero --stdout >/dev/null should do the trick, as this is quite cpu intensive.

If /dev/zero is not fast enough (you notice that pxz gets I/O throttled) you can do pxz -9e /dev/zero --stdout | pxz -9e --stdout >/dev/null

Newer versions of xz have the --threads option which is a substitute for pxz.

2

I wanted to add this to @jlliagre's comment, but I don't have enough reputation. If you're going to use this code on multiple servers and the CPU count will vary, you can use the following command:

for ((i=1; i<=`nproc --all`; i++)); do while : ; do : ; done & done

This will utilize all of the cores on your server regardless of how many you have. The command nproc is part of coreutils so should be on most Linux installations.

2

https://github.com/GaetanoCarlucci/CPULoadGenerator

pretty simple and scientific solution.

Here you can see dynamics example in which 50% load is generated on CPU core 0:

enter image description here

You can run the process on other cores at the same time.

user
  • 163
1

Dockerized solution, using stress. Can be deployed using k8s to cover multiple machines.

https://hub.docker.com/r/jfusterm/stress

Sida Zhou
  • 121
  • 4
1

You can use:

fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read; killall dd

Repeat dd if=/dev/zero of=/dev/null for your CPU cores.

Hit any key to stop the test!

0

Run the program stress specifying all the cores you have on your CPU:

stress -c `nproc`

You can get it on Debian/Ubuntu: sudo apt install stress

0

A simple command line does it too:

x="x" ; while : ; do x=$x$x ; echo -n "." ; done
ott--
  • 2,251