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-recorder): optimize network idle detection #9712

Merged
merged 2 commits into from
Sep 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 18 additions & 7 deletions lighthouse-core/lib/network-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,35 @@ class NetworkRecorder extends EventEmitter {
}

isIdle() {
return !!this._getActiveIdlePeriod(0);
return this._isActiveIdlePeriod(0);
}

is2Idle() {
return !!this._getActiveIdlePeriod(2);
return this._isActiveIdlePeriod(2);
}

/**
* Returns whether the number of currently inflight requests is less than or
* equal to the number of allowed concurrent requests.
* @param {number} allowedRequests
* @return {boolean}
*/
_getActiveIdlePeriod(allowedRequests) {
const quietPeriods = NetworkRecorder.findNetworkQuietPeriods(this._records, allowedRequests);
return quietPeriods.find(period => !Number.isFinite(period.end));
_isActiveIdlePeriod(allowedRequests) {
let inflightRequests = 0;

for (let i = 0; i < this._records.length; i++) {
const record = this._records[i];
if (NetworkRecorder.isNetworkRecordFinished(record)) continue;
if (IGNORED_NETWORK_SCHEMES.includes(record.parsedURL.scheme)) continue;
inflightRequests++;
}

return inflightRequests <= allowedRequests;
}

_emitNetworkStatus() {
const zeroQuiet = this._getActiveIdlePeriod(0);
const twoQuiet = this._getActiveIdlePeriod(2);
const zeroQuiet = this.isIdle();
const twoQuiet = this.is2Idle();

if (twoQuiet && zeroQuiet) {
log.verbose('NetworkRecorder', 'network fully-quiet');
Expand Down