Below you can find python script to burn cpu (for expamle, when testing alerting) in virtual (and physical) environment. Script is scaling (automatically) for number of CPU presented for the OS
burn_cpu.py
import multiprocessing
import time
def burn_cpu(duration):
end_time = time.time() + duration
while time.time() < end_time:
pass
if __name__ == "__main__":
num_threads = multiprocessing.cpu_count() # Get the number of CPU cores
duration = 60 # Burn CPU for 60 seconds
print(f"Burning CPU with {num_threads} threads for {duration} seconds.")
processes = []
for _ in range(num_threads):
process = multiprocessing.Process(target=burn_cpu, args=(duration,))
processes.append(process)
process.start()
for process in processes:
process.join()
print("CPU burn completed.")
To run script use the following command: python3 burn_cpu.py
No Comments