Skip to content

Commit

Permalink
core(network-requests): add network requests audit (GoogleChrome#4631)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored Mar 1, 2018
1 parent 662b387 commit fb3fbc3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lighthouse-cli/test/smokehouse/perf/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ module.exports = [
// Can be flaky, so test float rawValue instead of boolean score
rawValue: '<1000',
},
'network-requests': {
details: {
items: {
length: '>5',
},
},
},
'uses-rel-preload': {
score: '<100',
rawValue: '>500',
Expand Down
73 changes: 73 additions & 0 deletions lighthouse-core/audits/network-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @license Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const Audit = require('./audit');
const URL = require('../lib/url-shim');

class NetworkRequests extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
name: 'network-requests',
informative: true,
description: 'Network Requests',
helpText: 'Lists the network requests that were made during page load.',
requiredArtifacts: ['devtoolsLogs'],
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
return artifacts.requestNetworkRecords(devtoolsLog).then(records => {
const earliestStartTime = records.reduce(
(min, record) => Math.min(min, record.startTime),
Infinity
);

const results = records.map(record => {
return {
url: URL.elideDataURI(record.url),
startTime: (record.startTime - earliestStartTime) * 1000,
endTime: (record.endTime - earliestStartTime) * 1000,
transferSize: record.transferSize,
statusCode: record.statusCode,
};
});

const headings = [
{key: 'url', itemType: 'url', text: 'URL'},
{key: 'startTime', itemType: 'ms', granularity: 1, text: 'Start Time'},
{key: 'endTime', itemType: 'ms', granularity: 1, text: 'End Time'},
{
key: 'transferSize',
itemType: 'bytes',
displayUnit: 'kb',
granularity: 1,
text: 'Transfer Size',
},
{key: 'statusCode', itemType: 'text', text: 'Status Code'},
];

const tableDetails = Audit.makeTableDetails(headings, results);

return {
score: 100,
rawValue: records.length,
extendedInfo: {value: results},
details: tableDetails,
};
});
}
}

module.exports = NetworkRequests;
2 changes: 2 additions & 0 deletions lighthouse-core/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module.exports = {
'bootup-time',
'uses-rel-preload',
'font-display',
'network-requests',
'manual/pwa-cross-browser',
'manual/pwa-page-transitions',
'manual/pwa-each-page-has-url',
Expand Down Expand Up @@ -285,6 +286,7 @@ module.exports = {
{id: 'uses-long-cache-ttl', weight: 0, group: 'perf-info'},
{id: 'dom-size', weight: 0, group: 'perf-info'},
{id: 'critical-request-chains', weight: 0, group: 'perf-info'},
{id: 'network-requests', weight: 0},
{id: 'user-timings', weight: 0, group: 'perf-info'},
{id: 'bootup-time', weight: 0, group: 'perf-info'},
{id: 'screenshot-thumbnails', weight: 0},
Expand Down
33 changes: 33 additions & 0 deletions lighthouse-core/test/audits/network-requests-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const NetworkRequests = require('../../audits/network-requests');
const Runner = require('../../runner.js');
const assert = require('assert');

const acceptableDevToolsLog = require('../fixtures/traces/progressive-app-m60.devtools.log.json');

/* eslint-env mocha */
describe('Network requests audit', () => {
it('should return network requests', () => {
const artifacts = Object.assign({
devtoolsLogs: {
[NetworkRequests.DEFAULT_PASS]: acceptableDevToolsLog,
},
}, Runner.instantiateComputedArtifacts());

return NetworkRequests.audit(artifacts).then(output => {
assert.equal(output.score, 100);
assert.equal(output.rawValue, 66);
assert.equal(output.details.items.length, 66);
assert.equal(output.extendedInfo.value[0].url, 'https://pwa.rocks/');
assert.equal(output.extendedInfo.value[0].startTime, 0);
assert.equal(output.extendedInfo.value[0].statusCode, 200);
assert.equal(output.extendedInfo.value[0].transferSize, 5368);
});
});
});

0 comments on commit fb3fbc3

Please sign in to comment.