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.
Key Features
- ๐ Function-level hotspot profiling using
sys.setprofile - ๐ง Aggregated execution time, CPU usage, and peak memory
- ๐ Multi-run performance regression detection
- ๐ Automatic JSON & HTML report generation
- โก Zero instrumentation โ profile any Python script instantly
- ๐งช Designed for benchmarking, optimization, and experimentation workflows
Quick Start
Profile any Python script in one command:
python main.py your_script.py
PyScope automatically generates:
- CLI performance summary
- JSON report (
reports/json/) - HTML report (
reports/html/) - Regression analysis across historical runs
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.
Architecture Overview
- HotspotProfiler: Captures function call/return timing
- Runner: Manages execution, CPU & memory sampling
- MultiRunAnalyzer: Detects regressions across runs
- Report Engine: Serializes results to JSON & HTML
Folder Structure
๐ Multi-Run Regression Detection
Each run produces a timestamped JSON report
- Stored in
reports/json/ - Includes execution time, memory, hotspots, script path
Script-aware matching
- Only compares runs of the same script
- Uses OS-normalized absolute paths to avoid Windows/Linux mismatches
Latest-vs-Previous comparison
- Execution time
- Peak memory usage
- Top hotspot cumulative time
- Threshold-based decision
- Default: 10% increase = regression
- Fully configurable
Noise-aware
- Small runtime fluctuations are ignored
- Prevents false positives caused by OS scheduling variance
โ๏ธ 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?
- Traditional profilers focus on single-run analysis
- PyScope treats performance as a time-series problem
- Built for experimentation, benchmarking, and optimization feedback loops
๐ฃ๏ธ Future Extensions
- Statistical confidence intervals over multiple baseline runs
- CI/CD integration (fail builds on regression)
- Flamegraph visualization
- ML-assisted optimization recommendations
- Language-agnostic profiling backend
๐ Academic & Research Relevance
PyScope demonstrates:
- Systems-level performance engineering
- Runtime instrumentation
- Historical performance analysis
- Noise-aware regression detection
- Clean, extensible software architecture