diff --git a/cloud-function/src/handlers/render-html.ts b/cloud-function/src/handlers/render-html.ts index 7401b85f6ba6..0533feb101a2 100644 --- a/cloud-function/src/handlers/render-html.ts +++ b/cloud-function/src/handlers/render-html.ts @@ -18,6 +18,7 @@ export async function renderIndexHTML( next: NextFunction ) { if (req.url.endsWith("/index.html")) { + req.startServerTiming("totalSSR"); const html = await renderHTMLForContext( req, res, @@ -55,9 +56,7 @@ export async function renderHTMLForContext( } try { - req.startServerTiming("addHeaders"); withRenderedContentResponseHeaders(req, res); - req.endServerTiming("addHeaders"); req.startServerTiming("renderHTML"); const html = renderHTML(context); req.endServerTiming("renderHTML"); diff --git a/cloud-function/src/middlewares/server-timing.ts b/cloud-function/src/middlewares/server-timing.ts index 0918c09aa483..7e3ee68588a7 100644 --- a/cloud-function/src/middlewares/server-timing.ts +++ b/cloud-function/src/middlewares/server-timing.ts @@ -8,28 +8,27 @@ export async function addServerTimingHeaders( res: Response, next: NextFunction ) { - const timers = new Map< - string, - { finished: boolean; hrtime: [number, number] } - >(); + const timers = new Map(); req.startServerTiming = (id: string) => { - timers.set(id, { finished: false, hrtime: hrtime() }); + timers.set(id, { start: hrtime.bigint() }); }; + req.endServerTiming = (id: string) => { - timers.set(id, { finished: true, hrtime: hrtime(timers.get(id)?.hrtime) }); + const start = timers.get(id)?.start; + if (start !== undefined) { + timers.set(id, { start, diff: hrtime.bigint() - start }); + } }; req.startServerTiming("total"); onHeaders(res, () => { - req.endServerTiming("total"); const header = [...timers] - .filter(([, { finished }]) => finished) - .map( - ([id, { hrtime }]) => - `${id};dur=${((hrtime[0] * 1e9 + hrtime[1]) / 1e6).toFixed(0)}` - ) + .map(([id, { start, diff }]) => { + const time = diff !== undefined ? diff : hrtime.bigint() - start; + return `${id};dur=${time / BigInt(1e6)}`; + }) .join(", "); res.setHeader("Server-Timing", header); });