Repository: vercel/next.js
Pull request: Development: Use process.hrtime for request time calculation

## What?

Instead of using `Date.now()` to compare timing uses the more appropriate `process.hrtime.bigint()` 

Closes PACK-5689

Changed files:
filename: packages/next/src/build/duration-to-string.ts
patch: @@ -17,6 +17,18 @@ export function hrtimeToSeconds(hrtime: [number, number]): number {
   return hrtime[0] + hrtime[1] / 1e9
 }
 
+function nanosecondsBigIntToSeconds(nanoseconds: bigint): number {
+  return Number(nanoseconds) / 1000000000
+}
+
+function hrtimeBigIntToSeconds(hrtime: bigint): number {
+  return nanosecondsBigIntToSeconds(hrtime)
+}
+
+export function hrtimeBigIntDurationToString(hrtime: bigint) {
+  return durationToString(hrtimeBigIntToSeconds(hrtime))
+}
+
 export function hrtimeDurationToString(hrtime: [number, number]): string {
   return durationToString(hrtimeToSeconds(hrtime))
 }

filename: packages/next/src/server/dev/log-requests.ts
patch: @@ -1,3 +1,4 @@
+import { hrtimeBigIntDurationToString } from '../../build/duration-to-string'
 import {
   blue,
   bold,
@@ -13,13 +14,6 @@ import type { NodeNextRequest, NodeNextResponse } from '../base-http/node'
 import type { LoggingConfig } from '../config-shared'
 import { getRequestMeta } from '../request-meta'
 
-export interface RequestLoggingOptions {
-  readonly request: NodeNextRequest
-  readonly response: NodeNextResponse
-  readonly loggingConfig: LoggingConfig | undefined
-  readonly requestDurationInMs: number
-}
-
 /**
  * Returns true if the incoming request should be ignored for logging.
  */
@@ -44,15 +38,20 @@ export function ignoreLoggingIncomingRequests(
   return ignore.some((pattern) => pattern.test(request.url))
 }
 
-export function logRequests(options: RequestLoggingOptions): void {
-  const { request, response, loggingConfig, requestDurationInMs } = options
-
+export function logRequests(
+  request: NodeNextRequest,
+  response: NodeNextResponse,
+  loggingConfig: LoggingConfig,
+  requestStartTime: bigint,
+  requestEndTime: bigint
+): void {
   if (!ignoreLoggingIncomingRequests(request, loggingConfig)) {
-    logIncomingRequests({
+    logIncomingRequests(
       request,
-      requestDurationInMs,
-      statusCode: response.statusCode,
-    })
+      requestStartTime,
+      requestEndTime,
+      response.statusCode
+    )
   }
 
   if (request.fetchMetrics) {
@@ -62,14 +61,12 @@ export function logRequests(options: RequestLoggingOptions): void {
   }
 }
 
-interface IncomingRequestOptions {
-  readonly request: NodeNextRequest
-  readonly requestDurationInMs: number
-  readonly statusCode: number
-}
-
-function logIncomingRequests(options: IncomingRequestOptions): void {
-  const { request, requestDurationInMs, statusCode } = options
+function logIncomingRequests(
+  request: NodeNextRequest,
+  requestStartTime: bigint,
+  requestEndTime: bigint,
+  statusCode: number
+): void {
   const isRSC = getRequestMeta(request, 'isRSCRequest')
   const url = isRSC ? stripNextRscUnionQuery(request.url) : request.url
 
@@ -87,7 +84,7 @@ function logIncomingRequests(options: IncomingRequestOptions): void {
   const coloredStatus = statusCodeColor(statusCode.toString())
 
   return writeLine(
-    `${request.method} ${url} ${coloredStatus} in ${requestDurationInMs}ms`
+    `${request.method} ${url} ${coloredStatus} in ${hrtimeBigIntDurationToString(requestEndTime - requestStartTime)}`
   )
 }
 

filename: packages/next/src/server/dev/next-dev-server.ts
patch: @@ -466,7 +466,7 @@ export default class DevServer extends Server {
       const loggingConfig = this.nextConfig.logging
 
       if (loggingConfig !== false) {
-        const start = Date.now()
+        const requestStart = process.hrtime.bigint()
         const isMiddlewareRequest = getRequestMeta(req, 'middlewareInvoke')
 
         if (!isMiddlewareRequest) {
@@ -480,12 +480,14 @@ export default class DevServer extends Server {
               return
             }
 
-            logRequests({
+            const requestEnd = process.hrtime.bigint()
+            logRequests(
               request,
               response,
               loggingConfig,
-              requestDurationInMs: Date.now() - start,
-            })
+              requestStart,
+              requestEnd
+            )
           })
         }
       }