liveBook · Manning

Resource utilization

As explained in Starting Data Analytics with Generative AI and Python

Resource utilization is the amount or proportion of available computing resources consumed by a system or task.

Resource utilization measures how much of a system’s available computing capacity a program consumes. It complements execution time and throughput by showing whether performance is limited by CPU, memory, storage, or other system resources. section 7.1 section 7.1.3

Use resource measurements to distinguish the kind of limitation affecting a task:

  • CPU-bound tasks spend most of their capacity on processing; faster CPUs or parallel execution may improve them. section 7.1.3
  • I/O-bound tasks spend most of their time waiting for storage, network, or other input/output operations; faster storage, optimized I/O, or asynchronous I/O may help. section 7.1.3
  • Memory and storage observations can reveal that a task is approaching system limits, creating bottlenecks or resource contention. section 7.1.3

Checking a computationally intensive analysis

Bootstrap confidence intervals repeatedly draw samples and recalculate statistics, making them useful candidates for measurement. Record CPU activity, disk I/O, and memory usage while the function runs, then compare the readings with execution time to identify the limiting resource. section 7.1 section 7.1.3

def monitor_cpu_io_memory(duration, interval):
    cpu_percentages = []
    io_counters = []
    memory_usages = []

    end_time = time.time() + duration
    while time.time() < end_time:
        cpu_percentages.append(psutil.cpu_percent(interval=interval))
        io_counters.append(psutil.disk_io_counters())
        memory_usages.append(psutil.virtual_memory().used)
        time.sleep(interval)

    return cpu_percentages, io_counters, memory_usages

# Monitor CPU, I/O activity, and memory usage for 10 seconds with 
1-second intervals
cpu_percentages, io_counters, memory_usages = 
monitor_cpu_io_memory(duration=10, interval=1)

# Capture memory usage before running your_function()
memory_before = psutil.virtual_memory().used

def your_function():
    # Your code here
    pass

# Run your function
your_function()

# Capture memory usage after running your_function()
memory_after = psutil.virtual_memory().used

# Calculate function's memory usage
function_memory_usage = memory_after - memory_before

# Analyze CPU usage
avg_cpu_usage = sum(cpu_percentages) / len(cpu_percentages)
print(f"Average CPU usage: {avg_cpu_usage}%")

# Analyze I/O activity
read_bytes = [c.read_bytes for c in io_counters]
write_bytes = [c.write_bytes for c in io_counters]
print(f"Total read bytes: {sum(read_bytes)}")
print(f"Total write bytes: {sum(write_bytes)}")

# Analyze memory usage
avg_memory_usage = sum(memory_usages) / len(memory_usages) - memory_before
print(f"Average memory usage during function execution: 
{avg_memory_usage / (1024 * 1024):.2f} MB")
print(f"Function memory usage: 
{function_memory_usage / (1024 * 1024):.2f} MB")

A monitoring workflow samples CPU percentages, disk I/O counters, and virtual memory during a fixed interval, then compares memory before and after the target function executes. section 7.1.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines CPU-bound and I/O-bound behavior and gives monitoring guidance, but stops short of comprehensive systems monitoring.
Examples
Uses bootstrap confidence intervals, CPU percentages, disk I/O counters, memory readings, and a monitored function.
What it assumes
Assumes basic awareness of program execution and system resources while teaching bottleneck categories from practical measurements.
What you can do after
Diagnose whether a function is CPU-bound or I/O-bound and estimate its memory usage through sampled measurements.
How it connects
Connects resource utilization with execution time, throughput, memory usage, CPU-bound tasks, I/O-bound tasks, and performance bottlenecks.

Also explained in

Compare how 8 books explain resource utilization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.