You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
test: time linear-time checks on the calling thread's CPU only - #1127
Written by an AI agent on behalf of @HugoRCD. @HugoRCD has not reviewed it yet.
Ports #1126 to main, plus a fallback for older Node 22.
expectLinearTime now times each run with process.threadCpuUsage() instead of process.cpuUsage(). Process CPU time also counts V8's background GC and compiler threads, which can run alongside the long call only and push the ratio past 24. On CI that made redact-integration.test.ts > masks tokens in linear time fail with ratios around 30 (for example expected 31.722879684419304 to be less than 24). test/helpers/timers.ts and timers.test.ts are identical on main and v3, so #1126's diff applies as is.
Not in #1126: process.threadCpuUsage() exists from Node 22.19 (and 23.9), and v3 declares engines: >=22.0.0. On older versions the helper falls back to process.cpuUsage(), so the linear-time tests keep running there instead of throwing. CI is unaffected either way: main tests on Node 24.22.3, and v3's Test (22) job installs 22.23.4.
functioncpuMs(): number{const{ user, system }=process.threadCpuUsage?.()??process.cpuUsage()return(user+system)/1_000}
Validation
The two new tests in test/helpers/timers.test.ts failed first:
"ignores CPU time other threads use while the long input runs", run against main's current timers.ts (lines filtered from the vitest output):
× ignores CPU time other threads use while the long input runs 4ms
The other tests in that run failed too, because they now mock threadCpuUsage, which the old helper does not read.
"times on process CPU time where Node has no threadCpuUsage", run with the fallback removed (filtered):
× times on process CPU time where Node has no threadCpuUsage 5ms
"process.threadCpuUsage is not a function"
Tests 1 failed | 9 passed (10)
With this branch, the 4 test files that use expectLinearTime pass:
Test Files 4 passed (4)
Tests 109 passed (109)
pnpm --filter evlog run lint is clean. In the full local suite, test/nuxt/auto-import-types.test.ts fails, and it fails the same way on unchanged main in my checkout, whose node_modules come from a v3 checkout. CI runs it against main's own build.
No changeset: only tests change.
#1126 on v3 does not have the fallback yet. It reaches v3 with the next main → v3 sync, or it can be added to #1126 directly.
ELI5
One of the tests checks that the redaction code stays fast on long input by comparing how long a short and a long input take. It sometimes failed because the clock also counted work Node was doing in the background. It now counts only the test's own work, and on older Node versions without that clock it uses the old one.
Summary by CodeRabbit
Tests
Linear-time checks now measure CPU time on the calling thread when available, with process-wide CPU timing as a fallback. Tests cover both timing methods and verify that CPU usage from other threads does not affect the calling-thread measurement.
Documentation
Updated test-convention guidance to describe calling-thread CPU timing and its exclusions.
No actionable comments were generated in the recent review.
Walkthrough
The timing helper now measures calling-thread CPU time when available and falls back to process CPU time. Tests cover both paths, and the test guidance describes the updated timing scope.
Changes: Calling-thread CPU timing
Layer / File(s)
Summary
CPU measurement and validation packages/evlog/test/helpers/timers.ts, packages/evlog/test/helpers/timers.test.ts, packages/evlog/test/README.md
cpuMs uses process.threadCpuUsage() when available and falls back to process.cpuUsage(). Tests cover other-thread CPU usage and the fallback. The test guidance describes calling-thread CPU time.
evloghq/evlog#1071: Changes the expectLinearTime timing source, which this PR further updates.
evloghq/evlog#1122: Changes retry logic in the same helper and updates overlapping tests and documentation.
evloghq/evlog#1027: Introduces the ratio-based expectLinearTime check whose timing source this PR changes.
Suggested labels:test
Merge Risk: Minimal · up to 09d44
This change only affects how linear-time tests measure CPU time, so production behavior is unchanged. Merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title uses the conventional-commits type 'test' and states the main change: expectLinearTime now times the calling thread's CPU only. The change set matches this: timers.ts switches cpuMs to proce…
Description check
The description follows the template's main sections: it explains the change and its reason in the Description part, gives validation steps, and includes an ELI5. It does not fill in the Linked issue …
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title uses the conventional-commits type 'test' and states the main change: expectLinearTime now times the calling thread's CPU only. The change set matches this: timers.ts switches cpuMs to process.threadCpuUsage with a fallback, and the tests and docs are updated to match. The scope is omitted, which fits the template for a repo-wide helper change.
Full details: Description check
Explanation
The description follows the template's main sections: it explains the change and its reason in the Description part, gives validation steps, and includes an ELI5. It does not fill in the Linked issue section (it names PR #1126 in prose but has no issue link) and leaves the checklist unchecked. The template has no required fields beyond these, and the checklist items are not marked as done.
Resolution
Optionally, add a linked issue or PR reference under the Linked issue heading, and mark the checklist items that apply (for example, the documentation item, since the test README changed).
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: 59f9ec9c-3007-4c5b-9814-42915ce24eb9
Commits
Reviewing files that changed from the base of the PR and between 8644413 and 09d443e.
Files selected for processing: 3
packages/evlog/test/README.md
packages/evlog/test/helpers/timers.test.ts
packages/evlog/test/helpers/timers.ts
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ports #1126 to
main, plus a fallback for older Node 22.expectLinearTimenow times each run withprocess.threadCpuUsage()instead ofprocess.cpuUsage(). Process CPU time also counts V8's background GC and compiler threads, which can run alongside the long call only and push the ratio past 24. On CI that maderedact-integration.test.ts > masks tokens in linear timefail with ratios around 30 (for exampleexpected 31.722879684419304 to be less than 24).test/helpers/timers.tsandtimers.test.tsare identical onmainandv3, so #1126's diff applies as is.Not in #1126:
process.threadCpuUsage()exists from Node 22.19 (and 23.9), andv3declaresengines: >=22.0.0. On older versions the helper falls back toprocess.cpuUsage(), so the linear-time tests keep running there instead of throwing. CI is unaffected either way:maintests on Node 24.22.3, andv3's Test (22) job installs 22.23.4.Validation
The two new tests in
test/helpers/timers.test.tsfailed first:"ignores CPU time other threads use while the long input runs", run against
main's currenttimers.ts(lines filtered from the vitest output):The other tests in that run failed too, because they now mock
threadCpuUsage, which the old helper does not read."times on process CPU time where Node has no threadCpuUsage", run with the fallback removed (filtered):
With this branch, the 4 test files that use
expectLinearTimepass:pnpm --filter evlog run lintis clean. In the full local suite,test/nuxt/auto-import-types.test.tsfails, and it fails the same way on unchangedmainin my checkout, whosenode_modulescome from av3checkout. CI runs it againstmain's own build.No changeset: only tests change.
#1126 on
v3does not have the fallback yet. It reachesv3with the nextmain→v3sync, or it can be added to #1126 directly.ELI5
One of the tests checks that the redaction code stays fast on long input by comparing how long a short and a long input take. It sometimes failed because the clock also counted work Node was doing in the background. It now counts only the test's own work, and on older Node versions without that clock it uses the old one.
Summary by CodeRabbit