Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(network-monitor): treat EventSource as non-critical #16225

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading