Skip to content

Commit

Permalink
core(lantern): refactor Trace Engine specific code to file
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Jun 10, 2024
1 parent f8063f8 commit 2bf6074
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 455 deletions.
2 changes: 1 addition & 1 deletion core/computed/metrics/lantern-metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {LoadSimulator} from '../load-simulator.js';
import {ProcessedNavigation} from '../processed-navigation.js';
import {PageDependencyGraph} from '../page-dependency-graph.js';
import {TraceEngineResult} from '../trace-engine-result.js';
import {createProcessedNavigation} from '../../lib/lantern/lantern.js';
import {createProcessedNavigation} from '../../lib/lantern/trace-engine-computation-data.js';

/**
* @param {LH.Artifacts.MetricComputationDataInput} data
Expand Down
6 changes: 4 additions & 2 deletions core/computed/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {NetworkRequest} from '../lib/network-request.js';
import {ProcessedTrace} from './processed-trace.js';
import {NetworkRecords} from './network-records.js';
import {TraceEngineResult} from './trace-engine-result.js';
import * as TraceEngineComputationData from '../lib/lantern/trace-engine-computation-data.js';

/** @typedef {import('../lib/lantern/base-node.js').Node<LH.Artifacts.NetworkRequest>} Node */

Expand All @@ -28,8 +29,9 @@ class PageDependencyGraph {

if (data.fromTrace) {
const traceEngineResult = await TraceEngineResult.request({trace}, context);
const {graph} = await LanternPageDependencyGraph.createGraphFromTrace(
trace, traceEngineResult, URL);
const requests = TraceEngineComputationData.createNetworkRequests(trace, traceEngineResult);
const graph =
TraceEngineComputationData.createGraph(requests, trace, traceEngineResult, URL);
return graph;
}

Expand Down
47 changes: 0 additions & 47 deletions core/lib/lantern/lantern.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import * as Lantern from './types/lantern.js';

/** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricName} MetricName */
/** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricScore} MetricScore */

/** @type {LH.Util.SelfMap<LH.Crdp.Network.ResourceType>} */
const NetworkRequestTypes = {
XHR: 'XHR',
Expand All @@ -31,48 +26,6 @@ const NetworkRequestTypes = {
Prefetch: 'Prefetch',
};

/**
* @param {LH.Artifacts.TraceEngineResult} traceEngineResult
* @return {Lantern.Simulation.ProcessedNavigation}
*/
function createProcessedNavigation(traceEngineResult) {
const Meta = traceEngineResult.data.Meta;
const frameId = Meta.mainFrameId;
const scoresByNav = traceEngineResult.data.PageLoadMetrics.metricScoresByFrameId.get(frameId);
if (!scoresByNav) {
throw new Error('missing metric scores for main frame');
}

const lastNavigationId = Meta.mainFrameNavigations.at(-1)?.args.data?.navigationId;
const scores = lastNavigationId && scoresByNav.get(lastNavigationId);
if (!scores) {
throw new Error('missing metric scores for specified navigation');
}

/** @param {MetricName} metric */
const getTimestampOrUndefined = metric => {
const metricScore = scores.get(metric);
if (!metricScore?.event) return;
return metricScore.event.ts;
};
/** @param {MetricName} metric */
const getTimestamp = metric => {
const metricScore = scores.get(metric);
if (!metricScore?.event) throw new Error(`missing metric: ${metric}`);
return metricScore.event.ts;
};
// TODO: should use `MetricName.LCP`, but it is a const enum.
const FCP = /** @type {MetricName} */('FCP');
const LCP = /** @type {MetricName} */('LCP');
return {
timestamps: {
firstContentfulPaint: getTimestamp(FCP),
largestContentfulPaint: getTimestampOrUndefined(LCP),
},
};
}

export {
NetworkRequestTypes,
createProcessedNavigation,
};
Loading

0 comments on commit 2bf6074

Please sign in to comment.