Skip to content

fix: replace hrtime with performance not to allocate#1204

Merged
ferhatelmas merged 1 commit into
masterfrom
ferhat/no-hrtime
Jul 6, 2026
Merged

fix: replace hrtime with performance not to allocate#1204
ferhatelmas merged 1 commit into
masterfrom
ferhat/no-hrtime

Conversation

@ferhatelmas

Copy link
Copy Markdown
Member

What kind of change does this PR introduce?

refactor

What is the current behavior?

We use process.hrtime at the moment, which returns bigint. It's heap allocated.
It's not needed because a few seconds later, it's converted to a number for latency metrics.

What is the new behavior?

Use performance.now because it's monotonic, made for this purpose and not heap allocated.

Additional context

This cuts down garbage in hot paths.

Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
@ferhatelmas ferhatelmas requested a review from a team as a code owner July 6, 2026 08:17
Copilot AI review requested due to automatic review settings July 6, 2026 08:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors latency measurements across the storage DB layer, queue scheduling, and HTTP metrics to use performance.now() (numeric monotonic timestamps) instead of process.hrtime.bigint(), reducing allocations in hot paths while preserving duration semantics (seconds).

Changes:

  • Replace process.hrtime.bigint() start/end timing with performance.now() and compute durations in seconds via (now - start) / 1000.
  • Update type surfaces where timing values are stored (e.g., FastifyRequest.metricsStartTime and DB duration recorder signature) from bigint to number.
  • Add/adjust unit tests to assert metrics are recorded from numeric monotonic timestamps and to fix falsy-check behavior for 0 start times.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/storage/database/pg.ts Switch DB query duration timing to performance.now() and update recorder signature to number.
src/storage/database/pg.test.ts Add deterministic tests for scoped/unscoped DB query duration metrics using mocked performance.now().
src/internal/queue/event.ts Switch queue scheduling timing to performance.now() and compute duration in seconds.
src/internal/queue/event.test.ts Add test verifying queue scheduling duration metric uses numeric monotonic timestamps.
src/http/plugins/metrics.ts Switch HTTP request timing to performance.now(), fix start-time presence check for 0, and update request augmentation type.
src/http/plugins/metrics.test.ts Add test verifying HTTP metrics use numeric monotonic timestamps and that 0 is handled correctly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 28777683411

Coverage decreased (-0.008%) to 78.846%

Details

  • Coverage decreased (-0.008%) from the base build.
  • Patch coverage: 8 of 8 lines across 3 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 12239
Covered Lines: 10093
Line Coverage: 82.47%
Relevant Branches: 7039
Covered Branches: 5107
Branch Coverage: 72.55%
Branches in Coverage %: Yes
Coverage Strength: 420.76 hits per line

💛 - Coveralls

@ferhatelmas ferhatelmas merged commit 6f5a298 into master Jul 6, 2026
27 checks passed
@ferhatelmas ferhatelmas deleted the ferhat/no-hrtime branch July 6, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants