AI-powered Python profiling tool with Claude AI insights and intelligent optimization recommendations
Project description
AI Profiler CLI - Production Edition
Professional-grade AI-powered Python profiling tool with Claude AI insights and intelligent optimization recommendations.
โจ Features
- ๐ Industrial-Strength Profiling - CPU and memory profiling with Scalene (5-10% overhead)
- ๐ค AI-Powered Analysis - Get actionable optimization suggestions from Claude AI
- ๐ Detailed Metrics - Per-function CPU breakdown, memory hotspots, and call counts
- ๐จ Beautiful Terminal Output - Rich formatting with tables, colors, and progress indicators
- โก Zero Configuration - Works out of the box with sensible defaults
- ๐ Web Integration - Upload results to the web platform for sharing and team collaboration
Quick Start
Installation
cd aiprofile-cli
# Install Poetry if needed
curl -sSL https://install.python-poetry.org | python3 -
# Install dependencies
poetry install
Setup API Key
export ANTHROPIC_API_KEY="your-api-key-here"
Run
# Profile a script
poetry run aiprofile run your_script.py
# With arguments
poetry run aiprofile run script.py --input data.csv
# Skip AI analysis (no API key needed)
poetry run aiprofile run script.py --no-analysis
Example
# Try with included examples
poetry run aiprofile run examples/simple_script.py
poetry run aiprofile run examples/slow_script.py
Project Structure
aiprofile-cli/
โโโ packages/cli/src/cli/
โ โโโ profilers/ # Profiler implementations (Scalene)
โ โโโ runner/ # Script execution (Python runner)
โ โโโ ai/ # Claude AI analyzer
โ โโโ formatter/ # Rich terminal output
โ โโโ models/ # Data models (generic + Python-specific)
โ โโโ core/ # Core utilities (detector, logger, errors)
โ โโโ commands.py # CLI commands
โ โโโ main.py # Typer CLI entry point
โโโ examples/python/ # Example Python scripts
โโโ tests/ # Test suite
โโโ pyproject.toml # Poetry configuration
โโโ pytest.ini # Pytest configuration
โโโ README.md # This file
Architecture Notes
The codebase is designed for extensibility:
- Generic Base Classes:
Profiler,ScriptRunner, data models support multiple languages - Language Detection: Automatic detection + override support in
core/detector.py - Future Support: Adding a new language requires:
- New profiler in
profilers/<language>/ - New runner in
runner/<language>_runner.py - Updates to
detector.py - No changes to CLI or AI analysis logic needed
- New profiler in
Development
Run Tests
poetry run pytest
# or
./scripts/test.sh
Code Quality
poetry run black .
poetry run ruff check .
poetry run mypy packages/*/src
# or
./scripts/lint.sh
Format Code
./scripts/format.sh
Usage
Basic Commands
# Get help
poetry run aiprofile --help
# Show version
poetry run aiprofile version
# Profile with custom duration
poetry run aiprofile run script.py --duration 60
Activate Poetry Shell
poetry shell
aiprofile run script.py # No need for 'poetry run' prefix
How It Works
- Profile - Runs Scalene on your Python script to collect CPU and memory metrics
- Analyze (optional) - Sends profiling data to Claude for AI-powered analysis and recommendations
- Display - Shows beautiful terminal output with tables, metrics, and actionable suggestions
- Upload (optional) - Share results on the web platform for team collaboration
Sampling Rates & Accuracy
Uses carefully tuned sampling rates for production use [[memory:10987753]]:
- CPU: 1000 Hz (1ms sampling) for millisecond-level function breakdown
- Memory: 100 Hz (10ms sampling) for allocation tracking
- Overhead: ~5-10% - efficient enough for CI/CD pipelines
Requirements
- Python 3.9+ (tested on 3.9, 3.10, 3.11, 3.12)
- Poetry (for dependency management)
- Anthropic API key (optional, only needed for AI analysis feature)
Example Output
๐ AI Profiler
โโโโโโโโโโโโโโโโโโโโ
CPU Profile Results
Duration: 10.5s | Samples: 1050
โญโโโโโโโโโโโโโโโ Top Functions by CPU Time โโโโโโโโโโโโโโโโโฎ
โ Rank โ Function โ CPU % โ Time (s) โ
โโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโค
โ 1 โ cpu_intensive_loop โ 45.23% โ 4.75 โ
โ 2 โ nested_loops โ 28.45% โ 2.99 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
๐ค AI Analysis - Claude Insights
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฅ Bottleneck #1: cpu_intensive_loop
CPU: 45.2%
Optimization Suggestions:
1. Use list comprehension instead of loop
Improvement: 30-40% faster | Difficulty: Low
Use Cases
- Performance Optimization - Identify bottlenecks in production code
- Debugging - Find memory leaks and unexpected CPU usage patterns
- Code Review - Share profiling data with your team
- Benchmarking - Compare performance before and after changes
- Education - Understand how your code executes at runtime
- CI/CD Integration - Profile tests and builds automatically
Production Status
โ Production-Ready (v1.0.0)
- Thoroughly tested on Python 3.9-3.12
- Uses industry-standard Scalene profiler
- Minimal overhead (~5-10% CPU)
- Comprehensive error handling
- Type-safe with mypy checks
- Well-documented codebase
Open Source + Premium Model
This tool follows an open-source + web platform business model:
- aiprofile-cli (this repo) - Free, open-source profiler
- aiprofile-web - Premium web platform with:
- Beautiful result visualizations
- Team collaboration features
- Historical trend tracking
- Custom alerts and automation
- API access for CI/CD integration
- Shareable reports
Contributing
Contributions welcome! Areas for enhancement:
- Additional language support (Go, Rust, Node.js, Java)
- Performance optimizations
- Better error messages
- Documentation improvements
- Example scripts for various use cases
Future Roadmap
- Support for additional languages (Go, Rust, Node.js)
- GPU profiling support (PyTorch, TensorFlow metrics)
- Distributed tracing integration
- Custom metrics/annotations
- Export formats (Flamegraph, Speedscope, etc.)
License
MIT - See LICENSE file for details
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiprofile-1.0.0.tar.gz.
File metadata
- Download URL: aiprofile-1.0.0.tar.gz
- Upload date:
- Size: 29.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e478a80b6807f8b6e2b4f087e93633cde8ec733ee51f9b53e543b9404c1af76e
|
|
| MD5 |
cb77c3ac2f553ed34ba74fafba34da36
|
|
| BLAKE2b-256 |
7ee7f9ba31cbd0c7a245ed3e37aba8d0a9c6e35a395d7da83656109006b769ef
|
File details
Details for the file aiprofile-1.0.0-py3-none-any.whl.
File metadata
- Download URL: aiprofile-1.0.0-py3-none-any.whl
- Upload date:
- Size: 36.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2fb1aff4b6ed5c74bdba14b19fca0491f5e5cb48f581b74e8c67124765a67570
|
|
| MD5 |
227bccc7d24ef9057b1df2ab74ba46d9
|
|
| BLAKE2b-256 |
fab630666c5da73bd87c4e906a9bdc18e610c024729d7fa81ecd533f171cdcc4
|