Skip to content

Commit

Permalink
core(traces): move findTracingStartedEvt to tracingProcessor
Browse files Browse the repository at this point in the history
And add support of new traces to scripts/minify-trace.js
  • Loading branch information
patrickhulce authored and paulirish committed Jun 5, 2018
1 parent df631a0 commit 4826a77
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
28 changes: 3 additions & 25 deletions lighthouse-core/gather/computed/trace-of-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

const ComputedArtifact = require('./computed-artifact');
const log = require('lighthouse-logger');
const TracingProcessor = require('../../lib/traces/tracing-processor');
const LHError = require('../../lib/errors');
const Sentry = require('../../lib/sentry');

Expand Down Expand Up @@ -51,31 +52,8 @@ class TraceOfTab extends ComputedArtifact {
// @ts-ignore - stableSort added to Array by WebInspector.
.stableSort((event0, event1) => event0.ts - event1.ts);

// Find out the inspected page frame.
/** @type {LH.TraceEvent|undefined} */
let startedInPageEvt;
const startedInBrowserEvt = keyEvents.find(e => e.name === 'TracingStartedInBrowser');
if (startedInBrowserEvt && startedInBrowserEvt.args.data &&
startedInBrowserEvt.args.data.frames) {
const mainFrame = startedInBrowserEvt.args.data.frames.find(frame => !frame.parent);
const pid = mainFrame && mainFrame.processId;
const threadNameEvt = keyEvents.find(e => e.pid === pid && e.ph === 'M' &&
e.cat === '__metadata' && e.name === 'thread_name' && e.args.name === 'CrRendererMain');
startedInPageEvt = mainFrame && threadNameEvt ?
Object.assign({}, startedInBrowserEvt, {
pid, tid: threadNameEvt.tid, name: 'TracingStartedInPage',
args: {data: {page: mainFrame.frame}}}) :
undefined;
}
// Support legacy browser versions that do not emit TracingStartedInBrowser event.
if (!startedInPageEvt) {
// The first TracingStartedInPage in the trace is definitely our renderer thread of interest
// Beware: the tracingStartedInPage event can appear slightly after a navigationStart
startedInPageEvt = keyEvents.find(e => e.name === 'TracingStartedInPage');
}
if (!startedInPageEvt) throw new LHError(LHError.errors.NO_TRACING_STARTED);
// @ts-ignore - property chain exists for 'TracingStartedInPage' event.
const frameId = startedInPageEvt.args.data.page;
// Find the inspected frame
const {startedInPageEvt, frameId} = TracingProcessor.findTracingStartedEvt(keyEvents);

// Filter to just events matching the frame ID for sanity
const frameEvents = keyEvents.filter(e => e.args.frame === frameId);
Expand Down
38 changes: 38 additions & 0 deletions lighthouse-core/lib/traces/tracing-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const BASE_RESPONSE_LATENCY = 16;
const SCHEDULABLE_TASK_TITLE = 'TaskQueueManager::ProcessTaskFromWorkQueue';
const SCHEDULABLE_TASK_TITLE_ALT = 'ThreadControllerImpl::DoWork';
const LHError = require('../errors');

class TraceProcessor {
/**
Expand Down Expand Up @@ -179,6 +180,43 @@ class TraceProcessor {
return topLevelEvents;
}

/**
* @param {LH.TraceEvent[]} events
* @return {{startedInPageEvt: LH.TraceEvent, frameId: string}}
*/
static findTracingStartedEvt(events) {
/** @type {LH.TraceEvent|undefined} */
let startedInPageEvt;

// Prefer the newer TracingStartedInBrowser event first, if it exists
const startedInBrowserEvt = events.find(e => e.name === 'TracingStartedInBrowser');
if (startedInBrowserEvt && startedInBrowserEvt.args.data &&
startedInBrowserEvt.args.data.frames) {
const mainFrame = startedInBrowserEvt.args.data.frames.find(frame => !frame.parent);
const pid = mainFrame && mainFrame.processId;
const threadNameEvt = events.find(e => e.pid === pid && e.ph === 'M' &&
e.cat === '__metadata' && e.name === 'thread_name' && e.args.name === 'CrRendererMain');
startedInPageEvt = mainFrame && threadNameEvt ?
Object.assign({}, startedInBrowserEvt, {
pid, tid: threadNameEvt.tid, name: 'TracingStartedInPage',
args: {data: {page: mainFrame.frame}}}) :
undefined;
}

// Support legacy browser versions that do not emit TracingStartedInBrowser event.
if (!startedInPageEvt) {
// The first TracingStartedInPage in the trace is definitely our renderer thread of interest
// Beware: the tracingStartedInPage event can appear slightly after a navigationStart
startedInPageEvt = events.find(e => e.name === 'TracingStartedInPage');
}

if (!startedInPageEvt) throw new LHError(LHError.errors.NO_TRACING_STARTED);

// @ts-ignore - property chain exists for 'TracingStartedInPage' event.
const frameId = /** @type {string} */ (startedInPageEvt.args.data.page);
return {startedInPageEvt, frameId};
}

/**
* @param {LH.TraceEvent} evt
* @return {boolean}
Expand Down
5 changes: 2 additions & 3 deletions lighthouse-core/scripts/lantern/minify-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

const fs = require('fs');
const path = require('path');
const TracingProcessor = require('../../lib/traces/tracing-processor');

if (process.argv.length !== 4) {
console.error('Usage $0: <input file> <output file>');
Expand Down Expand Up @@ -76,9 +77,7 @@ const traceEventsToKeepInProcess = new Set([
* @param {LH.TraceEvent[]} events
*/
function filterOutUnnecessaryTasksByNameAndDuration(events) {
// TODO(phulce): update this once https://github.com/GoogleChrome/lighthouse/pull/5271 lands
const startedInPageEvt = events.find(evt => evt.name === 'TracingStartedInPage');
if (!startedInPageEvt) throw new Error('Could not find TracingStartedInPage');
const {startedInPageEvt} = TracingProcessor.findTracingStartedEvt(events);

return events.filter(evt => {
if (toplevelTaskNames.has(evt.name) && evt.dur < 1000) return false;
Expand Down

0 comments on commit 4826a77

Please sign in to comment.