completefrontend

Predict the outputMedium

What does this trace's self time add up to?

Leaf and Kettle saved a Chrome trace of one tap on Add to basket. In the Trace Event Format, the main thread's work is a list of complete events, ph: "X", each with a start ts and a duration dur, both in microseconds, and the file lists them in no particular order. The trace also holds a performance.mark() as an instant event.

The code nests the events the way the Performance panel does and works out each one's self time: its duration minus the durations of its direct children. Then it adds the self times up, and the durations too. What does it log?

The code
// One tap on Add to basket, from a Chrome trace in the Trace Event Format: the main thread's complete events
// (ph "X"), with ts and dur in microseconds, in the order the file lists them, and a performance.mark() (ph "I").
const events = [
  { name: "RunTask", ph: "X", ts: 2000000, dur: 64000 },
  { name: "FunctionCall", ph: "X", ts: 2001000, dur: 58000 },
  { name: "Layout", ph: "X", ts: 2030000, dur: 20000 },
  { name: "basket:tap", ph: "I", ts: 2001000 },
  { name: "EventDispatch", ph: "X", ts: 2000500, dur: 60000 },
  { name: "Paint", ph: "X", ts: 2064000, dur: 3000 },
  { name: "UpdateLayoutTree", ph: "X", ts: 2022000, dur: 8000 },
  { name: "MinorGC", ph: "X", ts: 2052000, dur: 4000 },
];
 
// Only complete events have a duration, and they may come in any order:
// sort a copy by start time, the longer one first when two start together.
const complete = events.filter((event) => event.ph === "X").sort((a, b) => a.ts - b.ts || b.dur - a.dur);
 
// Walk them with a stack of open events. An event's parent is the innermost event
// still open when it starts; one that has ended by then is closed first.
const selfTime = new Map();
const parentOf = new Map();
const open = [];
for (const event of complete) {
  while (open.length && open.at(-1).ts + open.at(-1).dur <= event.ts) open.pop();
  const parent = open.at(-1);
  parentOf.set(event.name, parent ? parent.name : "none");
  selfTime.set(event.name, event.dur);
  // A parent loses the time of its direct children, and only theirs.
  if (parent) selfTime.set(parent.name, selfTime.get(parent.name) - event.dur);
  open.push(event);
}
 
const ms = (microseconds) => microseconds / 1000;
const total = (numbers) => numbers.reduce((sum, value) => sum + value, 0);
const [busiest] = [...selfTime].sort((a, b) => b[1] - a[1]);
 
console.log("1. Complete events: " + complete.length);
console.log("2. RunTask: " + ms(complete[0].dur) + " ms in all, " + ms(selfTime.get("RunTask")) + " ms of its own");
console.log("3. UpdateLayoutTree: " + ms(selfTime.get("UpdateLayoutTree")) + " ms of its own");
console.log("4. Parent of Layout: " + parentOf.get("Layout") + ", of Paint: " + parentOf.get("Paint"));
console.log("5. Most self time: " + busiest[0] + ", " + ms(busiest[1]) + " ms");
console.log("6. Self times added up: " + ms(total([...selfTime.values()])) + " ms");
console.log("7. Durations added up: " + ms(total(complete.map((event) => event.dur))) + " ms");

Practice it in the editor, with tests

The answer

Work out every line it logs, in order. Then check.

Checking your sign-in…