How to Generate Controlled CPU Load on Linux for Performance Testing


22 views

When testing system performance or monitoring tools, you often need to simulate CPU load without affecting other system resources. This is particularly useful for:

  • Testing system monitoring tools
  • Evaluating cooling system performance
  • Benchmarking CPU throttling behavior
  • Stress testing server stability

The most straightforward approach is using the stress command, which is available in most Linux distributions:

sudo apt-get install stress  # For Debian/Ubuntu
sudo yum install stress      # For CentOS/RHEL

To generate CPU load for 10 minutes (600 seconds) using 4 cores:

stress --cpu 4 --timeout 600

For more control over the load generation, you can use this bash script:

#!/bin/bash
duration=600    # 10 minutes in seconds
end_time=$((SECONDS + duration))

while [ $SECONDS -lt $end_time ]; do
    # This creates CPU load without disk I/O
    dd if=/dev/zero of=/dev/null bs=1M count=10000 &> /dev/null
done

For simple single-core load testing:

timeout 600 yes > /dev/null

For more precise control over CPU utilization percentage, use this Python script:

import time
import multiprocessing
import math

def cpu_load(utilization, duration):
    end_time = time.time() + duration
    while time.time() < end_time:
        start_time = time.time()
        while time.time() - start_time < utilization/100.0:
            math.factorial(100)  # CPU-intensive operation
        time.sleep((1 - utilization/100.0))

if __name__ == "__main__":
    processes = []
    for _ in range(multiprocessing.cpu_count()):
        p = multiprocessing.Process(target=cpu_load, args=(75, 600))
        p.start()
        processes.append(p)
    
    for p in processes:
        p.join()

While running these tests, monitor your CPU usage with:

watch -n 1 'grep \"^[c]pu \" /proc/stat'

Or for a more visual representation:

htop
  • Always run tests in a controlled environment
  • Monitor system temperatures to prevent overheating
  • Be cautious when running on production systems
  • Consider using cgroups to limit resource usage

When testing system performance or simulating production workloads, generating controlled CPU load is essential. Unlike general stress testing tools that create system-wide load, we often need pure CPU-bound operations for precise benchmarking.

The most straightforward method uses the stress-ng utility, which offers fine-grained control:


# Install stress-ng (Ubuntu/Debian)
sudo apt install stress-ng

# Generate load on 4 cores for 10 minutes
stress-ng --cpu 4 --timeout 10m --metrics-brief

# Generate 50% load on all cores
stress-ng --cpu $(nproc) --cpu-load 50 --timeout 10m

For systems without stress-ng, we can use basic shell commands:

1. Infinite Loop Method:


# Single core load for 10 minutes (600 seconds)
timeout 600 bash -c 'while true; do :; done' &

2. Mathematical Calculation:


# Multi-core load using bc calculator
for i in $(seq 1 $(nproc)); do
    timeout 600 bash -c 'while true; do echo "scale=5000; 4*a(1)" | bc -l >/dev/null; done' &
done

For more sophisticated testing:

1. Custom C Program:


#include 
#include 

int main() {
    clock_t start = clock();
    while((clock() - start) < 600 * CLOCKS_PER_SEC) {
        // Busy wait
    }
    return 0;
}

Compile with: gcc -o cpuload cpuload.c

2. Dockerized Load Generator:


docker run -it --rm alpine sh -c "apk add stress-ng && stress-ng --cpu $(nproc) --timeout 10m"

Always monitor system metrics during testing:


watch -n 1 "grep 'MHz' /proc/cpuinfo; uptime; free -h"
  • Always run tests as non-root when possible
  • Monitor temperature thresholds
  • Consider CPU affinity when targeting specific cores
  • Clean up processes after testing