Python Forum
How to produce a CPU load for several seconds, and measure it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to produce a CPU load for several seconds, and measure it?
#1
I am trying to create a small program that will put a heavy load on the CPU, and display the load over time.

The code below should start 10 processes. Each process doing a fair load for a while. The processes will be terminated after 10 seconds. It will display the CPU load every quarter second.

I am finding that the load does start around 100%, but only stays there for about a second, then drops to around 25%. I would expect the load to stay high for the full 10 seconds.

Is there a reason the load doesn't stay near 100%? Is there a way to keep the load high?

I'm currently running this on a Windows 10 PC with Python 3.12. It will be also run on a Linux system as well.

import time
import psutil
import multiprocessing

process_count = 10
run_time = 10  # seconds

def do_work(x):
    # Simulate a CPU-intensive task
    total = 0
    for i in range(10**9):
        total += i * i
    return total

if __name__ == '__main__':
    print(f"CPU load running for {run_time} seconds...")

    args = [0 for _ in range(number_of_processes)]
    pool = multiprocessing.Pool(processes=process_count)
    processes = [pool.apply_async(do_work, args)]

    # Determine when the processes should stop
    start_time = time.time()
    end_time = start_time + run_time

    # Allow processes to run for the amount of time specified
    count = 1
    sum = 0
    while time.time() < end_time:
        # Determine CPU load for .25 seconds
        cpu = psutil.cpu_percent(interval=.25)
        sum += cpu
        print(f"{count} CPU Load: {cpu:.2f}%")
        count += 1
                
    # Kill the pool of processes
    pool.terminate()

    avg = sum / count
    print(f"CPU load completed. Average CPU usage: {avg:.2f}%")
Reply
#2
That 100% processor load is starting the processes. You create a processing pool, but don't give the processes anything to do. Only one process is running because you only create 1 task to perform. Create more tasks and the processor utilization will climb.
import psutil
import multiprocessing


def do_work():
    # Simulate a CPU-intensive task
    total = 0
    for i in range(10**9):
        total += i * i
    return total


if __name__ == "__main__":
    pool = multiprocessing.Pool(processes=8)
    results = [pool.apply_async(do_work, ()) for _ in range(20)]
    cpu = [psutil.cpu_percent(interval=0.1) for _ in range(100)]  # Collect CPU utilization 10 times a second for 10 seconds.
    pool.terminate()

    print(cpu, sum(cpu) / len(cpu))
Output:
[100.0, 100.0, 100.0, 89.6, 87.7, 90.7, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0] 99.68
Reply
#3
Thanks for the clarification, deanhystad. I'm just joining this thread, and your explanation makes sense. I was wondering why the CPU load would drop so quickly, and it didn’t occur to me that only one task was being run in the original code. By creating multiple tasks with apply, async, it ensures that all processes have work to do and can sustain the load. I also appreciate the example showing how to measure CPU usage in parallel, very helpful for testing across systems.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  measure diameter of complex structure alminnawi 0 1,298 Apr-18-2024, 10:20 AM
Last Post: alminnawi
  Produce One file Per PurchaseOrder jland47 1 1,510 Jan-26-2024, 11:38 AM
Last Post: Larz60+
  Problem with module time and leap seconds Pedroski55 3 2,764 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Using multiprocessing to produce objects for i in range lucasrohr 6 3,685 Feb-02-2022, 03:53 PM
Last Post: lucasrohr
  Store variable data and display sum after 60 seconds the_dude 11 7,290 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  How to measure execution time of a multithread loop spacedog 2 4,817 Apr-24-2021, 07:52 AM
Last Post: spacedog
  How to calculate time difference between each row of dataframe in seconds Mekala 1 4,283 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Need to add hours min and seconds tester_V 5 5,263 Jun-02-2020, 05:29 PM
Last Post: tester_V
  Can't substract seconds from time Raj_Kumar 1 2,891 Apr-15-2020, 02:47 AM
Last Post: bowlofred
  How to calculate time in seconds rajeshE 1 3,246 Feb-15-2020, 11:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020