Skip to main content

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:
    1. New profiler in profilers/<language>/
    2. New runner in runner/<language>_runner.py
    3. Updates to detector.py
    4. No changes to CLI or AI analysis logic needed

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

  1. Profile - Runs Scalene on your Python script to collect CPU and memory metrics
  2. Analyze (optional) - Sends profiling data to Claude for AI-powered analysis and recommendations
  3. Display - Shows beautiful terminal output with tables, metrics, and actionable suggestions
  4. 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


Download files

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

Source Distribution

aiprofile-1.0.1.tar.gz (29.8 kB view details)

Uploaded Source

Built Distribution

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

aiprofile-1.0.1-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

Details for the file aiprofile-1.0.1.tar.gz.

File metadata

  • Download URL: aiprofile-1.0.1.tar.gz
  • Upload date:
  • Size: 29.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for aiprofile-1.0.1.tar.gz
Algorithm Hash digest
SHA256 54eb13fdfebc8c82f2d5167ae107027036a1c236a2f33ec949a74f52753d97d5
MD5 994251b7ea852ada80d40b1f9ec3a74c
BLAKE2b-256 e0da13ebed9be67beb605cde0cf33f789d2799a8fe69635590629c8f2e058149

See more details on using hashes here.

File details

Details for the file aiprofile-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiprofile-1.0.1-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

Hashes for aiprofile-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 472bfb45f4c41b46d88d5edc72a732107dd6d1986f82b03efd63da443fe7dca5
MD5 ed9285ae001760a7f37c454c324c2cc6
BLAKE2b-256 5b3886eaaca83b82a3a004ef4c0232edbe3a807d09497d6838ae57063d84d30c

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