Often I have to test Linux systems to see how do they work when they have an high CPU load.
To generate High CPU load (100%) I've written a small shell script and then I generalized it to work fine on multi core architectures.
The CPU consuming function uses dd to get data from the /dev/urandom device
and then uses gzip to make some CPU intensive job. Such a function is mono thread, so it generates 100% load on a single core. To generate 100% load on a multicore architecture, the script has to create multiple instances of the CPU consuming function. The script accept has input parameter the number of CPU consuming functions it has to launch..
The source code for the script is the following one:
#!/bin/bash
cpuConsumingFunc()
{
dd if=/dev/urandom | bzip2 -9 >> /dev/null;
}
fulload()
{
x=0
while [ $x -lt $1 ]; do
cpuConsumingFunc&
let x=x+1
done
};
echo "----- Starting CPU load"
fulload $1;
echo "Press enter to stop CPU load"
read;
echo "----- CPU load stopped"
killall dd
save the script as you like, since I have a lot of fantasy, I named it cpuLoad.sh. Then run
$ chmod +x cpuLoad.sh
This makes the script executable.
then run the script issuing the command
$ ./cpuLoad.sh <number of CPU load functions>
Nothing more
Great script, I’ll try for sure soon. What about the possibility to add a parameter to load all cpus available?
Thanks
Thank you Luca,
you can use nproc command to retrieve the number of cpu’s running on your system and then you can launch the number of instances (of cpuConsumingFunc) you need.
The script is working fine, thanks again. In the copy I have I added the following comments
# Simple script to generate CPU load
# You must specify, as a parameter, the number of cpu involved in the load
# To know how many cpu your system have use the nproc command
I noticed that using this without parameter an error is generated, maybe a default value (1 or all) or a hint could be useful.