Skip to content

Commit

Permalink
core(lantern): use computed artifact to create graph using trace (#16040
Browse files Browse the repository at this point in the history
)
  • Loading branch information
connorjclark authored Jun 4, 2024
1 parent 19087a2 commit 36c2077
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
22 changes: 2 additions & 20 deletions core/computed/metrics/lantern-metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
*/

import {LanternError} from '../../lib/lantern/lantern-error.js';
import {PageDependencyGraph as LanternPageDependencyGraph} from '../../lib/lantern/page-dependency-graph.js';
import {LighthouseError} from '../../lib/lh-error.js';
import {LoadSimulator} from '../load-simulator.js';
import {ProcessedNavigation} from '../processed-navigation.js';
import {ProcessedTrace} from '../processed-trace.js';
import {TraceEngineResult} from '../trace-engine-result.js';
import {PageDependencyGraph} from '../page-dependency-graph.js';

// TODO: we need to update all test traces (that use lantern) before this can be removed
/**
* @param {LH.Artifacts.MetricComputationDataInput} data
* @param {LH.Artifacts.ComputedContext} context
Expand All @@ -30,18 +26,6 @@ async function getComputationDataParamsFromDevtoolsLog(data, context) {
return {simulator, graph, processedNavigation};
}

/**
* @param {LH.Artifacts.URL} theURL
* @param {LH.Trace} trace
* @param {LH.Artifacts.ComputedContext} context
*/
async function createGraphFromTrace(theURL, trace, context) {
const {mainThreadEvents} = await ProcessedTrace.request(trace, context);
const traceEngineResult = await TraceEngineResult.request({trace}, context);
return LanternPageDependencyGraph.createGraphFromTrace(
mainThreadEvents, trace, traceEngineResult, theURL);
}

/**
* @param {LH.Artifacts.MetricComputationDataInput} data
* @param {LH.Artifacts.ComputedContext} context
Expand All @@ -51,9 +35,7 @@ async function getComputationDataParamsFromTrace(data, context) {
throw new Error(`Lantern metrics can only be computed on navigations`);
}

const {trace, URL} = data;
// TODO(15841): use computed artifact.
const {graph} = await createGraphFromTrace(URL, trace, context);
const graph = await PageDependencyGraph.request({...data, fromTrace: true}, context);
const processedNavigation = await ProcessedNavigation.request(data.trace, context);
const simulator = data.simulator || (await LoadSimulator.request(data, context));

Expand Down Expand Up @@ -82,7 +64,7 @@ function lanternErrorAdapter(err) {
* @param {LH.Artifacts.ComputedContext} context
*/
function getComputationDataParams(data, context) {
// TODO(15841): remove when all tests use the trace, and we want to remove CDT graph impl.
// TODO(15841): remove devtools impl when ready to make breaking change.
if (process.env.INTERNAL_LANTERN_USE_TRACE !== undefined) {
return getComputationDataParamsFromTrace(data, context);
} else {
Expand Down
15 changes: 11 additions & 4 deletions core/computed/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@ import {PageDependencyGraph as LanternPageDependencyGraph} from '../lib/lantern/
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';

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

class PageDependencyGraph {
/**
* @param {{trace: LH.Trace, devtoolsLog: LH.DevtoolsLog, URL: LH.Artifacts['URL']}} data
* @param {{trace: LH.Trace, devtoolsLog: LH.DevtoolsLog, URL: LH.Artifacts['URL'], fromTrace?: boolean}} data
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<Node>}
*/
static async compute_(data, context) {
const {trace, devtoolsLog, URL} = data;
const [processedTrace, networkRecords] = await Promise.all([
const [{mainThreadEvents}, networkRecords] = await Promise.all([
ProcessedTrace.request(trace, context),
NetworkRecords.request(devtoolsLog, context),
]);

const mainThreadEvents = processedTrace.mainThreadEvents;
if (data.fromTrace) {
const traceEngineResult = await TraceEngineResult.request({trace}, context);
const {graph} = await LanternPageDependencyGraph.createGraphFromTrace(
mainThreadEvents, trace, traceEngineResult, URL);
return graph;
}

const lanternRequests = networkRecords.map(NetworkRequest.asLanternNetworkRequest);
return LanternPageDependencyGraph.createGraph(mainThreadEvents, lanternRequests, URL);
}
}

const PageDependencyGraphComputed =
makeComputedArtifact(PageDependencyGraph, ['devtoolsLog', 'trace', 'URL']);
makeComputedArtifact(PageDependencyGraph, ['devtoolsLog', 'trace', 'URL', 'fromTrace']);
export {PageDependencyGraphComputed as PageDependencyGraph};

0 comments on commit 36c2077

Please sign in to comment.