Repository navigation
feat: capture per-phase resource timing for API journeys - #1164
Conversation
Bump Playwright to 1.62.0 and surface the new APIResponse.timing() on API journeys. Previously API monitors only recorded a coarse wall-clock `wait`/`total` with all other phases at -1 and `receive` hard-coded to 0. Since APIResponse.timing() returns the same Resource Timing shape as Request.timing(), the phase-breakdown and total logic is extracted into a shared `network-timings` helper reused by both the browser NetworkManager and the APINetworkManager, so both journey types emit consistent dns/connect/ssl/wait/receive timings. The wall-clock span remains as the fallback for HAR replay / reused sockets where phases report -1. Co-authored-by: Cursor <cursoragent@cursor.com>
|
|
||
| // Gather all resource timing information up until the | ||
| // TTFB(Time to first byte) is received | ||
| // Resource timing up to TTFB; `receive` fills in on `requestfinished`. |
There was a problem hiding this comment.
This comment is more robotic, lets reset to the previous version.
There was a problem hiding this comment.
Done, restored the original comment 👍
There was a problem hiding this comment.
This file not a plugin per se, more of a helper for timing breakdown, lets move it somewhere else not under plugins directory.
There was a problem hiding this comment.
Moved it out to src/network-timings.ts (out of the plugins dir).
|
|
||
| networkEntry.loadEndTime = epochTimeInSeconds(); | ||
| // responseEnd is fired after the last byte of the response is received. | ||
| // Full timing (incl. `receive`) is available once the response completes. |
There was a problem hiding this comment.
same I prefer the old comment.
There was a problem hiding this comment.
Reverted this one too.
| * between `requestSentTime` and `loadEndTime` when Resource Timing data is | ||
| * unavailable (`startTime <= 0`, e.g. HAR replay or aborted requests). | ||
| */ | ||
| export function calcTotalTime( |
There was a problem hiding this comment.
maybe we can change it to a pure func and return a separate object instead of overwiting the timing?
There was a problem hiding this comment.
Done — made it pure, it returns total now and the caller assigns it onto timings.total.
| const request = this.driver.request; | ||
| this._originalFetch = request.fetch.bind(request); | ||
| (request as any).fetch = (urlOrRequest: RequestLike, options?: any) => | ||
| this._interceptRequest(urlOrRequest, options); |
There was a problem hiding this comment.
I didnt review the piece that added this, any reason why we didnt use the Events like ApiRequest.Request and RequestFinished ? Wasnt that possible?
There was a problem hiding this comment.
PW don't expose those unfortunately
- Move the timing-breakdown helper out of plugins/ (it is not a plugin) to src/network-timings.ts - Make calcTotalTime a pure function that returns total instead of mutating timings; callers assign the result - Restore the original TTFB / responseEnd comments in network.ts Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
mcapell
left a comment
There was a problem hiding this comment.
LGTM, added a small comment
| ? roundMilliSecs(rtiming.connectEnd - rtiming.connectStart) | ||
| : -1; | ||
| const ssl = | ||
| rtiming.secureConnectionStart !== -1 |
There was a problem hiding this comment.
Should we check rtiming.connectEnd as well? something like:
| rtiming.secureConnectionStart !== -1 | |
| rtiming.secureConnectionStart !== -1 && rtiming.connectEnd !== -1 |
There was a problem hiding this comment.
I see it's guarded here: https://github.com/elastic/synthetics/pull/1164/changes#diff-8191a864bcd614940da81a1dbf38c09856ee074f07fc648eb3c47bcce77fc748R125 -- feel free to ignore it
|
🎉 This PR is included in version 1.25.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
Reuses Playwright's newly released
APIResponse.timing()(shipped in 1.62.0) to give API journeys the same per-phase network timing breakdown that browser journeys already have.1.61.1→1.62.0(playwright,playwright-chromium,playwright-core).src/plugins/network-timings.ts— extracts the Resource Timing → phase-breakdown (getResourceTimings) and total calculation with wall-clock fallback (calcTotalTime). SinceAPIResponse.timing()returns the identical shape asRequest.timing(), bothNetworkManager(browser) andAPINetworkManager(API) now share exactly the same math.api-network.ts): replaced the coarse wall-clock stopwatch — which leftblocked/dns/ssl/connectat-1and hard-codedreceive: 0— with a real breakdown fromresponse.timing(). Phases that don't happen (reused keep-alive socket) or are unknown (HAR replay) still report-1, andcalcTotalTimefalls back to the wall-clock span in that case.network.ts): refactored to call the shared helper (removing the duplicated inline math and_calcTotalTime); behavior unchanged.Before vs after (API journeys)
-1APIResponse.timing()0Test plan
network-timings.test.ts— deterministic unit tests: full HTTPS timing, reused-socket/cached-DNS (-1phases), HAR-replay wall-clock fallback.api-network.test.ts— asserts per-phase timings are populated fromAPIResponse.timing().options.test.ts— updated the hard-coded device user-agent that shifted with the Playwright bump (Chrome/149…→151…).tsc --noEmit, ESLint, Prettier clean.Made with Cursor