zprofile: Statistical CPU and Wall-Clock Profilers for Python

1.0.13 · maintenance · verified Thu Apr 16

zprofile provides statistical (sampling) CPU and wall-clock profilers for Python applications. Derived from the `google-cloud-profiler`, it allows users to collect profiling data with minimal overhead, making it suitable for production environments. The library outputs data in the `pprof` format, which can then be visualized using the external `go tool pprof`. The current version is 1.0.13, last released in August 2023, indicating a maintenance-oriented release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `CPUProfiler` to collect CPU usage statistics over a 10-second period for a simulated busy-wait function. The collected profile data is saved to a file named `profile.pprof`, which can then be visualized using the `go tool pprof` utility.

import time
from zprofile.cpu_profiler import CPUProfiler

def busy_wait(duration):
    end_time = time.time() + duration
    while time.time() < end_time:
        pass

def main_function():
    print("Starting CPU intensive task...")
    busy_wait(2) # Simulate CPU work
    print("CPU task complete.")

p = CPUProfiler()
profile_data = p.profile(10) # Profile for 10 seconds

with open("profile.pprof", "wb") as f:
    f.write(profile_data)

print("CPU profile data written to profile.pprof")
print("Use 'go tool pprof -http=:8080 profile.pprof' to view the profile.")

view raw JSON →