Skip to content

Commit

Permalink
core(computed-artifacts): fix cache and add perf timing (#6619)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored Nov 21, 2018
1 parent 34a542a commit d964d66
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 7 deletions.
12 changes: 10 additions & 2 deletions lighthouse-core/computed/computed-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict';

const ArbitraryEqualityMap = require('../lib/arbitrary-equality-map.js');
const log = require('lighthouse-logger');

/**
* Decorate computableArtifact with a caching `request()` method which will
Expand All @@ -25,18 +26,25 @@ function makeComputedArtifact(computableArtifact) {
*/
const request = (artifacts, context) => {
const computedCache = context.computedCache;
const cache = computedCache.get(computableArtifact.name) || new ArbitraryEqualityMap();
computedCache.set(computableArtifact.name, cache);
const computedName = computableArtifact.name;

const cache = computedCache.get(computedName) || new ArbitraryEqualityMap();
computedCache.set(computedName, cache);

const computed = /** @type {ReturnType<C['compute_']>|undefined} */ (cache.get(artifacts));
if (computed) {
return computed;
}

const status = {msg: `Computing artifact: ${computedName}`, id: `lh:computed:${computedName}`};
log.time(status, 'verbose');

const artifactPromise = /** @type {ReturnType<C['compute_']>} */
(computableArtifact.compute_(artifacts, context));
cache.set(artifacts, artifactPromise);

artifactPromise.then(() => log.timeEnd(status)).catch(() => log.timeEnd(status));

return artifactPromise;
};

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/lib/arbitrary-equality-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const isEqual = require('lodash.isequal');
*/
class ArbitraryEqualityMap {
constructor() {
this._equalsFn = /** @type {function(*,*):boolean} */ ((a, b) => a === b);
this._equalsFn = ArbitraryEqualityMap.deepEquals;
/** @type {Array<{key: *, value: *}>} */
this._entries = [];
}
Expand Down
8 changes: 4 additions & 4 deletions lighthouse-core/test/computed/computed-artifact-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ const assert = require('assert');
const makeComputedArtifact = require('../../computed/computed-artifact.js');

describe('ComputedArtifact base class', () => {
it.skip('caches computed artifacts by strict equality', async () => {
it('caches computed artifacts by strict equality', async () => {
let computeCounter = 0;

const TestComputedArtifact = makeComputedArtifact(class TestComputedArtifact {
class RawTestComputedArtifact {
static async compute_() {
return computeCounter++;
}
});
}

const context = {
computedCache: new Map(),
};

const TestComputedArtifact = makeComputedArtifact(RawTestComputedArtifact);
let result = await TestComputedArtifact.request({x: 1}, context);
assert.equal(result, 0);

Expand Down
138 changes: 138 additions & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3474,6 +3474,12 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:NetworkRecords",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:redirects-http",
Expand Down Expand Up @@ -3510,24 +3516,60 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:FirstContentfulPaint",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:TraceOfTab",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:first-meaningful-paint",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:FirstMeaningfulPaint",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:load-fast-enough-for-pwa",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:Interactive",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:speed-index",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:SpeedIndex",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:Speedline",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:screenshot-thumbnails",
Expand All @@ -3540,12 +3582,24 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:Screenshots",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:estimated-input-latency",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:EstimatedInputLatency",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:errors-in-console",
Expand All @@ -3558,12 +3612,24 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:MainResource",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:first-cpu-idle",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:FirstCPUIdle",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:interactive",
Expand All @@ -3582,18 +3648,66 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:CriticalRequestChains",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:redirects",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:LanternInteractive",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:LanternFirstMeaningfulPaint",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:LanternFirstContentfulPaint",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:PageDependencyGraph",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:LoadSimulator",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:NetworkAnalysis",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:webapp-install-banner",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:ManifestValues",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:splash-screen",
Expand Down Expand Up @@ -3630,6 +3744,12 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:MainThreadTasks",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:bootup-time",
Expand All @@ -3642,6 +3762,12 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:LoadSimulator",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:uses-rel-preconnect",
Expand Down Expand Up @@ -3990,6 +4116,18 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:FirstContentfulPaint",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:computed:LanternFirstContentfulPaint",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:unminified-css",
Expand Down
Loading

0 comments on commit d964d66

Please sign in to comment.