Skip to main content

Profile and visualize energy consumption of Python code

Project description

py-power-profile ๐Ÿ”‹ | Python Energy Profiling Tool

PyPI version Python 3.9+ License: MIT Code style: black

Profile and visualize energy consumption of Python code on laptops, desktops, and Raspberry Pi devices. No external services or paid APIs required.

๐Ÿš€ Quick Start

# Install py-power-profile
pip install py-power-profile

# Profile your Python script
py-power profile your_script.py --output results.json

# Generate energy badge
py-power badge results.json --target 100

โœจ Key Features

  • ๐Ÿ”‹ Real-time Energy Profiling: Measure CPU energy consumption at function and line level
  • ๐Ÿ–ฅ๏ธ Multi-Platform Support: Works on Intel/AMD (RAPL), ARM (hwmon), and universal fallback
  • ๐Ÿ“Š Rich Visual Reports: Beautiful tables with energy breakdowns and visual progress bars
  • ๐Ÿ”„ Performance Comparison: Diff two runs to detect energy regressions and improvements
  • ๐Ÿท๏ธ CI/CD Integration: Generate Shields.io-compatible badges for GitHub/GitLab
  • โšก Low Overhead: <5% CPU overhead, <150MB memory footprint
  • ๐Ÿ”ง Zero Configuration: Auto-detects best energy measurement backend

๐Ÿ“ฆ Installation

Basic Installation

pip install py-power-profile

With RAPL Support (Intel/AMD Processors)

pip install py-power-profile[rapl]

Development Installation

git clone https://github.com/Sherin-SEF-AI/py-power-profile.git
cd py-power-profile
pip install -e .[dev]

๐Ÿ› ๏ธ Usage Examples

Profile Energy Consumption

# Basic profiling
py-power profile my_script.py

# Save results to JSON
py-power profile my_script.py --output energy_results.json

# Use specific backend
py-power profile my_script.py --backend rapl

# Line-level profiling (higher accuracy)
py-power profile my_script.py --line

Compare Performance Changes

# Compare two profiling runs
py-power compare old_results.json new_results.json

Generate Energy Badges

# Generate badge for CI/CD
py-power badge results.json --target 100 --output badge.svg

# Status-only badge
py-power badge results.json --target 100 --status-only

๐Ÿ”ง Supported Energy Measurement Backends

๐Ÿ–ฅ๏ธ Intel/AMD RAPL (Recommended)

  • Accuracy: High (hardware-level measurement)
  • Requirements: Intel/AMD processor with RAPL support
  • Installation: pip install py-power-profile[rapl]

๐Ÿ“ฑ ARM/Raspberry Pi HWMON

  • Accuracy: High (hardware sensors)
  • Requirements: ARM device with power sensors
  • Availability: Raspberry Pi, ARM-based systems

๐Ÿ’ป Universal PSUTIL Estimation

  • Accuracy: Medium (CPU usage estimation)
  • Requirements: None (fallback option)
  • Availability: All systems

๐Ÿงช Mock Backend (Testing)

  • Accuracy: Deterministic (for testing)
  • Use Case: Unit tests, CI/CD
  • Availability: All systems

๐Ÿ“Š Output Formats

Rich Console Tables

Energy Profile Results (Backend: rapl)
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Function                                           โ”ƒ Callsโ”ƒ Energyโ”ƒ Avg   โ”ƒ Time   โ”ƒ %     โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ my_script.py:heavy_computation                     โ”‚  100 โ”‚ 1500mJโ”‚ 15.0mJโ”‚ 50.0ms โ”‚ 75.0% โ”‚
โ”‚ my_script.py:light_operation                       โ”‚   10 โ”‚  100mJโ”‚ 10.0mJโ”‚  5.0ms โ”‚  5.0% โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”˜

JSON Output

{
  "metadata": {
    "backend": "rapl",
    "line_level": false,
    "timestamp": 1640995200.0
  },
  "functions": {
    "my_script.py:heavy_computation": {
      "calls": 100,
      "total_energy_mj": 1500.0,
      "avg_energy_mj": 15.0,
      "total_time_ms": 50.0
    }
  },
  "summary": {
    "total_energy_mj": 2000.0,
    "total_time_ms": 100.0,
    "function_count": 5
  }
}

SVG Badges

Energy

โš™๏ธ Configuration

Environment Variables

export PY_POWER_BACKEND="rapl"
export PY_POWER_TDP_WATTS="15"
export PY_POWER_ENERGY_BUDGET_MJ="1000"

pyproject.toml Configuration

[tool.py-power-profile]
backend = "auto"
tdp_watts = 15          # CPU TDP for estimation
energy_budget_mj = 1000 # CI threshold
ignore = ["tests/*"]    # glob patterns

๐Ÿ”„ GitHub Actions Integration

name: Energy Profile
on: [push, pull_request]

jobs:
  energy-profile:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.9'
    
    - name: Install py-power-profile
      run: pip install py-power-profile[rapl]
    
    - name: Run energy profile
      run: py-power profile tests/test_script.py --output results.json
    
    - name: Generate badge
      run: py-power badge results.json --target 100 --output badge.svg
    
    - name: Upload results
      uses: actions/upload-artifact@v3
      with:
        name: energy-results
        path: [results.json, badge.svg]

๐Ÿงช Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=py_power_profile

# Test specific backend
py-power profile samples/quick.py --backend mock

๐Ÿ“ˆ Performance Benchmarks

Metric Value
Profiling Overhead <5% CPU
Memory Footprint <150MB
Supported Python 3.9+
Supported OS Linux, macOS, Windows

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

git clone https://github.com/Sherin-SEF-AI/py-power-profile.git
cd py-power-profile
pip install -e .[dev]
pre-commit install

๐Ÿ“š Documentation

๐Ÿ” Use Cases

Software Development

  • Performance Optimization: Identify energy-intensive functions
  • Code Review: Energy impact analysis in pull requests
  • CI/CD: Automated energy regression detection

Research & Academia

  • Algorithm Analysis: Compare energy efficiency of algorithms
  • System Research: Energy consumption studies
  • Green Computing: Sustainable software development

IoT & Embedded Systems

  • Battery Life: Optimize Python applications for battery-powered devices
  • Raspberry Pi: Energy profiling on ARM devices
  • Edge Computing: Resource-constrained environments

๐Ÿ† Why py-power-profile?

  • ๐Ÿ”ฌ Scientific Accuracy: Hardware-level energy measurement
  • ๐Ÿš€ Easy Integration: Simple CLI with rich output
  • ๐Ÿ”ง Flexible Configuration: Multiple backends and options
  • ๐Ÿ“Š Professional Reports: Beautiful, informative output
  • ๐Ÿ”„ CI/CD Ready: GitHub Actions and badge integration
  • ๐Ÿ“ฑ Cross-Platform: Works on laptops, desktops, and SBCs

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • pyRAPL for Intel/AMD RAPL support
  • Rich for beautiful terminal output
  • Typer for CLI framework

๐Ÿ“ž Support


Made with โค๏ธ by sherin joseph roy

Empowering developers to build energy-efficient Python applications

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

py_power_profile-0.1.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

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

py_power_profile-0.1.0-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file py_power_profile-0.1.0.tar.gz.

File metadata

  • Download URL: py_power_profile-0.1.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for py_power_profile-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9906aba8de798971d4f50af6f30880eb456a96be4b9dc2c609547323e9de961e
MD5 a8e7503797156c9417f37adde6348eb6
BLAKE2b-256 cdc55d1ef5c84fb192d5c5b45c61fab55d302cb79f7dd41285a689258448913f

See more details on using hashes here.

File details

Details for the file py_power_profile-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_power_profile-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72fff9f4a5e7233191f3f9c05891ca08e5db880328b918b5192572ec51d149f4
MD5 07c65c28245ba0309a6cea452a7f46df
BLAKE2b-256 0dc7f780e7a2fc1074d555e4f66e7f9c5bdea2f393a7f3e3b4a7d5a45c6bc149

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