Tuesday, May 7, 2024

How to Simulate High CPU and RAM Load on CentOS for Performance Testing

How to Simulate High CPU and RAM Load on CentOS for Performance Testing


ncreasing CPU and RAM utilization on a CentOS system programmatically can be useful for testing purposes, such as evaluating the performance under different load conditions. Here, I'll provide you with a couple of ways to simulate high CPU and RAM usage using shell commands.

Increasing CPU Usage

To increase CPU usage, you can use the stress tool, which is designed to impose certain types of compute stress on your system.

Install the stress tool:

sudo yum install epel-release
sudo yum install stress

Simulate high CPU load: You can start a number of workers that will fully load a number of CPUs.

stress --cpu 8 --timeout 300

This command will start 8 workers that stress the CPU for 300 seconds.

Increasing RAM Usage

To increase RAM usage, you can use a simple shell script that allocates a certain amount of memory and holds it for a period of time.

Create a bash script to increase memory usage:

  1. Here’s a simple script that allocates memory in increments.

#!/bin/bash # Usage: ./increase_memory.sh <memory_in_mb> <duration_in_seconds> if [ $# -ne 2 ]; then echo "Usage: $0 <Memory in MB> <Duration in seconds>" exit 1 fi echo "Allocating $1 MB of memory for $2 seconds." /usr/bin/perl -e "\$|=1; \$a='a' x ($1*1024*1024); sleep $2;"

Save this script as increase_memory.sh and make it executable:

chmod +x increase_memory.sh

Run the script:

./increase_memory.sh 1024 300

This command will allocate 1024 MB of RAM for 300 seconds.

Automating and Looping the Stress Tests

If you want to continuously or repeatedly apply these loads in a loop for testing over a longer period, you can wrap these commands in a shell loop.

#!/bin/bash while true; do echo "Starting CPU and Memory stress test" stress --cpu 4 --io 2 --vm 2 --vm-bytes 128M --timeout 600 & ./increase_memory.sh 1024 600 sleep 60 # Rest for 60 seconds before repeating the test done

This script runs both CPU and memory stress tests in parallel, waits for 60 seconds after the stress period, and then repeats the tests. Adjust the parameters according to your testing requirements.

Important Notes

  • Monitoring: While running these tests, keep an eye on system monitoring tools to ensure that you are not causing damage or excessive wear on hardware components, especially in production environments.
  • Permissions: Ensure that you have the necessary permissions to install and run these tools on your system.
  • Resource Allocation: Be cautious with the amount of stress you apply, especially on production systems, as this could lead to system crashes or slowdowns for other processes.

These tools and scripts should help you simulate high CPU and RAM usage on a CentOS system, which can be crucial for performance tuning, capacity planning, and reliability testing.



No comments:

Post a Comment