Skip to content

Commit

Permalink
core(network-analyzer): more distrustful of chrome connection info (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored Mar 21, 2018
1 parent dc3c21d commit 1c23205
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
22 changes: 19 additions & 3 deletions lighthouse-core/lib/dependency-graph/simulator/network-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ class NetworkAnalyzer {
});
}

/**
* @param {LH.NetworkRequest[]} records
* @return {boolean}
*/
static canTrustConnectionInformation(records) {
const connectionIdWasStarted = new Map();
for (const record of records) {
const started = connectionIdWasStarted.get(record.connectionId) || !record.connectionReused;
connectionIdWasStarted.set(record.connectionId, started);
}

// We probably can't trust the network information if all the connection IDs were the same
if (connectionIdWasStarted.size <= 1) return false;
// Or if there were connections that were always reused (a connection had to have started at some point)
return Array.from(connectionIdWasStarted.values()).every(started => started);
}

/**
* Returns a map of requestId -> connectionReused, estimating the information if the information
* available in the records themselves appears untrustworthy.
Expand All @@ -196,9 +213,8 @@ class NetworkAnalyzer {
static estimateIfConnectionWasReused(records, options) {
options = Object.assign({forceCoarseEstimates: false}, options);

const connectionIds = new Set(records.map(record => record.connectionId));
// If the records actually have distinct connectionIds we can reuse these.
if (!options.forceCoarseEstimates && connectionIds.size > 1) {
// Check if we can trust the connection information coming from the protocol
if (!options.forceCoarseEstimates && NetworkAnalyzer.canTrustConnectionInformation(records)) {
// @ts-ignore
return new Map(records.map(record => [record.requestId, !!record.connectionReused]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('DependencyGraph/Simulator/NetworkAnalyzer', () => {
assert.deepStrictEqual(result, expected);
});

it('should estimate values when not trustworthy', () => {
it('should estimate values when not trustworthy (duplicate IDs)', () => {
const records = [
createRecord({requestId: 1, startTime: 0, endTime: 15}),
createRecord({requestId: 2, startTime: 10, endTime: 25}),
Expand All @@ -80,6 +80,43 @@ describe('DependencyGraph/Simulator/NetworkAnalyzer', () => {
assert.deepStrictEqual(result, expected);
});

it('should estimate values when not trustworthy (connectionReused nonsense)', () => {
const records = [
createRecord({
requestId: 1,
connectionId: 1,
connectionReused: true,
startTime: 0,
endTime: 15,
}),
createRecord({
requestId: 2,
connectionId: 1,
connectionReused: true,
startTime: 10,
endTime: 25,
}),
createRecord({
requestId: 3,
connectionId: 1,
connectionReused: true,
startTime: 20,
endTime: 40,
}),
createRecord({
requestId: 4,
connectionId: 2,
connectionReused: false,
startTime: 30,
endTime: 40,
}),
];

const result = NetworkAnalyzer.estimateIfConnectionWasReused(records);
const expected = new Map([[1, false], [2, false], [3, true], [4, true]]);
assert.deepStrictEqual(result, expected);
});

it('should estimate with earliest allowed reuse', () => {
const records = [
createRecord({requestId: 1, startTime: 0, endTime: 40}),
Expand Down Expand Up @@ -134,6 +171,23 @@ describe('DependencyGraph/Simulator/NetworkAnalyzer', () => {
assert.deepStrictEqual(result.get('https://example.com'), expected);
});

it('should handle untrustworthy connection information', () => {
const timing = {sendStart: 100};
const recordA = createRecord({startTime: 0, endTime: 1, timing, connectionReused: true});
const recordB = createRecord({
startTime: 0,
endTime: 1,
timing,
connectionId: 2,
connectionReused: true,
});
const result = NetworkAnalyzer.estimateRTTByOrigin([recordA, recordB], {
coarseEstimateMultiplier: 1,
});
const expected = {min: 50, max: 50, avg: 50, median: 50};
assert.deepStrictEqual(result.get('https://example.com'), expected);
});

it('should work on a real devtoolsLog', () => {
return computedArtifacts.requestNetworkRecords(devtoolsLog).then(records => {
records.forEach(addOriginToRecord);
Expand Down
7 changes: 1 addition & 6 deletions lighthouse-extension/test/extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,7 @@ describe('Lighthouse chrome extension', function() {
}

const auditErrors = await extensionPage.$$eval('.lh-debug', getDebugStrings, selectors);
const errors = auditErrors.filter(
item =>
item.debugString.includes('Audit error:') &&
// FIXME(phulce): fix timing failing on travis.
!item.debugString.includes('No timing information available')
);
const errors = auditErrors.filter(item => item.debugString.includes('Audit error:'));
assert.deepStrictEqual(errors, [], 'Audit errors found within the report');
});
});

0 comments on commit 1c23205

Please sign in to comment.