Skip to content

Commit

Permalink
report(util): fix formatDuration, add tests (GoogleChrome#5023)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored and kdzwinel committed Aug 16, 2018
1 parent 1982548 commit 2573e82
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 37 deletions.
6 changes: 3 additions & 3 deletions lighthouse-core/audits/byte-efficiency/uses-long-cache-ttl.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ class CacheHeaders extends Audit {
results.push({
url,
cacheControl,
cacheLifetimeInSeconds,
cacheLifetimeMs: cacheLifetimeInSeconds * 1000,
cacheHitProbability,
totalBytes,
wastedBytes,
});
}

results.sort(
(a, b) => a.cacheLifetimeInSeconds - b.cacheLifetimeInSeconds || b.totalBytes - a.totalBytes
(a, b) => a.cacheLifetimeMs - b.cacheLifetimeMs || b.totalBytes - a.totalBytes
);

const score = Audit.computeLogNormalScore(
Expand All @@ -221,7 +221,7 @@ class CacheHeaders extends Audit {

const headings = [
{key: 'url', itemType: 'url', text: 'URL'},
{key: 'cacheLifetimeInSeconds', itemType: 'ms', text: 'Cache TTL', displayUnit: 'duration'},
{key: 'cacheLifetimeMs', itemType: 'ms', text: 'Cache TTL', displayUnit: 'duration'},
{key: 'totalBytes', itemType: 'bytes', text: 'Size (KB)', displayUnit: 'kb',
granularity: 1},
];
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/report/v2/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Util {
* @return {string}
*/
static formatDuration(timeInMilliseconds) {
let timeInSeconds = timeInMilliseconds * 1000;
let timeInSeconds = timeInMilliseconds / 1000;
if (Math.round(timeInSeconds) === 0) {
return 'None';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Cache headers audit', () => {
return CacheHeadersAudit.audit(artifacts, {options}).then(result => {
const items = result.extendedInfo.value.results;
assert.equal(items.length, 1);
assert.equal(items[0].cacheLifetimeInSeconds, 0);
assert.equal(items[0].cacheLifetimeMs, 0);
assert.equal(items[0].wastedBytes, 10000);
assert.equal(result.displayValue, '1 asset found');
});
Expand All @@ -77,12 +77,12 @@ describe('Cache headers audit', () => {
return CacheHeadersAudit.audit(artifacts, {options}).then(result => {
const items = result.details.items;
assert.equal(items.length, 3);
assert.equal(items[0].cacheLifetimeInSeconds, 3600);
assert.equal(items[0].cacheLifetimeMs, 3600 * 1000);
assert.equal(items[0].cacheHitProbability, 0.2);
assert.equal(Math.round(items[0].wastedBytes), 80000);
assert.equal(items[1].cacheLifetimeInSeconds, 3600);
assert.equal(items[1].cacheLifetimeMs, 3600 * 1000);
assert.equal(Math.round(items[1].wastedBytes), 8000);
assert.equal(items[2].cacheLifetimeInSeconds, 86400);
assert.equal(items[2].cacheLifetimeMs, 86400 * 1000);
assert.equal(Math.round(items[2].wastedBytes), 4000);
assert.equal(result.displayValue, '3 assets found');
});
Expand All @@ -102,11 +102,11 @@ describe('Cache headers audit', () => {
return CacheHeadersAudit.audit(artifacts, {options}).then(result => {
const items = result.extendedInfo.value.results;
assert.equal(items.length, 3);
closeEnough(items[0].cacheLifetimeInSeconds, 3600);
closeEnough(items[0].cacheLifetimeMs, 3600 * 1000);
assert.equal(Math.round(items[0].wastedBytes), 8000);
closeEnough(items[1].cacheLifetimeInSeconds, 86400);
closeEnough(items[1].cacheLifetimeMs, 86400 * 1000);
assert.equal(Math.round(items[1].wastedBytes), 4000);
closeEnough(items[2].cacheLifetimeInSeconds, 86400 * 90);
closeEnough(items[2].cacheLifetimeMs, 86400 * 90 * 1000);
assert.equal(Math.round(items[2].wastedBytes), 768);
});
});
Expand All @@ -128,9 +128,9 @@ describe('Cache headers audit', () => {
return CacheHeadersAudit.audit(artifacts, {options}).then(result => {
const items = result.extendedInfo.value.results;
assert.equal(items.length, 2);
assert.ok(Math.abs(items[0].cacheLifetimeInSeconds - 3600) <= 1, 'invalid expires parsing');
assert.ok(Math.abs(items[0].cacheLifetimeMs - 3600 * 1000) <= 1, 'invalid expires parsing');
assert.equal(Math.round(items[0].wastedBytes), 8000);
assert.ok(Math.abs(items[1].cacheLifetimeInSeconds - 86400) <= 1, 'invalid expires parsing');
assert.ok(Math.abs(items[1].cacheLifetimeMs - 86400 * 1000) <= 1, 'invalid expires parsing');
assert.equal(Math.round(items[1].wastedBytes), 4000);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const assert = require('assert');
const Util = require('../../../../report/v2/renderer/util.js');
const NBSP = '\xa0';

/* eslint-env mocha */

Expand All @@ -26,6 +27,23 @@ describe('util helpers', () => {
);
});

it('formats bytes', () => {
assert.equal(Util.formatBytesToKB(100), `0.1${NBSP}KB`);
assert.equal(Util.formatBytesToKB(2000), `2${NBSP}KB`);
assert.equal(Util.formatBytesToKB(1014 * 1024), `1,014${NBSP}KB`);
});

it('formats ms', () => {
assert.equal(Util.formatMilliseconds(123), `120${NBSP}ms`);
assert.equal(Util.formatMilliseconds(2456.5, 0.1), `2,456.5${NBSP}ms`);
});

it('formats a duration', () => {
assert.equal(Util.formatDuration(60 * 1000), `1${NBSP}m`);
assert.equal(Util.formatDuration(60 * 60 * 1000 + 5000), `1${NBSP}h 5${NBSP}s`);
assert.equal(Util.formatDuration(28 * 60 * 60 * 1000 + 5000), `1${NBSP}d 4${NBSP}h 5${NBSP}s`);
});

it('calculates a score ratings', () => {
assert.equal(Util.calculateRating(0.0), 'fail');
assert.equal(Util.calculateRating(0.10), 'fail');
Expand Down
48 changes: 24 additions & 24 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2458,87 +2458,87 @@
{
"url": "http://localhost:10200/zone.js",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 71654,
"wastedBytes": 71654
},
{
"url": "http://localhost:10200/dobetterweb/lighthouse-480x318.jpg",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 24741,
"wastedBytes": 24741
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.js",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 1703,
"wastedBytes": 1703
},
{
"url": "http://localhost:10200/dobetterweb/dbw_disabled.css?delay=200&isdisabled",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 1108,
"wastedBytes": 1108
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=3000&async=true",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=100",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?scriptActivated&delay=200",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=2200",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=2000&async=true",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/empty_module.js?delay=500",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 144,
"wastedBytes": 144
},
{
"url": "blob:http://localhost:10200/ae0eac03-ab9b-4a6a-b299-f5212153e277",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 0,
"wastedBytes": 0
Expand All @@ -2560,7 +2560,7 @@
"text": "URL"
},
{
"key": "cacheLifetimeInSeconds",
"key": "cacheLifetimeMs",
"itemType": "ms",
"text": "Cache TTL",
"displayUnit": "duration"
Expand All @@ -2577,87 +2577,87 @@
{
"url": "http://localhost:10200/zone.js",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 71654,
"wastedBytes": 71654
},
{
"url": "http://localhost:10200/dobetterweb/lighthouse-480x318.jpg",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 24741,
"wastedBytes": 24741
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.js",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 1703,
"wastedBytes": 1703
},
{
"url": "http://localhost:10200/dobetterweb/dbw_disabled.css?delay=200&isdisabled",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 1108,
"wastedBytes": 1108
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=3000&async=true",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=100",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?scriptActivated&delay=200",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=2200",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/dbw_tester.css?delay=2000&async=true",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 821,
"wastedBytes": 821
},
{
"url": "http://localhost:10200/dobetterweb/empty_module.js?delay=500",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 144,
"wastedBytes": 144
},
{
"url": "blob:http://localhost:10200/ae0eac03-ab9b-4a6a-b299-f5212153e277",
"cacheControl": null,
"cacheLifetimeInSeconds": 0,
"cacheLifetimeMs": 0,
"cacheHitProbability": 0,
"totalBytes": 0,
"wastedBytes": 0
Expand Down Expand Up @@ -5326,4 +5326,4 @@
"description": "Run these additional validators on your site to check additional SEO best practices."
}
}
}
}

0 comments on commit 2573e82

Please sign in to comment.