Repository navigation
perf: use performance.now() for query execution timing - #12750
Conversation
Code Review by Qodo
1.
|
|
Both findings were correct — fixed in a74a442. 1. Missing Added Two details worth flagging, since they were not in the review and would each have been silent failures:
2. Falsy start-time check. Correct, and I introduced it.
The open question from the original description still stands for a maintainer: |
a74a442 to
0c372c5
Compare
| * Only ever use the returned value to compute a duration: its time origin | ||
| * is not the Unix epoch. | ||
| */ | ||
| static performanceNow(): number { |
There was a problem hiding this comment.
PlatformTools (and BrowserPlatformTools) are for those utility functions that are different across platforms. Since the implementation is the same for both NodeJS and browsers, this functions could be moved maybe to DateUtils.
There was a problem hiding this comment.
Moved — thanks, that's the right call. performanceNow() is gone from both PlatformTools.ts and BrowserPlatformTools.template (both files are byte-identical to master again) and now lives in DateUtils, which the 13 query runners import instead. DateUtils already has unit coverage in test/unit/util/date-utils.test.ts, so it seems a natural home for it.
Done in a511c24.
| * is not the Unix epoch. | ||
| */ | ||
| static performanceNow(): number { | ||
| const globalVariable = PlatformTools.getGlobalVariable() |
There was a problem hiding this comment.
I think globalThis can safely be used everywhere. PlatformTools.getGlobalVariable() is probably not necessary anymore.
globalThis.performance?.now() ?? Date.now()There was a problem hiding this comment.
Agreed, and applied — the helper is now just:
static performanceNow(): number {
return globalThis.performance?.now() ?? Date.now()
}PlatformTools.getGlobalVariable() is no longer involved. As a side effect this also settles the bot's "clock source may mismatch" note above: with no per-call resolution left, there is nothing that can change between the start and end readings.
|
Hi @pri12ya871, thanks for the PR, only wrote a small comment. Heads up for reviewers: there might be other PRs changing lines with |
|
@pri12ya871 You can round-off the time at logger level - only few changes. |
|
Thanks both — pushed an update addressing all of this. @Cprakhar rounding is now done at the logger level as you suggested (c3cdafa). A single @alumni both of your comments are addressed in a511c24: the helper is out of I also added On the |
c3cdafa to
b2f140c
Compare
commit: |
|
Rebased onto current @alumni that should also tie off your heads-up about other PRs touching these lines: on top of current |
|
Thanks for approving! Just checking if anything else is needed. If it's all good, could you please merge it when you get a chance? |
|
This repository was recently restructured into a pnpm workspace, and the If your branch has diverged enough that a full rebase isn't worth it, Sorry for the disruption, and thanks for your patience. |
QueryRunner.query() measured elapsed time with Date.now(), which is wall-clock and can jump backwards when the system clock is adjusted, producing negative or inflated durations. performance.now() is monotonic and higher resolution, which is what an elapsed-time measurement wants. Replaces the 30 timing call sites across the 13 driver query runners. Date.now() is deliberately left where the value is a real timestamp rather than an elapsed measurement: query result cache expiry in DbQueryResultCache and RedisQueryResultCache, CommandUtils, and SelectQueryBuilder. Closes typeorm#12731
…-time check Two follow-ups on the Date.now() to performance.now() change. 1. performance is not guaranteed on every runtime TypeORM targets. The Cordova, React Native, Expo, NativeScript and sql.js drivers run outside Node, and calling performance.now() unconditionally would throw before the query executed, turning a timing improvement into broken database access. Adds PlatformTools.performanceNow(), which prefers the monotonic performance.now() when the runtime exposes it and falls back to Date.now() otherwise, and routes every query runner through it. The helper is added to BrowserPlatformTools.template as well, since that replaces PlatformTools in browser builds. 2. SqlServerQueryRunner guarded its error-path timing with a truthiness check on queryStartTime. Date.now() could never return 0, but performance.now() can, which would silently suppress queryExecutionTime for after-query broadcasting and slow-query logging. Uses an explicit undefined check.
The implementation was identical in PlatformTools and BrowserPlatformTools.template, so it was not platform-specific code and did not belong in that pair. Moved to DateUtils and reduced to `globalThis.performance?.now() ?? Date.now()`, which drops the getGlobalVariable() indirection.
performance.now() is sub-millisecond, so the interpolated slow-query line changed from `[SLOW QUERY: 1234 ms]` to `[SLOW QUERY: 1234.5678 ms]`. Rounding once in AbstractLogger.logQuerySlow keeps the existing output format for every logger implementation, while maxQueryExecutionTime and the afterQuery event keep the unrounded value.
1c652b5 to
eb640a2
Compare
Description of change
QueryRunner.query()measured elapsed execution time withDate.now(). That is a wall-clock source, so an NTP correction or a manual clock change mid-query can make the measurement jump backwards, producing a negative or inflated duration.performance.now()is monotonic and higher resolution, which is what an elapsed-time measurement wants.Current behavior:
queryExecutionTimeis derived from twoDate.now()readings, at whole-millisecond resolution, and is not monotonic.New behavior: the same measurement uses
performance.now(). The value feeding themaxQueryExecutionTimecomparison andlogQuerySlowis now monotonic and sub-millisecond.This replaces the 30 timing call sites across the 13 driver query runners.
Date.now()is deliberately left in place wherever the value is a real timestamp rather than an elapsed measurement — swapping those would be a bug, sinceperformance.now()is relative to process start rather than the epoch:src/cache/DbQueryResultCache.tssrc/cache/RedisQueryResultCache.tssrc/commands/CommandUtils.tssrc/query-builder/SelectQueryBuilder.tsHow I verified it:
pnpm run compilepasses,eslintreports no new errors on the changed files, andprettier --checkis clean. No import is needed —performancehas been a Node global since v16 and this package requires^20.19.0 || ^22.13.0 || >=24.11.0.One decision worth your call
Date.now()returns an integer;performance.now()returns a float. That value is interpolated straight into the slow-query log inFileLogger:`[SLOW QUERY: ${message.additionalInfo?.time} ms]: ${message.message}`so slow-query lines change from
[SLOW QUERY: 1234 ms]to e.g.[SLOW QUERY: 1234.5678901 ms]. ThemaxQueryExecutionTimecomparison stays correct; only the rendered text changes.I left it unrounded because the issue asks for a higher-resolution timer and rounding to whole milliseconds would discard exactly that. Rounding at the log boundary instead would touch
AbstractLoggerand affect every logger implementation, which felt broader than this issue's scope. Happy to add rounding at either the measurement or the log boundary if you would prefer to keep the existing log format — just say which.Pull-Request Checklist
masterbranchtests/**.test.ts) — N/A, see belowdocs/docs/**.md) — N/A, no user-facing API changeOn tests: this is a like-for-like swap of the timing source with no behavioral branch to assert, and the driver query paths need live database connections, so
test:fastdoes not reach them. If you would like coverage, the meaningful place is a logger-level assertion on the slow-query path — glad to add one if you want it in this PR.