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

report(redirects): reformat results, incl all requests and wasted time, #3492

Merged
merged 4 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions lighthouse-cli/test/smokehouse/redirects/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = [
rawValue: '>=500',
details: {
items: {
length: 2,
length: 3,
},
},
},
Expand All @@ -32,10 +32,10 @@ module.exports = [
audits: {
'redirects': {
score: 100,
rawValue: 0,
rawValue: '>=250',
details: {
items: {
length: 1,
length: 2,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/audits/dobetterweb/geolocation-on-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class GeolocationOnStart extends ViolationAudit {
* @return {!AuditResult}
*/
static audit(artifacts) {
// 'Only request geolocation information in response to a user gesture.'
const results = ViolationAudit.getViolationResults(artifacts, /geolocation/);

const headings = [
Expand Down
35 changes: 21 additions & 14 deletions lighthouse-core/audits/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,45 @@ class Redirects extends Audit {
.then(mainResource => {
// redirects is only available when redirects happens
const redirectRequests = Array.from(mainResource.redirects || []);

// add main resource to redirectRequests so we can use it to calculate wastedMs
redirectRequests.push(mainResource);
let totalWastedMs = 0;

let totalWastedMs = 0;
const pageRedirects = [];

// Kickoff the results table (with the initial request) if there are > 1 redirects
if (redirectRequests.length > 1) {
pageRedirects.push({
url: `(Initial: ${redirectRequests[0].url})`,
wastedMs: 'n/a',
});
}

for (let i = 1; i < redirectRequests.length; i++) {
const request = redirectRequests[i - 1];
const nextRequest = redirectRequests[i];
const wastedMs = (nextRequest.startTime - request.startTime) * 1000;
const initialRequest = redirectRequests[i - 1];
const redirectedRequest = redirectRequests[i];

// We skip the first redirect in our calculations but show it in the table below.
// We allow 1 redirect (www. => m.)
if (i > 1) {
totalWastedMs += wastedMs;
}
const wastedMs = (redirectedRequest.startTime - initialRequest.startTime) * 1000;
totalWastedMs += wastedMs;

pageRedirects.push({
url: request.url,
wastedMs: Util.formatMilliseconds(wastedMs),
url: redirectedRequest.url,
wastedMs: Util.formatMilliseconds(wastedMs, 1),
});
}

const headings = [
{key: 'url', itemType: 'text', text: 'URL'},
{key: 'url', itemType: 'text', text: 'Redirected URL'},
{key: 'wastedMs', itemType: 'text', text: 'Time for Redirect'},
];
const details = Audit.makeTableDetails(headings, pageRedirects);

return {
score: UnusedBytes.scoreForWastedMs(totalWastedMs),
// We award a passing grade if you only have 1 redirect
score: redirectRequests.length === 2 ? 100 : UnusedBytes.scoreForWastedMs(totalWastedMs),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if totalWastedMs === 0 scoreForWastedMs doesn't already give you a 100?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new logic you can get totalWastedMs > 0 and still get a passing grade. Take http://www.lisairish.com which i was seeing it take 500ms to redirect to https://www.lisairish.com. We should still tell them about the 500ms even if we pass them.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a passing test case for a resource with 0 redirects too? I had to pause for a second on this, alternatively switch to redirectRequests.length <= 2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already have passing test case for 0 redirects.
changed to <= 2.

rawValue: totalWastedMs,
displayValue: Util.formatMilliseconds(totalWastedMs),
displayValue: Util.formatMilliseconds(totalWastedMs, 1),
extendedInfo: {
value: {
wastedMs: totalWastedMs,
Expand Down
58 changes: 40 additions & 18 deletions lighthouse-core/test/audits/redirects-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,54 @@ const Audit = require('../../audits/redirects.js');
const assert = require('assert');

/* eslint-env mocha */
const FAILING_REDIRECTS = {
const FAILING_THREE_REDIRECTS = {
startTime: 17,
url: 'http://exampel.com/',
Copy link
Collaborator

@wardpeet wardpeet Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo example.com?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was deliberate. :)

redirects: [
{
endTime: 1,
responseReceivedTime: 5,
startTime: 0,
url: 'http://example.com/',
},
{
endTime: 16,
responseReceivedTime: 14,
startTime: 11,
url: 'https://example.com/',
},
{
endTime: 17,
responseReceivedTime: 15,
startTime: 12,
url: 'https://m.example.com/',
},
],
};

const FAILING_TWO_REDIRECTS = {
startTime: 446.286,
url: 'http://lisairish.com/',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps better to just use example.com as an url so it's consistent with the rest?

Or is this a marketing stunt for your mom's website? 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well i tested this with these actual URLs, which have this set of redirects setup.

redirects: [
{
startTime: 445.648,
url: 'https://lisairish.com/',
},
{
startTime: 445.757,
url: 'https://www.lisairish.com/',
},
],
};

const SUCCESS_ONE_REDIRECT = {
startTime: 0.7,
startTime: 136.383,
url: 'https://lisairish.com/',
redirects: [{
endTime: 0.7,
responseReceivedTime: 5,
startTime: 0,
url: 'https://example.com/',
startTime: 135.873,
url: 'https://www.lisairish.com/',
}],
};

const SUCCESS_NOREDIRECT = {};
const SUCCESS_NOREDIRECT = {
startTime: 135.873,
url: 'https://www.google.com/',
redirects: [],
};

const mockArtifacts = (mockChain) => {
return {
Expand All @@ -60,19 +73,28 @@ const mockArtifacts = (mockChain) => {
};

describe('Performance: Redirects audit', () => {
it('fails when more than one redirect detected', () => {
return Audit.audit(mockArtifacts(FAILING_REDIRECTS)).then(output => {
it('fails when 3 redirects detected', () => {
return Audit.audit(mockArtifacts(FAILING_THREE_REDIRECTS)).then(output => {
assert.equal(output.score, 0);
assert.equal(output.details.items.length, 4);
assert.equal(output.rawValue, 17000);
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add newline

it('fails when 2 redirects detected', () => {
return Audit.audit(mockArtifacts(FAILING_TWO_REDIRECTS)).then(output => {
assert.equal(output.score, 65);
assert.equal(output.details.items.length, 3);
assert.equal(output.rawValue, 6000);
assert.equal(Math.round(output.rawValue), 638);
});
});

it('passes when one redirect detected', () => {
return Audit.audit(mockArtifacts(SUCCESS_ONE_REDIRECT)).then(output => {
// If === 1 redirect, perfect score is expected, regardless of latency
assert.equal(output.score, 100);
assert.equal(output.details.items.length, 1);
assert.equal(output.rawValue, 0);
// We will still generate a table and show wasted time
assert.equal(output.details.items.length, 2);
assert.equal(Math.round(output.rawValue), 510);
});
});

Expand Down