STEM: Using Python to Simulate CPU and Memory Load for Performance Testing
Introduction
Computer performance testing is essential for diagnosing system behavior under high-stress conditions. Whether you're testing hardware stability, benchmarking a system, or evaluating cooling solutions, simulating high CPU and memory loads can provide valuable insights.
This article explores a Python script that allows users to simulate CPU and memory stress using the multiprocessing and threading libraries. This simple but effective tool enables users to test system endurance and performance by generating controlled workloads.
Understanding the Code
The provided Python script lets users choose between two types of stress tests:
The script achieves this through parallel execution using multiprocessing (for CPU) and threading (for memory), allowing users to tailor the test parameters.
How the CPU Load Simulation Works
The function cpu_load(duration) runs a simple mathematical operation in a loop to keep the CPU engaged for a set duration. When users select CPU stress testing, the script prompts them to specify:
The script then launches multiple processes, each running cpu_load(), ensuring that multiple CPU cores are stressed simultaneously.
Use Case:
Recommended by LinkedIn
How the Memory Load Simulation Works
The function memory_load(size_mb, duration) allocates a list of byte arrays in memory, effectively reserving RAM space. The allocated memory remains occupied until the specified duration ends, at which point it is released.
Users specify:
This is executed as a separate thread, ensuring minimal CPU overhead while stressing only the RAM.
Use Case:
Feel free to copy this code and modify. Add more tests like read write tests and report the results or simply use as is. Code has been tested on Windows 11.
The code:
import threading
import time
def cpu_load(duration):
# Function to simulate CPU load for a given duration
end_time = time.time() + duration
while time.time() < end_time:
_ = 10**5 # Perform a simple calculation to keep the CPU busy
def memory_load(size_mb, duration):
# Function to allocate memory and hold it for a given duration
memory = []
for _ in range(size_mb):
memory.append(bytearray(1024 * 1024)) # Allocate 1 MB at a time
time.sleep(duration)
del memory # Free up memory after the duration
if __name__ == "__main__":
print("Enter the type of load to simulate: ")
print("1. CPU load")
print("2. Memory load")
choice = int(input("Your choice: "))
if choice == 1:
cpu_cores = int(input("Enter the number of CPU cores to stress: "))
duration = int(input("Enter the duration of the test (in seconds): "))
processes = []
for _ in range(cpu_cores):
p = multiprocessing.Process(target=cpu_load, args=(duration,))
processes.append(p)
p.start()
for p in processes:
p.join()
elif choice == 2:
size_mb = int(input("Enter the size of memory to allocate (in MB): "))
duration = int(input("Enter the duration of the test (in seconds): "))
memory_thread = threading.Thread(target=memory_load, args=(size_mb, duration))
memory_thread.start()
memory_thread.join()
else:
print("Invalid choice!")
Demo video: