$npx -y skills add kevmoo/dash_skills --skill profile-dart-codeProfile Dart command-line applications using the VM Service protocol to capture CPU samples and identify performance bottlenecks. Helps agents automate CPU profiling, generate function call breakdown summaries, and export JSON profiles without a browser or DevTools.
| 1 | # Dart CPU Profiling |
| 2 | |
| 3 | Guidelines and automated tools for capturing CPU profiles and identifying |
| 4 | bottlenecks in Dart command-line applications. |
| 5 | |
| 6 | ## When to use this skill |
| 7 | - When asked to profile, optimize, or benchmark CPU execution of a Dart script |
| 8 | or CLI tool. |
| 9 | - When investigating hot loops, heavy function calls, or unexpected execution |
| 10 | overhead. |
| 11 | |
| 12 | ## Workflow |
| 13 | 1. **Ensure clean compilation**: Make sure the target Dart script runs cleanly |
| 14 | (`dart run <script.dart>`). |
| 15 | 2. **Run Profiler Script**: Use the automated profiling helper script inside |
| 16 | this skill directory to launch the target app with VM Service observability |
| 17 | enabled, capture CPU samples, and output top-consuming functions. |
| 18 | 3. **Analyze & Optimize**: Review the self and total sample percentages |
| 19 | reported by the tool to pinpoint bottlenecks (e.g., excessive object |
| 20 | allocation, costly hashing, virtual dispatch overhead). |
| 21 | |
| 22 | ## Running the Profiler Helper Script |
| 23 | |
| 24 | This repository includes a zero-dependency (using only official `vm_service`) |
| 25 | profiling script that launches any Dart file, connects to the VM Service, waits |
| 26 | for execution to complete (`--pause-isolates-on-exit`), retrieves CPU samples, |
| 27 | and prints a clean summary while exporting the full JSON profile. |
| 28 | |
| 29 | Run it from any working directory: |
| 30 | ```bash |
| 31 | dart run <dash_skills_repo>/skills/profile-dart-code/scripts/bin/profile.dart --out=cpu_profile.json -- <path_to_target.dart> [target_arguments...] |
| 32 | ``` |
| 33 | |
| 34 | ### Script Arguments |
| 35 | - `-o, --out=<file>`: Output file path to save the raw JSON CPU profile |
| 36 | (default: `cpu_profile.json`). |
| 37 | - `-p, --period=<micros>`: Sampling interval in microseconds (default: `1000`µs |
| 38 | = 1ms). Minimum `50`µs. |
| 39 | - `-- <target.dart> [args...]`: The Dart script to profile, followed by any |
| 40 | arguments passed to `main()`. |
| 41 | |
| 42 | > [!WARNING] |
| 43 | > **Potential Hangs**: When profiling or debugging Dart targets using VM services, |
| 44 | > target exceptions or connection issues can cause the process to hang |
| 45 | > indefinitely. Ensure your target script handles timeouts, and monitor the |
| 46 | > process output. |
| 47 | |
| 48 | ### Example Output |
| 49 | ``` |
| 50 | Connecting to VM service at ws://127.0.0.1:8181/ws... |
| 51 | Target execution paused at exit. Retrieving CPU profile samples... |
| 52 | |
| 53 | === Top CPU Functions (Self Samples) === |
| 54 | 1. _PuzzleSmart._shiftSlice (self: 34.2%, total: 41.0%) |
| 55 | 2. _countInversions (self: 18.5%, total: 18.5%) |
| 56 | 3. shortestPaths (self: 12.1%, total: 98.4%) |
| 57 | |
| 58 | Saved complete JSON profile to: cpu_profile.json |
| 59 | ``` |
| 60 | |
| 61 | ## Best Practices for Interpreting Profiles |
| 62 | 1. **Focus on Self % vs. Total %**: High `self %` indicates where CPU time is |
| 63 | spent directly inside a function's own body (math, loop branching, array |
| 64 | indexing). High `total %` with low `self %` indicates a dispatcher or outer |
| 65 | orchestration loop. |
| 66 | 2. **Look for Hidden Overhead**: Watch out for implicit object allocations |
| 67 | (`_copyData`, iterator wrappers, closure creation) inside tight loops. |
| 68 | 3. **Verify Optimizations Empirically**: Always record baseline sample counts |
| 69 | and execution duration (`time -v`) before and after applying optimizations. |