PyScope

A lightweight Python performance profiling tool that captures execution time, CPU usage, memory consumption, function-level hotspots, and detects performance regressions across multiple runs โ€” with zero code modification.

What is PyScope?

PyScope is a developer-oriented performance analysis tool designed to help identify bottlenecks in Python programs. Unlike traditional profilers that focus only on a single execution, PyScope tracks performance across multiple runs and detects regressions automatically.

PyScope is built as an end-to-end profiling system: data collection, aggregation, regression analysis, and report generation.

Key Features

Quick Start

Profile any Python script in one command:

python main.py your_script.py

PyScope automatically generates:

Sample Output

PyScope generates multiple forms of output to support both developer workflows and performance analysis over time. Below is a real example captured from profiling a slow Python script.

CLI Output

A concise command-line summary highlighting execution time, resource usage, hotspots, optimization hints, and regression warnings.

PyScope Performance Report
----------------------------------------
Execution Time : 1.8917 seconds
Average CPU    : 40.91 %
Peak Memory   : 19.15 MB

Top Hotspots
----------------------------------------
examples/slow_script.py:slow
  Calls      : 1
  Total Time : 1.8782 seconds

Optimization Suggestions
----------------------------------------
โ€ข Function 'examples/slow_script.py:slow' dominates runtime (99%).
Consider optimizing its algorithm or reducing repeated work.

JSON report saved to : reports/json/pyscope_report_2025-12-16T12-26-04.603376.json
HTML report saved to : reports/html/pyscope_report_2025-12-16T12-26-04.603376.html

Performance Regression Check
----------------------------------------
โš ๏ธ Execution time increased from 1.5524s โ†’ 1.9941s (+28.4541%)
โš ๏ธ Peak memory increased from 18.844MB โ†’ 19.2539MB (+2.1766%)
โš ๏ธ Top hotspot 'examples/slow_script.py:<module>' increased from 1.4750s โ†’ 1.9084s

JSON Report

A structured machine-readable report suitable for automation, dashboards, historical analysis, and research experiments.

{
    "timestamp": "2025-12-16T12:26:04.603376",
    "script": "examples/slow_script.py",
    "execution_time": 1.8917008000425994,
    "avg_cpu_percent": 40.90555555555555,
    "peak_memory_mb": 19.15234375,
    "hotspots": [
        {
            "function": "examples/slow_script.py:slow",
            "calls": 1,
            "total_time": 1.878245399799198
        }
    ],
    "suggestions": [
        "Function 'examples/slow_script.py:slow' dominates runtime (99%).\nConsider optimizing its algorithm or reducing repeated work."
    ],
    "regression": {
        "status": "regression",
        "messages": [
            {
                "level": "warning",
                "text": "Execution time increased from 1.5524s โ†’ 1.9941s (+28.4541%)"
            },
            {
                "level": "warning",
                "text": "Peak memory increased from 18.844MB โ†’ 19.2539MB (+2.1766%)"
            },
            {
                "level": "warning",
                "text": "Top hotspot 'examples/slow_script.py:' increased from 1.4750s โ†’ 1.9084s"
            }
        ]
    }
}

HTML Report

An interactive, human-readable HTML report that visualizes performance metrics, hotspots, optimization suggestions, and regression alerts in a single view.

PyScope HTML Performance Report

Architecture Overview

Target Script โ”‚ โ–ผ Runner (runner.py) โ”‚ โ”œโ”€โ”€ ExecutionTimer โ†’ wall-clock execution time โ”œโ”€โ”€ ProcessProfiler โ†’ CPU & memory sampling โ”œโ”€โ”€ HotspotProfiler โ†’ function-level hotspots โ”‚ โ–ผ ProfilingReport โ”‚ โ”œโ”€โ”€ JSON Report (machine-readable) โ”œโ”€โ”€ HTML Report (human-readable) โ”‚ โ”œโ”€โ”€ OptimizationEngine โ†’ rule-based suggestions โ””โ”€โ”€ MultiRunAnalyzer โ†’ regression detection
The architecture emphasizes separation of concerns, extensibility, and low runtime overhead.

Folder Structure

PyScope/ โ”œโ”€โ”€ main.py # CLI entry point โ”œโ”€โ”€ pyscope/ โ”‚ โ”œโ”€โ”€ runner.py # Orchestrates profiling lifecycle โ”‚ โ”œโ”€โ”€ timer.py # Wall-clock timing โ”‚ โ”œโ”€โ”€ profiler.py # CPU & memory sampling (psutil) โ”‚ โ”œโ”€โ”€ hotspots.py # Function-level execution tracking โ”‚ โ”œโ”€โ”€ optimizer.py # Optimization suggestion engine โ”‚ โ”œโ”€โ”€ report.py # JSON report generation โ”‚ โ”œโ”€โ”€ html_report.py # HTML report rendering โ”‚ โ””โ”€โ”€ multi_run.py # Historical regression analysis โ”œโ”€โ”€ examples/ โ”‚ โ””โ”€โ”€ slow_script.py # Sample workload โ””โ”€โ”€ reports/ โ”œโ”€โ”€ json/ โ””โ”€โ”€ html/

๐Ÿ”„ Multi-Run Regression Detection

Each run produces a timestamped JSON report

Script-aware matching

Latest-vs-Previous comparison

Noise-aware

โš™๏ธ Configuration

Adjust regression sensitivity in pyscope/multi_run.py:

def compare_latest(self, script_name=None, threshold=0.001):
    # Lower threshold โ†’ more sensitive detection
    # Higher threshold โ†’ more conservative detection
    

Why PyScope?

๐Ÿ›ฃ๏ธ Future Extensions

๐ŸŽ“ Academic & Research Relevance

PyScope demonstrates: