Skip to the code

Self time from a trace, like Bottom-up

The Performance panel's Bottom-up tab answers where the time really went. For every activity it shows Self Time, the time spent directly in it across all its occurrences, and Total Time, the time in it or anything it called. Write bottomUp(events). events are one thread's events from a trace in the Trace Event Format: objects with a name, a phase ph, and ts and dur in microseconds. Return [{ name, selfTime, totalTime }] in milliseconds, computed the way DevTools computes them.

Only complete events count: ph: "X" with a dur above 0 (DevTools treats an event that lasts 0 as an instant event). They may come in any order, so sort a copy by ts, and when two start together put the one that ends later first, keeping their order when both match: that's how DevTools sorts them. Then walk them with a stack of open events. Before taking an event, close every open event that ends at or before its ts; the event is then a direct child of the open event on top, if there is one. Events nest properly: a child never ends after its parent.

An event's self time is its dur minus the dur of each of its direct children, and a name's selfTime is the sum over all its events. A name's totalTime is the sum of its events' dur, except that an event inside another event of the same name (recursion) adds nothing, because its time is already inside the outer one. Leave out names whose selfTime is 0, as DevTools drops them. Sort by selfTime, largest first, then by name with <. Divide microseconds by 1000 for milliseconds.

bottomUp([
  { name: "Task", ph: "X", ts: 1000, dur: 8000 },
  { name: "renderMenu", ph: "X", ts: 1500, dur: 6000 },
  { name: "formatPrice", ph: "X", ts: 2000, dur: 1500 },
  { name: "formatPrice", ph: "X", ts: 4000, dur: 1500 },
  { name: "Layout", ph: "X", ts: 9000, dur: 2000 }, // starts as Task ends: not its child
  { name: "TracingStartedInBrowser", ph: "I", ts: 0 },
]);
// [
//   { name: "formatPrice", selfTime: 3, totalTime: 3 },
//   { name: "renderMenu", selfTime: 3, totalTime: 6 },
//   { name: "Layout", selfTime: 2, totalTime: 2 },
//   { name: "Task", selfTime: 2, totalTime: 8 },
// ]

What the tests check

  1. a parent's self time is its duration minus its children's
    const x = (name, ts, dur) => ({ name, ph: "X", ts, dur, pid: 1, tid: 1 });
    expect(bottomUp([x("Task", 0, 10000), x("renderMenu", 1000, 6000), x("Layout", 8000, 1500)])).toEqual([
      { name: "renderMenu", selfTime: 6, totalTime: 6 },
      { name: "Task", selfTime: 2.5, totalTime: 10 },
      { name: "Layout", selfTime: 1.5, totalTime: 1.5 },
    ]);
    
  2. only direct children are taken off, never grandchildren
    const x = (name, ts, dur) => ({ name, ph: "X", ts, dur, pid: 1, tid: 1 });
    const events = [x("Task", 0, 12000), x("handleClick", 500, 11000), x("updateCart", 1000, 8000), x("saveCart", 2000, 4000)];
    expect(bottomUp(events)).toEqual([
      { name: "saveCart", selfTime: 4, totalTime: 4 },
      { name: "updateCart", selfTime: 4, totalTime: 8 },
      { name: "handleClick", selfTime: 3, totalTime: 11 },
      { name: "Task", selfTime: 1, totalTime: 12 },
    ]);
    
  3. an event that starts as another ends is its sibling, not its child
    const x = (name, ts, dur) => ({ name, ph: "X", ts, dur, pid: 1, tid: 1 });
    const events = [x("Task", 0, 5000), x("parseHTML", 0, 2000), x("evaluateScript", 2000, 3000), x("Task", 5000, 4000), x("Paint", 5000, 1000)];
    expect(bottomUp(events)).toEqual([
      { name: "Task", selfTime: 3, totalTime: 9 },
      { name: "evaluateScript", selfTime: 3, totalTime: 3 },
      { name: "parseHTML", selfTime: 2, totalTime: 2 },
      { name: "Paint", selfTime: 1, totalTime: 1 },
    ]);
    
  4. sorts a copy of the events by start, the longer first when two start together
    const x = (name, ts, dur) => ({ name, ph: "X", ts, dur, pid: 1, tid: 1 });
    const events = [x("formatPrice", 3000, 1000), x("renderMenu", 1000, 5000), x("Layout", 9000, 500), x("Task", 1000, 8000)];
    expect(bottomUp(events)).toEqual([
      { name: "renderMenu", selfTime: 4, totalTime: 5 },
      { name: "Task", selfTime: 3, totalTime: 8 },
      { name: "formatPrice", selfTime: 1, totalTime: 1 },
      { name: "Layout", selfTime: 0.5, totalTime: 0.5 },
    ]);
    expect(events.map((event) => event.name)).toEqual(["formatPrice", "renderMenu", "Layout", "Task"]);
    
  5. adds up a name's occurrences, and counts recursion once in total time
    const x = (name, ts, dur) => ({ name, ph: "X", ts, dur, pid: 1, tid: 1 });
    const events = [
      x("Task", 0, 20000),
      x("walkMenu", 1000, 10000),
      x("walkMenu", 3000, 4000),
      x("walkMenu", 4000, 1000),
      x("formatPrice", 8000, 2000),
      x("walkMenu", 14000, 2000),
    ];
    expect(bottomUp(events)).toEqual([
      { name: "walkMenu", selfTime: 10, totalTime: 12 },
      { name: "Task", selfTime: 8, totalTime: 20 },
      { name: "formatPrice", selfTime: 2, totalTime: 2 },
    ]);
    
  6. only complete events that last longer than 0 count
    const x = (name, ts, dur) => ({ name, ph: "X", ts, dur, pid: 1, tid: 1 });
    const events = [
      { name: "thread_name", ph: "M", ts: 0, pid: 1, tid: 1, args: { name: "CrRendererMain" } },
      { name: "TracingStartedInBrowser", ph: "I", ts: 0, pid: 1, tid: 1, s: "t" },
      { name: "Task", ph: "B", ts: 0, pid: 1, tid: 1 },
      { name: "Task", ph: "E", ts: 3000, pid: 1, tid: 1 },
      x("Task", 5000, 3000),
      x("MarkDOMContent", 6000, 0),
      x("ParseHTML", 6000, 1000),
    ];
    expect(bottomUp(events)).toEqual([
      { name: "Task", selfTime: 2, totalTime: 3 },
      { name: "ParseHTML", selfTime: 1, totalTime: 1 },
    ]);
    
  7. a name with no self time is left out, and events that match exactly nest in the given order
    const x = (name, ts, dur) => ({ name, ph: "X", ts, dur, pid: 1, tid: 1 });
    expect(bottomUp([x("FunctionCall", 1000, 2000), x("renderMenu", 1000, 2000), x("Task", 0, 4000)])).toEqual([
      { name: "Task", selfTime: 2, totalTime: 4 },
      { name: "renderMenu", selfTime: 2, totalTime: 2 },
    ]);
    
  8. no complete events is an empty table
    expect(bottomUp([])).toEqual([]);
    expect(bottomUp([{ name: "TracingStartedInBrowser", ph: "I", ts: 0, pid: 1, tid: 1, s: "t" }])).toEqual([]);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).