Profile slow code with cProfile, node --cpu-prof, DevTools or EXPLAIN, fix the measured hot spots and re-measure to confirm the gain.
---
name: performance-profiler
description: Finds where a program actually spends time or memory by running a real profiler (Python cProfile, Node --cpu-prof, browser DevTools, database EXPLAIN) on a reproducible workload, then proposes and re-measures targeted fixes. Use when code is slow, a request or job got slower, memory grows, or someone asks to profile or optimise performance.
compatibility: Needs a runnable copy of the code and a representative workload. Commands shown assume Python 3 or Node.js on PATH; database steps need read access to the database.
metadata:
version: "1.0.0"
---
# Performance Profiler
Measure first, change one thing, measure again. Do not guess hot spots from reading code, and do not predict a speed-up percentage before you have measured it.
## When not to use
- There is no way to run the code with realistic input (ask for a sample or a recorded request first).
- The problem is availability or errors rather than speed — use debug-helper.
## Inputs to collect
- The slow operation and how to reproduce it (command, endpoint, test, data size).
- The current measurement and the target (for example "p95 of /orders is 2.3 s, want < 500 ms").
- Environment: language/runtime version, production-like data volume, database engine.
## Workflow
1. **Baseline.** Run the workload 3–5 times and record wall time (and peak memory if relevant). Note the machine and data size; small inputs hide O(n²) behaviour.
2. **Profile with the right tool.**
- Python CPU: `python -m cProfile -o prof.out app.py`, then `python -c "import pstats; pstats.Stats('prof.out').sort_stats('cumulative').print_stats(15)"`.
- Python memory: `tracemalloc` snapshots around the suspect block.
- Node CPU: `node --cpu-prof --cpu-prof-dir=prof app.js`; open the `.cpuprofile` in Chrome DevTools (Performance panel) or VS Code.
- Node heap: `node --heap-prof app.js` or a DevTools heap snapshot.
- Browser: DevTools Performance recording of the interaction.
- SQL: `EXPLAIN` / `EXPLAIN ANALYZE` (PostgreSQL), `EXPLAIN QUERY PLAN` (SQLite); count queries per request to spot N+1 patterns.
3. **Read the profile.** List the top functions by cumulative time with file:line, call counts, and the share of total time. Separate your own code from library and I/O wait.
4. **Explain each hot spot** with the evidence: nested loop over two large lists, repeated query inside a loop, re-render on every keystroke, large JSON serialisation, missing index.
5. **Propose the smallest fix per hot spot** (dict/Map lookup instead of a scan, batch query, memoisation, index, streaming) and the trade-off (memory, cache invalidation, write cost of an index).
6. **Re-measure** after each change with the same workload and confirm outputs are identical (add an assertion or test). Report measured numbers only.
## Output format
```
PERFORMANCE REPORT <operation> · <runtime/version> · <data size>
Baseline: <median wall time over N runs> Tool: <profiler + command>
Hot spots (from profile):
1. <function> <file:line> — <cum time / share>, <calls> — cause — proposed fix
After fix: <median wall time>, outputs identical: yes/no (how checked)
Not measured / open questions
```
## Limitations
- Profilers add overhead; compare relative shares, and confirm with plain wall-clock timing.
- Results on a laptop with sample data may not match production; say which you measured.
- Never run profiling or `EXPLAIN ANALYZE` (which executes the query) against production without permission.
## Example
Input: "Matching 4,000 orders to 4,000 users takes too long" with a Python function that loops over `users` for every order.
Good result: cProfile shows `join_nested` holding almost all cumulative time; the fix builds `{u["id"]: u for u in users}` once and looks up each order; the re-run shows the measured new time and an assertion that both versions return the same list.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A Python batch job matches 4,000 orders to 4,000 users with a nested loop and has become slow as data grew.
This script got slow: for each order it loops over all users to find the matching id (4,000 orders, 4,000 users). Profile it and fix the slowest part without changing the output.
A baseline timing, then `python -m cProfile -o prof.out` output summarised as a short hot-spot list in which the nested-loop function holds nearly all cumulative time while the lookup version is negligible. The proposed fix builds a dict keyed by user id once. The report gives the re-measured time from an actual re-run (no predicted percentage) and states how output equality was checked, for example an `assert old == new`.
AI Agent Skills are reusable workflow packages that extend your AI coding assistant with multi-step capabilities. Install performance-profiler by following these steps.
SKILL.md inside, in the location documented by your client. Do not assume every AI application reads the same directory.Requires an Agent Skills-compatible client; supported locations and activation vary by version. Read the SKILL.md setup guide. MCP connects tools and data and does not, by itself, install a skill.