t3code icon indicating copy to clipboard operation
t3code copied to clipboard

perf(server): fix N+1 PowerShell process query and reduce sampling frequency

Open UtkarshUsername opened this issue 4 months ago • 2 comments

What Changed

Eliminated two sources of excessive CPU usage in the Windows process resource monitor:

  1. N+1 PowerShell CIM query to bulk query (ProcessDiagnostics.ts): The Windows process reader was querying Win32_PerfFormattedData_PerfProc_Process individually 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.
  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: readWindowsProcessRows no longer runs a filtered Win32_PerfFormattedData_PerfProc_Process CIM call inside the per-process loop. It loads all perf rows once into a hashtable keyed by IDProcess, then walks Win32_Process and joins in memory—two CIM queries per cycle instead of 1+N.

Resource monitor: SAMPLE_INTERVAL_MS moves 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 readWindowsProcessRows in ProcessDiagnostics.ts to preload all Win32_PerfFormattedData_PerfProc_Process entries into a hashtable keyed by IDProcess, eliminating a per-process WMI query.
  • Increases SAMPLE_INTERVAL_MS in 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.

UtkarshUsername avatar Jul 29 '26 10:07 UtkarshUsername

[!IMPORTANT]

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 97fb8722-bcb4-4dfc-b5c7-48c11a50685b

You can disable this status message by setting the reviews.review_status to false in 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 29 '26 10:07 coderabbitai[bot]

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.

macroscopeapp[bot] avatar Jul 29 '26 11:07 macroscopeapp[bot]

Superseded by #2679

UtkarshUsername avatar Jul 29 '26 14:07 UtkarshUsername