Profile and visualize energy consumption of Python code
Project description
py-power-profile ๐ | Python Energy Profiling Tool
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
โ๏ธ 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
๐ Support
- GitHub Issues: Report bugs
- Discussions: Community support
- Email: sherin.joseph2217@gmail.com
Made with โค๏ธ by sherin joseph roy
Empowering developers to build energy-efficient Python applications
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9906aba8de798971d4f50af6f30880eb456a96be4b9dc2c609547323e9de961e
|
|
| MD5 |
a8e7503797156c9417f37adde6348eb6
|
|
| BLAKE2b-256 |
cdc55d1ef5c84fb192d5c5b45c61fab55d302cb79f7dd41285a689258448913f
|
File details
Details for the file py_power_profile-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_power_profile-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72fff9f4a5e7233191f3f9c05891ca08e5db880328b918b5192572ec51d149f4
|
|
| MD5 |
07c65c28245ba0309a6cea452a7f46df
|
|
| BLAKE2b-256 |
0dc7f780e7a2fc1074d555e4f66e7f9c5bdea2f393a7f3e3b4a7d5a45c6bc149
|