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(lantern): use computed artifact to create graph using trace #16040

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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};
Loading