Skip to content

Commit

Permalink
core(network-monitor): treat EventSource as non-critical (#16225)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish authored Oct 15, 2024
1 parent 6ff3ef0 commit 0da3e1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 8 additions & 5 deletions core/gather/driver/network-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class NetworkMonitor extends NetworkMonitorEventEmitter {
* Returns whether the network is completely idle (i.e. there are 0 inflight network requests).
*/
isIdle() {
return this._isActiveIdlePeriod(0);
return this._isIdlePeriod(0);
}

/**
Expand All @@ -144,10 +144,13 @@ class NetworkMonitor extends NetworkMonitorEventEmitter {
const rootFrameRequest = requests.find(r => r.resourceType === 'Document');
const rootFrameId = rootFrameRequest?.frameId;

return this._isActiveIdlePeriod(
return this._isIdlePeriod(
0,
// Return true if it should be a candidate for critical.
request =>
request.frameId === rootFrameId &&
// WebSocket and Server-sent Events are typically long-lived and shouldn't be considered critical.
request.resourceType !== 'WebSocket' && request.resourceType !== 'EventSource' &&
(request.priority === 'VeryHigh' || request.priority === 'High')
);
}
Expand All @@ -156,7 +159,7 @@ class NetworkMonitor extends NetworkMonitorEventEmitter {
* Returns whether the network is semi-idle (i.e. there are 2 or fewer inflight network requests).
*/
is2Idle() {
return this._isActiveIdlePeriod(2);
return this._isIdlePeriod(2);
}

/**
Expand All @@ -166,15 +169,15 @@ class NetworkMonitor extends NetworkMonitorEventEmitter {
* @param {(request: NetworkRequest) => boolean} [requestFilter]
* @return {boolean}
*/
_isActiveIdlePeriod(allowedRequests, requestFilter) {
_isIdlePeriod(allowedRequests, requestFilter) {
if (!this._networkRecorder) return false;
const requests = this._networkRecorder.getRawRecords();
let inflightRequests = 0;

for (let i = 0; i < requests.length; i++) {
const request = requests[i];
if (request.finished) continue;
if (requestFilter && !requestFilter(request)) continue;
if (requestFilter?.(request) === false) continue;
if (NetworkRequest.isNonNetworkRequest(request)) continue;
inflightRequests++;
}
Expand Down
16 changes: 16 additions & 0 deletions core/test/gather/driver/network-monitor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,22 @@ describe('NetworkMonitor', () => {
expect(monitor.is2Idle()).toBe(false);
expect(monitor.isCriticalIdle()).toBe(true);
});

it('should treat longlived stuff as noncritical', () => {
const messages = networkRecordsToDevtoolsLog([
// WebSockets usually dont have a priority on them. SSE usually is a 'High'
{url: 'http://example.com/ws', priority: undefined, requestId: `314.1`, resourceType: 'WebSocket'},
{url: 'http://example.com/sse', priority: 'High', requestId: `314.2`, resourceType: 'EventSource'},
], {skipVerification: true}).filter(event => event.method === 'Network.requestWillBeSent');

for (const message of messages) {
rootDispatch(message);
}

expect(monitor.isCriticalIdle()).toBe(true);
expect(monitor.isIdle()).toBe(false);
expect(monitor.is2Idle()).toBe(true);
});
});

describe('#findNetworkQuietPeriods', () => {
Expand Down

0 comments on commit 0da3e1d

Please sign in to comment.