https://github.com/waydotnet/profiler-powered-by-scalene
https://github.com/waydotnet/profiler-powered-by-scalene
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/waydotnet/profiler-powered-by-scalene
- Owner: WaYdotNET
- License: apache-2.0
- Created: 2025-12-29T19:35:26.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-12-29T22:42:48.000Z (6 months ago)
- Last Synced: 2026-01-13T20:41:49.390Z (5 months ago)
- Language: TypeScript
- Size: 838 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Profiler Powered by Scalene
[](https://open-vsx.org/)
[](LICENSE)
[](https://www.typescriptlang.org/)
[](https://rosepinetheme.com/)
[](https://github.com/WaYdotNET)
A powerful VS Code extension for [Scalene](https://github.com/plasma-umass/scalene) - the AI-powered CPU, GPU, and memory profiler for Python.
**Built for Open VSX** - Works in Cursor, VSCodium, and other VS Code alternatives.
**Author:** [Carlo Bertini (WaYdotNET)](https://github.com/WaYdotNET)
---
## π― Inspired By
This extension is **inspired by** the official [Scalene VS Code Extension](https://github.com/plasma-umass/scalene-vscode-extension) by [Emery Berger](https://github.com/emeryberger).
The original extension is available on the [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=EmeryBerger.scalene) but **not on Open VSX**, which is required for Cursor, VSCodium, and other open-source VS Code alternatives.
---
## β¨ What's Different from the Original
| Feature | Original Extension | This Extension |
|---------|-------------------|----------------|
| **Open VSX Support** | β Not available | β
Available |
| **Built-in Profile Viewer** | β Opens in browser only | β
Integrated webview with [RosΓ© Pine Dawn](https://rosepinetheme.com/) theme |
| **Source Code Display** | β Not shown | β
**Shows source code for each line** |
| **Complete Metrics** | β οΈ Basic | β
**All Scalene metrics (CPU, Memory, Peak, Copy, Growth)** |
| **Function Profiles** | β Not available | β
**Dedicated function profile view** |
| **Memory Leak Detection UI** | β CLI output only | β
Dedicated "Potential Leaks" tab **with code** |
| **Memory-Only Mode** | β Not available | β
Focus profiling on memory |
| **Profile Selected Code** | β Not available | β
Profile just selected region |
| **Auto-detect Python** | β οΈ Basic | β
Integrates with VS Code Python extension |
| **uv Support** | β | β
**Auto-detects uv + pyproject.toml** |
| **Status Bar Integration** | β Not available | β
Quick access from status bar |
| **Namespace** | `scalene.*` | `profilerScalene.*` (no conflicts!) |
| **Codebase** | JavaScript | TypeScript with strict mode |
---
## Features
| Feature | Description |
|---------|-------------|
| π₯ **CPU Profiling** | Line-level CPU usage with Python vs native code breakdown |
| πΎ **Memory Profiling** | Track memory allocations, peak usage, and copy operations per line |
| π¨ **Memory Leak Detection** | Automatically identifies potential memory leaks **with source code** |
| π **Built-in Profile Viewer** | Beautiful [RosΓ© Pine Dawn](https://rosepinetheme.com/) themed webview showing **source code + all metrics** |
| β‘ **Function Profiles** | See which functions consume the most resources |
| π **Source Code Display** | See the actual code for each profiled line |
| π **Scalene Web Viewer** | Open profiles in Scalene's native web UI |
| β‘ **Low Overhead** | Sampling-based profiling with minimal slowdown |
| π§ **Auto-Install** | Automatically installs Scalene with uv or pip |
## Requirements
- Python 3.8+
- Scalene (auto-installed if missing)
- Optional: uv (for faster installation if pyproject.toml exists)
## Quick Start
1. Open a Python file
2. Click the **π₯ Profiler** button in the status bar, or
3. Right-click β **Profiler: Profile Current File**
4. View results in the integrated viewer with **source code + metrics**
## Commands
| Command | Description |
|---------|-------------|
| `Profiler: Profile Current File` | Profile the entire file |
| `Profiler: Profile Memory Only` | Focus on memory profiling |
| `Profiler: Profile Selected Code` | Profile selected code region |
| `Profiler: View Last Profile` | Open built-in profile viewer |
| `Profiler: Open in Scalene Web Viewer` | Open in browser |
| `Profiler: Stop Profiling` | Stop current session |
| `Profiler: Install Scalene` | Install/update Scalene (auto-detects uv) |
## Profile Viewer
The built-in profile viewer uses the **[RosΓ© Pine Dawn](https://rosepinetheme.com/)** theme (WCAG AAA compliant) and has four tabs:
### π Line Profile
Shows for each line:
- **Line Number**
- **π Source Code** (the actual code!)
- **CPU (Python)** - Time in Python code
- **CPU (Native)** - Time in C/C++ libraries
- **Memory (Avg MB)** - Average memory allocated
- **Memory (Peak MB)** - Peak memory usage
- **Copy (MB/s)** - Memory copy rate (high = inefficient)
- **Growth (MB)** - Memory growth (leak indicator)
### β‘ Functions
- Function name and location
- CPU time breakdown
- Memory usage
- Peak memory
### π¨ Potential Leaks
- **π Source code** of leaking lines
- Leak score (0-100%)
- Memory allocated
- File:Line location
### π About
- Complete metrics guide and quick tips
- Color coding reference
- Links to Scalene, extension repository, and theme
## Extension Settings
| Setting | Description | Default |
|---------|-------------|---------|
| `profilerScalene.pythonPath` | Python interpreter path | Auto-detect |
| `profilerScalene.cpuSamplingRate` | CPU sampling rate (seconds) | `0.01` |
| `profilerScalene.reducedProfile` | Only show significant lines | `true` |
| `profilerScalene.outputDirectory` | Profile output directory | `.scalene-profiles` |
| `profilerScalene.autoOpenViewer` | Auto-open viewer after profiling | `true` |
| `profilerScalene.showMemoryLeaks` | Show potential leaks tab | `true` |
## Development
```bash
npm install # Install dependencies
npm run compile # Build
npm run watch # Watch mode
npm run package # Create .vsix
```
**Requirements:** Node.js 18+, npm
## Installation
### From Open VSX (Cursor/VSCodium)
Search for "Profiler Powered by Scalene" in the Extensions view, or:
```
ext install WaYdotNET.profiler-powered-by-scalene
```
### From VSIX
1. Download the `.vsix` file from [Releases](https://github.com/WaYdotNET/profiler-powered-by-scalene/releases)
2. In VS Code/Cursor: `Ctrl+Shift+P` β "Install from VSIX"
3. Select the `.vsix` file
## π Profiling Django Applications
### Method 1: Test Client Script (β Recommended)
```python
# profile_api.py
import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from django.test import Client
client = Client()
for i in range(100):
response = client.get('/api/users/')
print(f"β {i}: {response.status_code}")
```
Run: **Profiler: Profile Current File** on `profile_api.py`
### Method 2: Profile runserver
```bash
scalene run -o profile.json manage.py --- runserver 0:8000 --noreload
# Make requests, then Ctrl+C
# In Cursor: Profiler: View Last Profile
```
β οΈ Always use `--noreload` to prevent auto-reloader issues
---
### Method 3: Profile Specific View
```python
# profile_view.py
import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from myapp.views import expensive_view
from django.test import RequestFactory
for i in range(50):
request = RequestFactory().get('/my-url/')
response = expensive_view(request)
```
### Method 4: Profile Management Command
```python
# profile_command.py
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
from django.core.management import execute_from_command_line
execute_from_command_line(['manage.py', 'your_command', '--args'])
```
---
### Method 5: Profile ORM Queries
```python
# profile_queries.py
import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from myapp.models import User
users = User.objects.all()[:100]
for user in users:
orders = user.orders.all() # N+1 query?
print(f"User {user.id}: {len(orders)} orders")
```
**Look for:** High CPU (Native) = DB calls, High Memory (Peak) = Large querysets
---
### π What to Look For
| Issue | Indicator | Solution |
|-------|-----------|----------|
| **Memory Leak** | Growth > 0, Growth Rate > 110% | Check cached querysets, signal handlers, sessions |
| **Slow Views** | High CPU (Python) | Optimize serializers, templates, business logic |
| **Database** | High CPU (Native) | Use `select_related()`, `prefetch_related()`, check N+1 queries |
| **Memory Usage** | High Peak MB | Use `.iterator()`, pagination, streaming |
### π‘ Best Practices
- β
Use `--noreload` with runserver profiling
- β
Profile 50-100 iterations for reliable data
- β
Use **Profiler: Profile Memory Only** for leak detection
- β
Profile realistic workloads (not empty DBs)
## Credits
- **[Scalene](https://github.com/plasma-umass/scalene)** - The underlying profiler by Emery Berger, Sam Stern, and Juan Altmayer Pizzorno
- **[Original VS Code Extension](https://github.com/plasma-umass/scalene-vscode-extension)** - Inspiration for this extension by Emery Berger
- **[Carlo Bertini (WaYdotNET)](https://github.com/WaYdotNET)** - Author of this enhanced Open VSX version
## License
Apache License 2.0 - see [LICENSE](LICENSE)
---
**Made with β€οΈ by [WaYdotNET](https://github.com/WaYdotNET)**