CPU Load Generator Project
Welcome to the CPU Load Generator project — a Python-based tool that generates a fixed CPU load for a finite or indefinite period on one or more CPU cores, using a PI controller (proportional–integral).

Motivation
This project is ideal for:
- Performance Testing
- Resource Allocation Optimization
- Benchmarking
- Education & Training
- Thermal & Power Analysis
Architecture
The system consists of:
- CPU Target Load
- Monitor Thread for actual CPU usage measurement
- Controller Thread (PI regulator)
- Actuator adjusting sleep time for control
- Disturbances (e.g., other processes)
Monitor Thread
Uses psutil to sample the current process CPU usage (per core when affinity is set):
p = psutil.Process(os.getpid())
sample = p.cpu_percent(sampling_interval)
The call blocks for the sampling interval, ensuring timing consistency.
Controller Thread (PI Regulator)
Calculates a control signal (sleep time) to modulate load. If the error is positive (load too low), it reduces sleep to increase load, and vice versa. Tuned experimentally with kp and ki.
Actuator
The actuator applies the controller output by alternating CPU-bound work and sleep. Less sleep ⇒ higher load.
def generate_load(self, sleep_time):
interval = time.time() + self.period - sleep_time
while time.time() < interval:
pr = 213123
_ = pr * pr
pr += 1
time.sleep(sleep_time)
Results
The figure below shows the controller maintaining 50% CPU load.

Supported platforms: Linux, macOS, and Windows (see README for setup). Use --plot to show the load curve and save a PNG.
Resources
Thanks for checking out this project! Feel free to contribute or raise issues on the repository.