perf(server): fix N+1 PowerShell process query and reduce sampling frequency
What Changed
Eliminated two sources of excessive CPU usage in the Windows process resource monitor:
- N+1 PowerShell CIM query to bulk query (
ProcessDiagnostics.ts): The Windows process reader was queryingWin32_PerfFormattedData_PerfProc_Processindividually for each process via an IDProcess filter inside a ForEach-Object loop. Changed to query all perf counter data once into a hashtable, then join with the process list. Reduced from 1+N CIM queries to exactly 2. - Sampling interval 5s to 15s (ProcessResourceMonitor.ts): Tripled the interval between resource samples, reducing overhead 3x while maintaining adequate granularity for multi-second bucket aggregation.
Why
On Windows machines with 200+ processes, ProcessResourceMonitor was spawning powershell.exe every 5 seconds with an N+1 WMI query pattern. Each cycle issued 1+N CIM queries (e.g., 206 queries for 205 processes). The old command consistently timed out after 60 seconds on a typical development machine, pegging CPU indefinitely.
| Metric | Before | After | Improvement |
|---|---|---|---|
| Per-query time | >60s (timed out) | ~2.7s | ≥22x+ |
| CIM queries per cycle | 1+N (e.g., 206) | 2 | ~100x |
| Sampling interval | 5s | 15s | 3x |
| Effective overhead | 100% CPU (never finishes) | ~18% for 2.7s | ≥66x |
Checklist
- [x] This PR is small and focused
- [x] I explained what changed and why
- [ ] I included before/after screenshots for any UI changes
- [ ] I included a video for animation/interaction changes
[!NOTE] Low Risk Diagnostics-only changes on Windows sampling path; behavior is coarser metrics (15s) but lower server CPU load, with no auth or data-path impact.
Overview Reduces Windows process diagnostics overhead by fixing how PowerShell loads CPU perf data and by sampling less often.
Windows process query:
readWindowsProcessRowsno longer runs a filteredWin32_PerfFormattedData_PerfProc_ProcessCIM call inside the per-process loop. It loads all perf rows once into a hashtable keyed byIDProcess, then walksWin32_Processand joins in memory—two CIM queries per cycle instead of 1+N.Resource monitor:
SAMPLE_INTERVAL_MSmoves from 5s to 15s, so the background sampler runs three times less often. Approximate CPU-second rollups use the new interval; tests now expect 6 instead of 2 for the same two-sample scenario.Reviewed by Cursor Bugbot for commit 9d9363e3d4b7016127366951bc0e4211bbf3a1a7. Bugbot is set up for automated code reviews on this repo. Configure here.
[!NOTE]
Fix N+1 PowerShell process query and increase sampling interval to 15s
- Rewrites
readWindowsProcessRowsin ProcessDiagnostics.ts to preload allWin32_PerfFormattedData_PerfProc_Processentries into a hashtable keyed byIDProcess, eliminating a per-process WMI query.- Increases
SAMPLE_INTERVAL_MSin ProcessResourceMonitor.ts from 5,000 ms to 15,000 ms to reduce polling overhead.- Behavioral Change: process CPU metrics are now refreshed every 15s instead of every 5s.
Macroscope summarized 9d9363e.
[!IMPORTANT]
Review skipped
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
97fb8722-bcb4-4dfc-b5c7-48c11a50685bYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
- [ ] 🔍 Trigger review
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
Approvability
Verdict: Approved 2db1250
Performance optimization fixing N+1 PowerShell process query and reducing diagnostic sampling frequency from 5s to 15s. Self-contained changes to monitoring code with corresponding test updates.
You can customize Macroscope's approvability policy. Learn more.
Superseded by #2679