Skip to content

perf(detection): charge the scan budget per pass and cut monitor overhead - #152

Merged
rennf93 merged 3 commits into
masterfrom
perf/detection-cpu
Oct 16, 2026
Merged

rennf93 merged 3 commits into
masterfrom
perf/detection-cpu

Conversation

@rennf93

@rennf93 rennf93 commented Oct 16, 2026

Copy link
Copy Markdown
Member

Closes #151

Port of the reference fix in Guard-Core/guard-core#173. ScanTimeBudget charges CPU per pass: one clock reading when a pass opens, a charge at each yieldIfDue slice boundary (charged before the yield and restarted after, so other tasks' CPU is never charged), around the awaits of the opt-in worker pool, and a final charge when the pass closes; a budget without a cap never reads the clock. The monitor now matches the reference: it flags only slower-than-average samples, honours detectionMinSamplesForAnomaly (default 30, 10 to 1000, window max(min samples, 100)), computes the mean once and the deviation in one allocation-free loop, and skips the sanitized copy when no callback is registered. On a ~1 KB JSON-like value plus 10 short header values detect went from 5.11 ms to 4.05 ms of CPU. Tests cover the clock reads per pass, a long pass stopping within about one slice, suspended time not being charged, and the one-sided z-score.

…e engine

The statistical check used the absolute z-score, so a faster-than-average sample was flagged. It is now one-sided (z > threshold) with the early return at or below the mean, reads the minimum sample count from detectionMinSamplesForAnomaly (default 30, range 10 to 1000, matching the reference) instead of a fixed 10, and computes mean and standard deviation in allocation-free loops.
…attern

The per-request scan time budget read the thread CPU clock around every pattern. It now charges per pass: one read when a pass opens, one at each 2 ms wall-clock slice boundary and one when the pass closes. The per-pattern check reads only a cheap monotonic clock and compares the charged total, so the budget lags the true spend by at most one slice plus one pattern.
@github-actions github-actions Bot added area: detection-engine Touches packages/core/src/detection-engine/ area: handlers Touches packages/core/src/handlers/ area: models Touches packages/core/src/models/ package: core Changes inside @guardcore/core tests Test suite changes (vitest) labels Oct 16, 2026
@rennf93
rennf93 merged commit f857414 into master Oct 16, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: detection-engine Touches packages/core/src/detection-engine/ area: handlers Touches packages/core/src/handlers/ area: models Touches packages/core/src/models/ package: core Changes inside @guardcore/core tests Test suite changes (vitest)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scan time budget and performance monitor add CPU on every pattern check

1 participant