DEV Community

How to Simulate High CPU Usage on AWS Ubuntu Instances for Testing and Performance Optimization

To increase CPU usage for testing in an AWS Ubuntu instance, you can use tools like stress, stress-ng, or custom scripts to generate artificial load on the CPU. Stress is a simple workload generator for Linux systems that can be installed using the commands sudo apt update and sudo apt install stress. To generate CPU load using stress, run the command stress --cpu 4 --timeout 60, adjusting the number of CPU cores to stress based on your instance's available cores. Stress-ng is a more advanced and flexible tool that can be installed using the same commands as stress, and can be used to stress all CPUs with the command stress-ng --cpu 4 --cpu-load 80 --timeout 60s. A custom script can also be used to load the CPU, such as a bash script that runs an infinite loop, and can be executed in the background to create a full load on a single core. The yes command can also be used to generate CPU load by running it multiple times in the background. To monitor CPU usage during the test, use the top or htop commands, which can be installed with sudo apt install htop. After testing, terminate the stress processes manually using killall or pkill commands, except for stress and stress-ng which stop automatically after the timeout. It's essential to clean up after testing to avoid unnecessary CPU usage.
favicon
dev.to
dev.to
Create attached notes ...