Skip to content

test: time linear-time checks on the calling thread's CPU only - #1127

Merged
HugoRCD merged 1 commit into
mainfrom
fix/linear-time-thread-cpu-main
Nov 17, 2026
Merged

HugoRCD merged 1 commit into
mainfrom
fix/linear-time-thread-cpu-main

Conversation

@HugoRCD

@HugoRCD HugoRCD commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

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.

function cpuMs(): 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.

@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
evi Ready Ready Preview Nov 17, 2026 8:32pm UTC
evlog-telemetry Ready Ready Preview Nov 17, 2026 8:32pm UTC
3 Skipped Deployments
Project Deployment Actions Updated
evlog-docs Skipped Skipped v0 Nov 17, 2026 8:32pm UTC
evlog-render-lab Skipped Skipped Nov 17, 2026 8:32pm UTC
just-use-evlog Skipped Skipped Nov 17, 2026 8:32pm UTC

Request Review

@github-actions github-actions Bot added the test label Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Risk score: Low (1/4)

Assessed at 09d443e from the changed files and the title, by scripts/risk-score. A new push replaces this score.

  • On merge: nothing ships or deploys
  • 3 files changed (49 lines), 3 of them tests
Area Level Why
evlog core Low Tests only: test/README.md, helpers/timers.test.ts, helpers/timers.ts

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026

Copy link
Copy Markdown
@evlog/cli

npm i https://pkg.pr.new/@evlog/cli@1127

evlog

npm i https://pkg.pr.new/evlog@1127

@evlog/nuxthub

npm i https://pkg.pr.new/@evlog/nuxthub@1127

@evlog/signals

npm i https://pkg.pr.new/@evlog/signals@1127

@evlog/telemetry

npm i https://pkg.pr.new/@evlog/telemetry@1127

commit: 09d443e

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Possibly related PRs

  • 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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

@HugoRCD
HugoRCD merged commit 2bee0b4 into main Nov 17, 2026
30 checks passed
@HugoRCD
HugoRCD deleted the fix/linear-time-thread-cpu-main branch November 17, 2026 20:55

This branch was successfully deployed

2 active and 3 inactive deployments
Preview – evlog-telemetry — 09d443ec Deployed Nov 17, 2026 by vercel[bot]
Preview – evi — 09d443ec Deployed Nov 17, 2026 by vercel[bot]
Preview – evlog-render-lab — 09d443ec Deployed Nov 17, 2026 by vercel[bot]
Preview – evlog-docs — 09d443ec Deployed Nov 17, 2026 by vercel[bot]
Preview – just-use-evlog — 09d443ec Deployed Nov 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant