forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(network-requests): add network requests audit (GoogleChrome#4631)
- Loading branch information
1 parent
662b387
commit fb3fbc3
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |