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: convert computed artifact loading to regular require() #6204

Merged
merged 7 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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-core/gather/computed/computed-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ArbitraryEqualityMap = require('../../lib/arbitrary-equality-map');

class ComputedArtifact {
/**
* @param {LH.ComputedArtifacts} allComputedArtifacts
* @param {*} allComputedArtifacts
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the longer term plan for new-computed-artifact and this file? or is new-computed-artifact meant to be like the verb constructor not the new file and I've misread it the whole time... 😮

Copy link
Member Author

Choose a reason for hiding this comment

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

what's the longer term plan for new-computed-artifact and this file?

that's the cleanup followup I mentioned. I'll delete that one, rename the new- file, then I wanted to add some tests and possibly do the file move.

*/
constructor(allComputedArtifacts) {
const cache = new ArbitraryEqualityMap();
Expand All @@ -19,7 +19,7 @@ class ComputedArtifact {
// @ts-ignore cache is close enough to a Map for our purposes (but e.g. no [Symbol.toStringTag])
this._cache = cache;

/** @type {LH.ComputedArtifacts} */
/** @type {*} */
this._allComputedArtifacts = allComputedArtifacts;
}

Expand All @@ -36,7 +36,7 @@ class ComputedArtifact {
* Override with more specific `artifact` and return type to implement a
* computed artifact.
* @param {*} artifact Input to computation.
* @param {LH.ComputedArtifacts} allComputedArtifacts Access to all computed artifacts.
* @param {*} allComputedArtifacts Access to all computed artifacts.
* @return {Promise<*>}
* @throws {Error}
*/
Expand Down
20 changes: 9 additions & 11 deletions lighthouse-core/gather/computed/critical-request-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
*/
'use strict';

const ComputedArtifact = require('./computed-artifact');
const makeComputedArtifact = require('./new-computed-artifact.js');
const NetworkRequest = require('../../lib/network-request');
const assert = require('assert');
const NetworkRecords = require('./network-records.js');
const MainResource = require('./main-resource.js');

class CriticalRequestChains extends ComputedArtifact {
get name() {
return 'CriticalRequestChains';
}

class CriticalRequestChains {
/**
* For now, we use network priorities as a proxy for "render-blocking"/critical-ness.
* It's imperfect, but there is not a higher-fidelity signal available yet.
Expand Down Expand Up @@ -145,17 +143,17 @@ class CriticalRequestChains extends ComputedArtifact {

/**
* @param {{URL: LH.Artifacts['URL'], devtoolsLog: LH.DevtoolsLog}} data
* @param {LH.ComputedArtifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Artifacts.CriticalRequestNode>}
*/
async compute_(data, artifacts) {
static async compute_(data, context) {
const [networkRecords, mainResource] = await Promise.all([
artifacts.requestNetworkRecords(data.devtoolsLog),
artifacts.requestMainResource(data),
NetworkRecords.request(data.devtoolsLog, context),
MainResource.request(data, context),
]);

return CriticalRequestChains.extractChain(networkRecords, mainResource);
}
}

module.exports = CriticalRequestChains;
module.exports = makeComputedArtifact(CriticalRequestChains);
17 changes: 7 additions & 10 deletions lighthouse-core/gather/computed/load-simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
*/
'use strict';

const ComputedArtifact = require('./computed-artifact');
const makeComputedArtifact = require('./new-computed-artifact');
const constants = require('../../config/constants');
const Simulator = require('../../lib/dependency-graph/simulator/simulator');
const NetworkAnalysis = require('../../gather/computed/network-analysis.js');

class LoadSimulatorArtifact extends ComputedArtifact {
get name() {
return 'LoadSimulator';
}

class LoadSimulator {
/**
* @param {{devtoolsLog: LH.DevtoolsLog, settings: LH.Config.Settings}} data
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<Simulator>}
*/
async compute_(data, artifacts) {
static async compute_(data, context) {
const {throttlingMethod, throttling} = data.settings;
const networkAnalysis = await artifacts.requestNetworkAnalysis(data.devtoolsLog);
const networkAnalysis = await NetworkAnalysis.request(data.devtoolsLog, context);

/** @type {LH.Gatherer.Simulation.Options} */
const options = {
Expand Down Expand Up @@ -64,4 +61,4 @@ class LoadSimulatorArtifact extends ComputedArtifact {
}
}

module.exports = LoadSimulatorArtifact;
module.exports = makeComputedArtifact(LoadSimulator);
17 changes: 7 additions & 10 deletions lighthouse-core/gather/computed/main-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
*/
'use strict';

const ComputedArtifact = require('./computed-artifact');
const makeComputedArtifact = require('./new-computed-artifact.js');
const URL = require('../../lib/url-shim');
const NetworkRecords = require('./network-records.js');

/**
* @fileoverview This artifact identifies the main resource on the page. Current solution assumes
* that the main resource is the first non-rediected one.
*/
class MainResource extends ComputedArtifact {
get name() {
return 'MainResource';
}

class MainResource {
/**
* @param {{URL: LH.Artifacts['URL'], devtoolsLog: LH.DevtoolsLog}} data
* @param {LH.ComputedArtifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Artifacts.NetworkRequest>}
*/
async compute_(data, artifacts) {
static async compute_(data, context) {
const finalUrl = data.URL.finalUrl;
const requests = await artifacts.requestNetworkRecords(data.devtoolsLog);
const requests = await NetworkRecords.request(data.devtoolsLog, context);
// equalWithExcludedFragments is expensive, so check that the finalUrl starts with the request first
const mainResource = requests.find(request => finalUrl.startsWith(request.url) &&
URL.equalWithExcludedFragments(request.url, finalUrl));
Expand All @@ -37,4 +34,4 @@ class MainResource extends ComputedArtifact {
}
}

module.exports = MainResource;
module.exports = makeComputedArtifact(MainResource);
17 changes: 7 additions & 10 deletions lighthouse-core/gather/computed/main-thread-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
'use strict';

const ComputedArtifact = require('./computed-artifact');
const makeComputedArtifact = require('./new-computed-artifact.js');
const {taskGroups, taskNameToGroup} = require('../../lib/task-groups');
const TraceOfTab = require('./trace-of-tab.js');

/**
* @fileoverview
Expand Down Expand Up @@ -40,11 +41,7 @@ const {taskGroups, taskNameToGroup} = require('../../lib/task-groups');
* @prop {TaskGroup} group
*/

class MainThreadTasks extends ComputedArtifact {
get name() {
return 'MainThreadTasks';
}

class MainThreadTasks {
/**
* @param {LH.TraceEvent} event
* @param {TaskNode} [parent]
Expand Down Expand Up @@ -229,13 +226,13 @@ class MainThreadTasks extends ComputedArtifact {

/**
* @param {LH.Trace} trace
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<Array<TaskNode>>} networkRecords
*/
async compute_(trace, artifacts) {
const {mainThreadEvents} = await artifacts.requestTraceOfTab(trace);
static async compute_(trace, context) {
const {mainThreadEvents} = await TraceOfTab.request(trace, context);
return MainThreadTasks.getMainThreadTasks(mainThreadEvents);
}
}

module.exports = MainThreadTasks;
module.exports = makeComputedArtifact(MainThreadTasks);
23 changes: 15 additions & 8 deletions lighthouse-core/gather/computed/metrics/estimated-input-latency.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*/
'use strict';

const MetricArtifact = require('./metric');
const makeComputedArtifact = require('../new-computed-artifact.js');
const ComputedMetric = require('./metric');
const LHError = require('../../../lib/lh-error');
const TracingProcessor = require('../../../lib/traces/tracing-processor');
const LanternEstimatedInputLatency = require('./lantern-estimated-input-latency.js');

const ROLLING_WINDOW_SIZE = 5000;

Expand All @@ -16,11 +18,7 @@ const ROLLING_WINDOW_SIZE = 5000;
* FMP and the end of the trace.
* @see https://docs.google.com/document/d/1b9slyaB9yho91YTOkAQfpCdULFkZM9LqsipcX3t7He8/preview
*/
class EstimatedInputLatency extends MetricArtifact {
get name() {
return 'EstimatedInputLatency';
}

class EstimatedInputLatency extends ComputedMetric {
/**
* @param {Array<{start: number, end: number, duration: number}>} events
* @return {number}
Expand All @@ -43,11 +41,20 @@ class EstimatedInputLatency extends MetricArtifact {
return worst90thPercentileLatency;
}

/**
* @param {LH.Artifacts.MetricComputationData} data
* @param {LH.Audit.Context} context
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static computeSimulatedMetric(data, context) {
return LanternEstimatedInputLatency.request(data, context);
}

/**
* @param {LH.Artifacts.MetricComputationData} data
* @return {Promise<LH.Artifacts.Metric>}
*/
computeObservedMetric(data) {
static computeObservedMetric(data) {
const {firstMeaningfulPaint} = data.traceOfTab.timings;
if (!firstMeaningfulPaint) {
throw new LHError(LHError.errors.NO_FMP);
Expand All @@ -64,4 +71,4 @@ class EstimatedInputLatency extends MetricArtifact {
}
}

module.exports = EstimatedInputLatency;
module.exports = makeComputedArtifact(EstimatedInputLatency);
19 changes: 13 additions & 6 deletions lighthouse-core/gather/computed/metrics/first-contentful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
*/
'use strict';

const MetricArtifact = require('./metric');
const makeComputedArtifact = require('../new-computed-artifact.js');
const ComputedMetric = require('./metric');
const LanternFirstContentfulPaint = require('./lantern-first-contentful-paint.js');

class FirstContentfulPaint extends MetricArtifact {
get name() {
return 'FirstContentfulPaint';
class FirstContentfulPaint extends ComputedMetric {
/**
* @param {LH.Artifacts.MetricComputationData} data
* @param {LH.Audit.Context} context
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static computeSimulatedMetric(data, context) {
return LanternFirstContentfulPaint.request(data, context);
}

/**
* @param {LH.Artifacts.MetricComputationData} data
* @return {Promise<LH.Artifacts.Metric>}
*/
async computeObservedMetric(data) {
static async computeObservedMetric(data) {
const {traceOfTab} = data;

return {
Expand All @@ -26,4 +33,4 @@ class FirstContentfulPaint extends MetricArtifact {
}
}

module.exports = FirstContentfulPaint;
module.exports = makeComputedArtifact(FirstContentfulPaint);
24 changes: 16 additions & 8 deletions lighthouse-core/gather/computed/metrics/first-cpu-idle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* 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 MetricArtifact = require('./metric');

const makeComputedArtifact = require('../new-computed-artifact.js');
const ComputedMetric = require('./metric');
const TracingProcessor = require('../../../lib/traces/tracing-processor');
const LHError = require('../../../lib/lh-error');
const LanternFirstCPUIdle = require('./lantern-first-cpu-idle.js');

const LONG_TASK_THRESHOLD = 50;

Expand Down Expand Up @@ -47,11 +50,7 @@ const EXPONENTIATION_COEFFICIENT = -Math.log(3 - 1) / 15;
*
* If this timestamp is earlier than DOMContentLoaded, use DOMContentLoaded as firstCPUIdle.
*/
class FirstCPUIdle extends MetricArtifact {
get name() {
return 'FirstCPUIdle';
}

class FirstCPUIdle extends ComputedMetric {
/**
* @param {number} t The time passed since FMP in miliseconds.
* @return {number}
Expand Down Expand Up @@ -166,11 +165,20 @@ class FirstCPUIdle extends MetricArtifact {
throw new LHError(LHError.errors.NO_FCPUI_IDLE_PERIOD);
}

/**
* @param {LH.Artifacts.MetricComputationData} data
* @param {LH.Audit.Context} context
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static computeSimulatedMetric(data, context) {
return LanternFirstCPUIdle.request(data, context);
}

/**
* @param {LH.Artifacts.MetricComputationData} data
* @return {Promise<LH.Artifacts.Metric>}
*/
computeObservedMetric(data) {
static async computeObservedMetric(data) {
const {traceOfTab} = data;
const navStart = traceOfTab.timestamps.navigationStart;
const FMP = traceOfTab.timings.firstMeaningfulPaint;
Expand Down Expand Up @@ -203,4 +211,4 @@ class FirstCPUIdle extends MetricArtifact {
* @typedef {{start: number, end: number, duration: number}} TaskCluster
*/

module.exports = FirstCPUIdle;
module.exports = makeComputedArtifact(FirstCPUIdle);
19 changes: 13 additions & 6 deletions lighthouse-core/gather/computed/metrics/first-meaningful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
*/
'use strict';

const MetricArtifact = require('./metric');
const makeComputedArtifact = require('../new-computed-artifact.js');
const ComputedMetric = require('./metric');
const LHError = require('../../../lib/lh-error');
const LanternFirstMeaningfulPaint = require('./lantern-first-meaningful-paint.js');

class FirstMeaningfulPaint extends MetricArtifact {
get name() {
return 'FirstMeaningfulPaint';
class FirstMeaningfulPaint extends ComputedMetric {
/**
* @param {LH.Artifacts.MetricComputationData} data
* @param {LH.Audit.Context} context
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static computeSimulatedMetric(data, context) {
return LanternFirstMeaningfulPaint.request(data, context);
}

/**
* @param {LH.Artifacts.MetricComputationData} data
* @return {Promise<LH.Artifacts.Metric>}
*/
async computeObservedMetric(data) {
static async computeObservedMetric(data) {
const {traceOfTab} = data;
if (!traceOfTab.timestamps.firstMeaningfulPaint) {
throw new LHError(LHError.errors.NO_FMP);
Expand All @@ -31,4 +38,4 @@ class FirstMeaningfulPaint extends MetricArtifact {
}
}

module.exports = FirstMeaningfulPaint;
module.exports = makeComputedArtifact(FirstMeaningfulPaint);
Loading