Skip to content

Commit

Permalink
core(network-request): loosen lightrider timing checksum (#15330)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Aug 3, 2023
1 parent d4dd645 commit 81389e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 12 additions & 3 deletions core/lib/network-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ class NetworkRequest {
// Bail if there was no totalTime.
if (!totalHeader) return;

const totalMs = parseInt(totalHeader.value);
let totalMs = parseInt(totalHeader.value);
const TCPMsHeader = this.responseHeaders.find(item => item.name === HEADER_TCP);
const SSLMsHeader = this.responseHeaders.find(item => item.name === HEADER_SSL);
const requestMsHeader = this.responseHeaders.find(item => item.name === HEADER_REQ);
Expand All @@ -502,11 +502,20 @@ class NetworkRequest {
const requestMs = requestMsHeader ? Math.max(0, parseInt(requestMsHeader.value)) : 0;
const responseMs = responseMsHeader ? Math.max(0, parseInt(responseMsHeader.value)) : 0;

// Bail if the timings don't add up.
if (TCPMs + requestMs + responseMs !== totalMs) {
if (Number.isNaN(TCPMs + requestMs + responseMs + totalMs)) {
return;
}

// If things don't add up, tweak the total a bit.
if (TCPMs + requestMs + responseMs !== totalMs) {
const delta = Math.abs(TCPMs + requestMs + responseMs - totalMs);
// We didn't see total being more than 5ms less than the total of the components.
// Allow some discrepancy in the timing, but not too much.
if (delta >= 25) return;

totalMs = TCPMs + requestMs + responseMs;
}

// Bail if SSL time is > TCP time.
if (SSLMs > TCPMs) {
return;
Expand Down
16 changes: 15 additions & 1 deletion core/test/lib/network-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('NetworkRequest', () => {
expect(record.lrStatistics).toStrictEqual(undefined);
});

it('does nothing if header timings do not add up', () => {
it('accepts if header timings only kinda do not add up', () => {
const req = getRequest();
const tcpHeader = req.responseHeaders[1];
expect(tcpHeader.name).toStrictEqual(NetworkRequest.HEADER_TCP);
Expand All @@ -193,6 +193,20 @@ describe('NetworkRequest', () => {
global.isLightrider = true;
const record = NetworkRecorder.recordsFromLogs(devtoolsLog)[0];

expect(record).toMatchObject(req);
expect(record.lrStatistics).not.toStrictEqual(undefined);
});

it('does nothing if header timings _really_ do not add up', () => {
const req = getRequest();
const tcpHeader = req.responseHeaders[1];
expect(tcpHeader.name).toStrictEqual(NetworkRequest.HEADER_TCP);
tcpHeader.value = '8000';

const devtoolsLog = networkRecordsToDevtoolsLog([req]);
global.isLightrider = true;
const record = NetworkRecorder.recordsFromLogs(devtoolsLog)[0];

expect(record).toMatchObject(req);
expect(record.lrStatistics).toStrictEqual(undefined);
});
Expand Down

0 comments on commit 81389e9

Please sign in to comment.