Skip to content

Commit

Permalink
Get elements that needs recalculate styles before FCP/LCP. (#1964)
Browse files Browse the repository at this point in the history
* Get elements that needs recalculate styles before FCP/LCP.

First go at collecting number of elements that needs recalculate
styles before FCP/LCP.

I think this should work fine, the only thing that needs tweaking is
the data structure.

* Add the data in renderBlocking instead
  • Loading branch information
soulgalore committed Jul 15, 2023
1 parent a76897f commit 940fe58
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/chrome/webdriver/chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { parse } from '../traceCategoriesParser.js';
import { pathToFolder } from '../../support/pathToFolder.js';
import { ChromeDevtoolsProtocol } from '../chromeDevtoolsProtocol.js';
import { Android, isAndroidConfigured } from '../../android/index.js';
import {
getFirstContentFulPaintEvent,
getLargestContentfulPaintEvent,
getRecalculateStyleElementsAndTimeBefore
} from './traceUtilities.js';
const unlink = promisify(_unlink);
const rm = promisify(_rm);

Expand Down Expand Up @@ -353,6 +358,27 @@ export class Chromium {
asset.args.data.renderBlocking;
}

const fcpEvent = getFirstContentFulPaintEvent(trace.traceEvents);
const lcpEvent = getLargestContentfulPaintEvent(trace.traceEvents);

result.renderBlocking = { recalculateStyle: {}, requests: {} };

if (fcpEvent) {
const beforeFCP = getRecalculateStyleElementsAndTimeBefore(
trace.traceEvents,
fcpEvent.ts
);
result.renderBlocking.recalculateStyle.beforeFCP = beforeFCP;
}

if (lcpEvent) {
const beforeLCP = getRecalculateStyleElementsAndTimeBefore(
trace.traceEvents,
lcpEvent.ts
);
result.renderBlocking.recalculateStyle.beforeLCP = beforeLCP;
}

if (!this.options.skipHar) {
for (let harRequest of this.hars[index - 1].log.entries) {
if (renderBlockingInfo[harRequest.request.url]) {
Expand All @@ -362,7 +388,7 @@ export class Chromium {
}
}

result.renderBlocking = renderBlockingInfo;
result.renderBlocking.requests = renderBlockingInfo;
}

// Google Web Vitals hacksery
Expand Down
64 changes: 64 additions & 0 deletions lib/chrome/webdriver/traceUtilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import intel from 'intel';
const log = intel.getLogger('browsertime.chrome');

export function getLargestContentfulPaintEvent(traceEvents) {
const lcpCandidates = traceEvents.filter(
task => task.name === 'largestContentfulPaint::Candidate'
);

if (lcpCandidates.length > 0) {
let lcpEvent = lcpCandidates[0];

for (const candidate of lcpCandidates) {
if (candidate.ts > lcpEvent.ts) {
lcpEvent = candidate;
}
}
return lcpEvent;
} else {
log.info('No LCP event found in the trace');
}
}

export function getFirstContentFulPaintEvent(traceEvents) {
// Get first contentful paint
const fcpEvent = traceEvents.find(
task => task.name === 'firstContentfulPaint'
);

if (fcpEvent) {
return fcpEvent;
} else {
log.info('Did not find the FCP event in the trace');
}
}

export function getRecalculateStyleElementsAndTimeBefore(
traceEvents,
timestamp
) {
const recalculatesBefore = traceEvents.filter(
task =>
task.cat === 'disabled-by-default-devtools.timeline' &&
task.name === 'ScheduleStyleRecalculation' &&
task.ts < timestamp
);

const updateLayoutTree = traceEvents.filter(
task =>
task.cat === 'blink,devtools.timeline' &&
task.name === 'UpdateLayoutTree' &&
task.ts < timestamp &&
recalculatesBefore.some(
recalculate => task.args.beginData.frame === recalculate.args.data.frame
)
);
let elements = 0;
let duration = 0;
for (let a of updateLayoutTree) {
elements += a.args.elementCount;
duration += a.dur;
}

return { elements, durationInMillis: duration / 1000 };
}
6 changes: 6 additions & 0 deletions lib/core/engine/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ export class Collector {
results.renderBlocking = [];
}
results.renderBlocking.push(data.renderBlocking);

statistics.addDeep({
renderBlocking: {
recalculateStyle: data.renderBlocking.recalculateStyle
}
});
}

// Add power data (if we have it)
Expand Down

0 comments on commit 940fe58

Please sign in to comment.