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(opportunities): more granular score #5331

Merged
merged 2 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module.exports = [
},
},
'efficient-animated-content': {
score: 0,
score: '<0.5',
details: {
overallSavingsMs: '>2000',
items: [
Expand Down
20 changes: 16 additions & 4 deletions lighthouse-core/audits/byte-efficiency/byte-efficiency-audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict';

const Audit = require('../audit');
const linearInterpolation = require('../../lib/statistics').linearInterpolation;
const Interactive = require('../../gather/computed/metrics/lantern-interactive'); // eslint-disable-line max-len
const Simulator = require('../../lib/dependency-graph/simulator/simulator'); // eslint-disable-line no-unused-vars
const Node = require('../../lib/dependency-graph/node.js'); // eslint-disable-line no-unused-vars
Expand All @@ -16,6 +17,7 @@ const KB_IN_BYTES = 1024;

const WASTED_MS_FOR_AVERAGE = 300;
const WASTED_MS_FOR_POOR = 750;
const WASTED_MS_FOR_SCORE_OF_ZERO = 5000;

/**
* @typedef {object} ByteEfficiencyProduct
Expand All @@ -32,14 +34,24 @@ const WASTED_MS_FOR_POOR = 750;
*/
class UnusedBytes extends Audit {
/**
* Creates a score based on the wastedMs value using linear interpolation between control points.
*
* @param {number} wastedMs
* @return {number}
*/
static scoreForWastedMs(wastedMs) {
if (wastedMs === 0) return 1;
else if (wastedMs < WASTED_MS_FOR_AVERAGE) return 0.9;
else if (wastedMs < WASTED_MS_FOR_POOR) return 0.65;
else return 0;
if (wastedMs === 0) {
return 1;
} else if (wastedMs < WASTED_MS_FOR_AVERAGE) {
return linearInterpolation(0, 1, WASTED_MS_FOR_AVERAGE, 0.75, wastedMs);
} else if (wastedMs < WASTED_MS_FOR_POOR) {
return linearInterpolation(WASTED_MS_FOR_AVERAGE, 0.75, WASTED_MS_FOR_POOR, 0.5, wastedMs);
} else {
return Math.max(
0,
linearInterpolation(WASTED_MS_FOR_POOR, 0.5, WASTED_MS_FOR_SCORE_OF_ZERO, 0, wastedMs)
);
}
}

/**
Expand Down
17 changes: 2 additions & 15 deletions lighthouse-core/audits/byte-efficiency/uses-long-cache-ttl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const parseCacheControl = require('parse-cache-control');
const Audit = require('../audit');
const WebInspector = require('../../lib/web-inspector');
const URL = require('../../lib/url-shim');
const linearInterpolation = require('../../lib/statistics').linearInterpolation;

// Ignore assets that have very high likelihood of cache hit
const IGNORE_THRESHOLD_IN_PERCENT = 0.925;
Expand Down Expand Up @@ -45,20 +46,6 @@ class CacheHeaders extends Audit {
};
}

/**
* Interpolates the y value at a point x on the line defined by (x0, y0) and (x1, y1)
* @param {number} x0
* @param {number} y0
* @param {number} x1
* @param {number} y1
* @param {number} x
* @return {number}
*/
static linearInterpolation(x0, y0, x1, y1, x) {
const slope = (y1 - y0) / (x1 - x0);
return y0 + (x - x0) * slope;
}

/**
* Computes the percent likelihood that a return visit will be within the cache lifetime, based on
* Chrome UMA stats see the note below.
Expand Down Expand Up @@ -90,7 +77,7 @@ class CacheHeaders extends Audit {
const lowerDecile = (upperDecileIndex - 1) / 10;

// Approximate the real likelihood with linear interpolation
return CacheHeaders.linearInterpolation(
return linearInterpolation(
lowerDecileValue,
lowerDecile,
upperDecileValue,
Expand Down
15 changes: 15 additions & 0 deletions lighthouse-core/lib/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ function getLogNormalDistribution(median, falloff) {
};
}

/**
* Interpolates the y value at a point x on the line defined by (x0, y0) and (x1, y1)
* @param {number} x0
* @param {number} y0
* @param {number} x1
* @param {number} y1
* @param {number} x
* @return {number}
*/
function linearInterpolation(x0, y0, x1, y1, x) {
const slope = (y1 - y0) / (x1 - x0);
return y0 + (x - x0) * slope;
}

module.exports = {
linearInterpolation,
getLogNormalDistribution,
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,6 @@ describe('Cache headers audit', () => {
};
});

describe('#linearInterpolation', () => {
it('correctly interpolates when slope is 2', () => {
const slopeOf2 = x => CacheHeadersAudit.linearInterpolation(0, 0, 10, 20, x);
assert.equal(slopeOf2(-10), -20);
assert.equal(slopeOf2(5), 10);
assert.equal(slopeOf2(10), 20);
});

it('correctly interpolates when slope is 0', () => {
const slopeOf0 = x => CacheHeadersAudit.linearInterpolation(0, 0, 10, 0, x);
assert.equal(slopeOf0(-10), 0);
assert.equal(slopeOf0(5), 0);
assert.equal(slopeOf0(10), 0);
});
});

it('detects missing cache headers', () => {
networkRecords = [networkRecord()];
return CacheHeadersAudit.audit(artifacts, {options}).then(result => {
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/redirects-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Performance: Redirects audit', () => {
]);

return Audit.audit(mockArtifacts(FAILING_TWO_REDIRECTS), {}).then(output => {
assert.equal(output.score, 0.65);
assert.equal(Math.round(output.score * 100) / 100, 0.56);
assert.equal(output.details.items.length, 3);
assert.equal(Math.round(output.rawValue), 638);
});
Expand Down
60 changes: 39 additions & 21 deletions lighthouse-core/test/lib/statistics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,46 @@
const assert = require('assert');
const statistics = require('../../lib/statistics.js');

describe('log normal distribution', () => {
it('creates a log normal distribution', () => {
// This curve plotted with the below percentile assertions
// https://www.desmos.com/calculator/vjk2rwd17y
describe('statistics', () => {
describe('#getLogNormalDistribution', () => {
it('creates a log normal distribution', () => {
// This curve plotted with the below percentile assertions
// https://www.desmos.com/calculator/vjk2rwd17y

const median = 5000;
const pODM = 3500;
const distribution = statistics.getLogNormalDistribution(median, pODM);
const median = 5000;
const pODM = 3500;
const distribution = statistics.getLogNormalDistribution(median, pODM);

function getPct(distribution, value) {
return Number(distribution.computeComplementaryPercentile(value).toFixed(2));
}
assert.equal(typeof distribution.computeComplementaryPercentile, 'function');
assert.equal(getPct(distribution, 2000), 1.00, 'pct for 2000 does not match');
assert.equal(getPct(distribution, 3000), 0.98, 'pct for 3000 does not match');
assert.equal(getPct(distribution, 3500), 0.92, 'pct for 3500 does not match');
assert.equal(getPct(distribution, 4000), 0.81, 'pct for 4000 does not match');
assert.equal(getPct(distribution, 5000), 0.50, 'pct for 5000 does not match');
assert.equal(getPct(distribution, 6000), 0.24, 'pct for 6000 does not match');
assert.equal(getPct(distribution, 7000), 0.09, 'pct for 7000 does not match');
assert.equal(getPct(distribution, 8000), 0.03, 'pct for 8000 does not match');
assert.equal(getPct(distribution, 9000), 0.01, 'pct for 9000 does not match');
assert.equal(getPct(distribution, 10000), 0.00, 'pct for 10000 does not match');
function getPct(distribution, value) {
return Number(distribution.computeComplementaryPercentile(value).toFixed(2));
}
assert.equal(typeof distribution.computeComplementaryPercentile, 'function');
assert.equal(getPct(distribution, 2000), 1.00, 'pct for 2000 does not match');
assert.equal(getPct(distribution, 3000), 0.98, 'pct for 3000 does not match');
assert.equal(getPct(distribution, 3500), 0.92, 'pct for 3500 does not match');
assert.equal(getPct(distribution, 4000), 0.81, 'pct for 4000 does not match');
assert.equal(getPct(distribution, 5000), 0.50, 'pct for 5000 does not match');
assert.equal(getPct(distribution, 6000), 0.24, 'pct for 6000 does not match');
assert.equal(getPct(distribution, 7000), 0.09, 'pct for 7000 does not match');
assert.equal(getPct(distribution, 8000), 0.03, 'pct for 8000 does not match');
assert.equal(getPct(distribution, 9000), 0.01, 'pct for 9000 does not match');
assert.equal(getPct(distribution, 10000), 0.00, 'pct for 10000 does not match');
});
});

describe('#linearInterpolation', () => {
it('correctly interpolates when slope is 2', () => {
const slopeOf2 = x => statistics.linearInterpolation(0, 0, 10, 20, x);
assert.equal(slopeOf2(-10), -20);
assert.equal(slopeOf2(5), 10);
assert.equal(slopeOf2(10), 20);
});

it('correctly interpolates when slope is 0', () => {
const slopeOf0 = x => statistics.linearInterpolation(0, 0, 10, 0, x);
assert.equal(slopeOf0(-10), 0);
assert.equal(slopeOf0(5), 0);
assert.equal(slopeOf0(10), 0);
});
});
});
4 changes: 2 additions & 2 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,7 @@
"id": "render-blocking-resources",
"title": "Eliminate render-blocking resources",
"description": "Resources are blocking the first paint of your page. Consider delivering critical JS/CSS inline and deferring all non-critical JS/styles. [Learn more](https://developers.google.com/web/tools/lighthouse/audits/blocking-resources).",
"score": 0,
"score": 0.46,
"scoreDisplayMode": "numeric",
"rawValue": 1129,
"displayValue": "5 resources delayed first paint by 1129ms",
Expand Down Expand Up @@ -2067,7 +2067,7 @@
"id": "uses-text-compression",
"title": "Enable text compression",
"description": "Text-based responses should be served with compression (gzip, deflate or brotli) to minimize total network bytes. [Learn more](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/optimize-encoding-and-transfer).",
"score": 0.9,
"score": 0.88,
"scoreDisplayMode": "numeric",
"rawValue": 150,
"displayValue": [
Expand Down