Skip to main content

A lightweight Python profiler for tracing CPU, memory, I/O, GC, and system metrics with storage and CLI support.

Project description

PerfTrace 🔍

PyPI version Python versions License Stars

PerfTrace is a unified performance tracing and profiling CLI for Python applications.

It provides detailed insights into function execution, context/module performance, CPU and memory usage, and system metrics, with rich statistical summaries and export support — all through a clean, production-ready command-line interface.

PerfTrace is developer-centric, explicit, and lightweight.
It focuses on performance analysis, not error or exception tracking.


✨ Key Features

  • 🔍 Function & context-level profiling
  • 📊 Statistical metrics (min / max / avg / p90 / p95 / p99 / std dev)
  • 🕒 Recent, daily, and historical analysis
  • 🐢 Slowest / fastest execution detection
  • 🧠 System & memory monitoring
  • 📁 Export to CSV & JSON
  • 🩺 Health diagnostics (doctor)
  • ⚙️ Configurable storage backends (DuckDB & PostgreSQL)
  • 🧩 Function, class, and context-manager based instrumentation

📦 Installation

pip install perftrace

Requirements

  • Python 3.11+

🚀 Getting Started

perftrace help

Recommended first commands:

perftrace summary
perftrace doctor
perftrace stats-function <FUNCTION_NAME>

🧠 How PerfTrace Works

PerfTrace works in two clear phases:

  1. Instrumentation
    Developers explicitly mark functions, classes, or code blocks using decorators or context managers.

  2. Analysis
    The CLI queries stored metrics and generates summaries, statistics, and exports.

PerfTrace runs only when your code runs — there are no background agents, daemons, or always-on processes.


🧩 Instrumenting Your Code

🎛 Selecting Metrics to Collect

PerfTrace allows you to explicitly control which performance metrics are collected.

You can either:

  • provide a list of specific metrics, or
  • use "all" to collect every supported metric

Supported Metrics

Metric Description
cpu CPU usage and CPU time
memory Memory usage and deltas
execution Execution time
file File I/O activity
garbagecollector Garbage collection activity
ThreadContext Thread and execution context
network Network activity

Using Specific Metrics

from perftrace import perf_trace_metrics

@perf_trace_metrics(profilers=["cpu", "memory", "execution"])
def compute():
    return [i for i in range(100_000)]

Using All Metrics

@perf_trace_metrics(profilers="all")
def full_trace():
    return [i for i in range(100_000)]

Class-Level Profiling

from perftrace import perf_trace_metrics_cl

@perf_trace_metrics_cl(profilers=["cpu", "execution"])
class MyProcessor:
    @staticmethod
    def step1(x):
        return x + 1

    def step2(self, y):
        return y * 2

Context-Based Profiling

from perftrace import PerfTraceContextManager

with PerfTraceContextManager(
    context_tag="work",
    cls_collectors=["cpu", "memory"]
):
    work = [x ** 2 for x in range(100_000)]

📖 Complete CLI Reference

General Commands

Command Description
version Show PerfTrace version
help Display help
doctor Run health checks
summary Overall performance summary
list List available functions and contexts

Function Commands

Command Description
show-function <name> Detailed trace data
stats-function <name> Statistical metrics
recent-function Recently executed functions
search-function <name> Search execution history
count-function Execution frequency
slowest Slowest execution
fastest Fastest execution

Context Commands

Command Description
show-context <name> Context trace data
stats-context <name> Statistical metrics
recent-context Recently executed contexts
search-context <name> Search history
count-context Execution frequency

Time-Based

Command Description
today Executions today
history Historical data

System & Memory

Command Description
system-status System status
system-info System information
system-monitor Real-time monitoring
memory Memory usage

Export

Command Description
export-csv Export database to CSV
export-json Export database to JSON
export-function-csv Export function CSV
export-context-csv Export context CSV
export-function-json Export function JSON
export-context-json Export context JSON

⚙️ Configuration (YAML)

Config file locations:

  • Linux / macOS: ~/.perftrace/config.yaml
  • Windows: %USERPROFILE%\.perftrace\config.yaml

DuckDB (Default)

database:
  engine: duckdb
  duckdb:
    path: ./data/default.duckdb

PostgreSQL (Optional)

database:
  engine: postgresql
  postgresql:
    host: localhost
    port: 5432
    user: postgres
    password: your_password

Interactive setup:

perftrace set-config

Verify:

perftrace doctor

🔍 Positioning (PerfTrace vs APM Tools)

PerfTrace complements APM tools by providing:

  • precise function-level metrics
  • lightweight, on-demand profiling
  • local and CI-friendly analysis

PerfTrace is not a distributed tracing or error-tracking system.


📄 License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

perftrace-1.0.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

perftrace-1.0.0-py3-none-any.whl (34.0 kB view details)

Uploaded Python 3

File details

Details for the file perftrace-1.0.0.tar.gz.

File metadata

  • Download URL: perftrace-1.0.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for perftrace-1.0.0.tar.gz
Algorithm Hash digest
SHA256 961f4a2006fc7b4d3bfd2c9a404940330876925ff16c23848ccdc014d9e24b52
MD5 fba7b1dd837822de25db9c6c4e3b2242
BLAKE2b-256 0cec4d0a7e2379ebd4403291b47044f22132afb996e4f85c8ffe1a38423d0957

See more details on using hashes here.

File details

Details for the file perftrace-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: perftrace-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 34.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for perftrace-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d47a167e7ceb9ca77975a9ba8f3c023c241fa641dd3df0499fe2cdc2bfd45118
MD5 0d92ff536f625718089278177a8bb95c
BLAKE2b-256 56c5209b62ee1ecef72f1857205f09816eae3b846becedf9dc493f694efa8790

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page