fix(har): account for socket reuse

This commit is contained in:
Simon Knott 2024-10-14 11:59:24 +02:00
parent 0a63427c77
commit 15414fb59e
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 24 additions and 2 deletions

View file

@ -329,6 +329,11 @@ export abstract class APIRequestContext extends SdkObject {
blocked: -1, blocked: -1,
}; };
if (request.reusedSocket) {
timings.connect = -1;
timings.dns = -1;
}
const requestFinishedEvent: APIRequestFinishedEvent = { const requestFinishedEvent: APIRequestFinishedEvent = {
requestEvent, requestEvent,
httpVersion: response.httpVersion, httpVersion: response.httpVersion,
@ -491,8 +496,13 @@ export abstract class APIRequestContext extends SdkObject {
request.on('socket', socket => { request.on('socket', socket => {
// happy eyeballs don't emit lookup and connect events, so we use our custom ones // happy eyeballs don't emit lookup and connect events, so we use our custom ones
const happyEyeBallsTimings = timingForSocket(socket); const happyEyeBallsTimings = timingForSocket(socket);
dnsLookupAt = happyEyeBallsTimings.dnsLookupAt; if (request.reusedSocket) {
tcpConnectionAt ??= happyEyeBallsTimings.tcpConnectionAt; dnsLookupAt = startAt;
tcpConnectionAt = startAt;
} else {
dnsLookupAt = happyEyeBallsTimings.dnsLookupAt;
tcpConnectionAt ??= happyEyeBallsTimings.tcpConnectionAt;
}
// non-happy-eyeballs sockets // non-happy-eyeballs sockets
listeners.push( listeners.push(

View file

@ -877,6 +877,18 @@ it('should include timings when using socks proxy', async ({ contextFactory, ser
expect(log.entries[0].timings.connect).toBeGreaterThan(0); expect(log.entries[0].timings.connect).toBeGreaterThan(0);
}); });
it('should not have connect and dns timings when socket is reused', async ({ contextFactory, server }, testInfo) => {
const { page, getLog } = await pageWithHar(contextFactory, testInfo);
await page.request.get(server.EMPTY_PAGE);
await page.request.get(server.EMPTY_PAGE);
const log = await getLog();
expect(log.entries).toHaveLength(2);
const request2 = log.entries[1];
expect.soft(request2.timings.connect).toBe(-1);
expect.soft(request2.timings.dns).toBe(-1);
});
it('should include redirects from API request', async ({ contextFactory, server }, testInfo) => { it('should include redirects from API request', async ({ contextFactory, server }, testInfo) => {
server.setRedirect('/redirect-me', '/simple.json'); server.setRedirect('/redirect-me', '/simple.json');
const { page, getLog } = await pageWithHar(contextFactory, testInfo); const { page, getLog } = await pageWithHar(contextFactory, testInfo);