While testing out some home automation code on my Raspberry Pi I noticed it was pretty CPU intensive. Time to bump up the overclock to squeeze more performance out of the Broadcom Arm7 processor. I wanted to keep an eye on temps as well as stability under full load so I needed to simulate CPU usage.
This small Python script will do the job. It uses the multiprocessing library which you can read more about here.
#!/usr/bin/env python """ Produces load on all available CPU cores Updated with suggestion to prevent Zombie processes Linted for Python 3 Source: insaner @ https://danielflannery.ie/simulate-cpu-load-with-python/#comment-34130 """ from multiprocessing import Pool from multiprocessing import cpu_count import signal stop_loop = 0 def exit_chld(x, y): global stop_loop stop_loop = 1 def f(x): global stop_loop while not stop_loop: x*x signal.signal(signal.SIGINT, exit_chld) if __name__ == '__main__': processes = cpu_count() print('-' * 20) print('Running load on CPU(s)') print('Utilizing %d cores' % processes) print('-' * 20) pool = Pool(processes) pool.map(f, range(processes))
This will utilize each CPU core and produce ~100% load with the while loops calculation.
-------------------- Running load on CPU(s) Utilizing 8 cores --------------------
June 11, 2020 at 10:46 am
Could we calibrate the load so as to stress only a certain percent of the CPU?
August 21, 2019 at 8:22 am
Actually, I went ahead and turned this into a gtk3 gui script (tested only on Linux, though):
https://github.com/insaner/cpu_stress_cores.py
September 11, 2019 at 8:17 pm
Hi insaner,
Nice job! Thanks for the credit. Your comment about the Zombie processes was also correct. I’ve updated the original post with your suggestions.
July 22, 2019 at 4:55 pm
I have noticed you don’t monetize danielflannery.ie, don’t waste your traffic,
you can earn extra bucks every month with new monetization method.
This is the best adsense alternative for any type of website (they approve all websites), for more info
simply search in gooogle: murgrabia’s tools
May 15, 2019 at 6:09 am
This creates a bunch of zombie processes. To avoid that:
#!/usr/bin/env python
“””
Produces load on all available CPU cores
# https://danielflannery.ie/simulate-cpu-load-with-python/
“””
from multiprocessing import Pool
from multiprocessing import cpu_count
import signal
stop_loop = 0
def exit_chld(x,y):
global stop_loop
stop_loop = 1
def f(x):
global stop_loop
while not stop_loop:
x*x
signal.signal(signal.SIGINT, exit_chld)
if __name__ == ‘__main__’:
processes = cpu_count()
print ‘-‘ * 20
print ‘Running load on CPU’
print ‘Utilizing %d cores’ % processes
print ‘-‘ * 20
pool = Pool(processes)
pool.map(f, range(processes))