Skip to content

Commit 8108742

Browse files
committed
fix(studio): the edit bench counts a task without thread time at its wall time
1 parent 83e63da commit 8108742

3 files changed

Lines changed: 24 additions & 31 deletions

File tree

‎packages/studio/tests/e2e/edit-accuracy/case.mjs‎

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -494,50 +494,39 @@ function topLevelTasks(trace, { pid, tid }) {
494494
return tops;
495495
}
496496

497-
// Thread CPU time, spread evenly over the task, so a loaded machine descheduling the thread does not count as work.
497+
// Thread CPU time, spread evenly over the task, so a descheduled thread does not count; untimed tasks count wall time.
498498
const cpuUs = (e, a, b) =>
499-
(Math.max(0, Math.min(b, e.ts + e.dur) - Math.max(a, e.ts)) * e.tdur) / (e.dur || 1);
499+
(Math.max(0, Math.min(b, e.ts + e.dur) - Math.max(a, e.ts)) * (e.tdur ?? e.dur)) / (e.dur || 1);
500500

501501
/** Main-thread CPU ms inside each frame interval, on the thread that ran the end mark; null when unknown. */
502502
function mainThreadPerFrame({ frames, mark, trace }) {
503503
const anchor = trace.find((e) => e.name === TRACE_MARK && e.cat.includes("user_timing"));
504-
if (!anchor) return { work: null, untimedOutside: 0, unknown: "no end mark in the trace" };
504+
// Without the mark the work is unknown, which fails smoothness alone.
505+
if (!anchor) return { work: null, wallTimed: 0, unknown: "no end mark in the trace" };
505506
const toTrace = (ms) => anchor.ts + (ms - mark) * 1000;
506507
const [from, to] = [toTrace(frames[0]), toTrace(frames.at(-1))];
507-
// A task cut by the trace's start or end has no thread time; outside the frames it adds nothing anyway.
508-
const all = topLevelTasks(trace, anchor);
509-
const tasks = all.filter((e) => e.ts < to && e.ts + e.dur > from);
510-
const untimedOutside = all.filter((e) => e.tdur === undefined && !tasks.includes(e)).length;
511-
// Without the mark or thread CPU time the work is unknown, which fails smoothness alone.
512-
const untimed = tasks.filter((e) => e.tdur === undefined);
513-
if (untimed.length) {
514-
const [first] = untimed;
515-
const at = `${first.name} ${first.dur} us at ${Math.round((first.ts - from) / 1000)} ms`;
516-
return {
517-
work: null,
518-
untimedOutside,
519-
unknown: `${untimed.length} untimed task(s) in the frames, first ${at}`,
520-
};
521-
}
508+
const tasks = topLevelTasks(trace, anchor);
509+
const inFrames = (e) => e.ts < to && e.ts + e.dur > from;
510+
const wallTimed = tasks.filter((e) => e.tdur === undefined && inFrames(e)).length;
522511
const work = frames.slice(1).map((t, i) => {
523512
const [a, b] = [toTrace(frames[i]), toTrace(t)];
524513
return tasks.reduce((sum, e) => sum + cpuUs(e, a, b), 0) / 1000;
525514
});
526-
return { work, untimedOutside };
515+
return { work, wallTimed };
527516
}
528517

529518
const hundredth = (v) => Math.round(v * 100) / 100;
530519

531520
export function smoothness(rec) {
532521
const intervals = rec.frames.slice(1).map((t, i) => t - rec.frames[i]);
533-
const { work, untimedOutside, unknown } = mainThreadPerFrame(rec);
522+
const { work, wallTimed, unknown } = mainThreadPerFrame(rec);
534523
return {
535524
p95: percentile(intervals, 95),
536525
frames: intervals.length,
537526
longTasks: rec.long.length,
538527
intervals: intervals.map(hundredth),
539528
work: work && work.map(hundredth),
540-
untimedOutside,
529+
wallTimed,
541530
...(unknown && { unknown }),
542531
};
543532
}

‎packages/studio/tests/e2e/edit-accuracy/sequences.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function mergeSmooth(parts) {
118118
longTasks: parts.reduce((n, s) => n + s.longTasks, 0),
119119
intervals,
120120
work: parts.every((s) => s.work) ? parts.flatMap((s) => s.work) : null,
121-
untimedOutside: parts.reduce((n, s) => n + s.untimedOutside, 0),
121+
wallTimed: parts.reduce((n, s) => n + s.wallTimed, 0),
122122
...(parts.some((s) => s.unknown) && {
123123
unknown: parts
124124
.map((s) => s.unknown)

‎packages/studio/tests/e2e/edit-accuracy/smoothness.test.mjs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ const thread = { pid: 1, tid: 2 };
55
const mark = { name: "edit-bench-end", cat: "blink.user_timing", ts: 1_000_000, ...thread };
66
const task = (ts, dur, tdur) => ({ name: "RunTask", ph: "X", ts, dur, tdur, ...thread });
77
// Frame stamps 50, 66 and 82 ms before a mark at 100 ms: trace time 950, 966 and 982 ms.
8-
const rec = (tasks) => ({ frames: [50, 66, 82], mark: 100, long: [], trace: [mark, ...tasks] });
8+
const rec = (trace) => ({ frames: [50, 66, 82], mark: 100, long: [], trace });
99

1010
describe("smoothness work per frame", () => {
11-
it("measures the frames when only a task before them lacks thread time", () => {
12-
const s = smoothness(rec([task(900_000, 10_000), task(960_000, 4_000, 4_000)]));
11+
it("measures the frames when a task outside them lacks thread time", () => {
12+
const s = smoothness(rec([mark, task(900_000, 10_000), task(960_000, 4_000, 2_000)]));
13+
expect(s.work).toEqual([2, 0]);
14+
expect(s.wallTimed).toBe(0);
15+
});
16+
17+
it("counts a task inside the frames that lacks thread time at its wall time", () => {
18+
const s = smoothness(rec([mark, task(960_000, 4_000)]));
1319
expect(s.work).toEqual([4, 0]);
14-
expect(s.untimedOutside).toBe(1);
20+
expect(s.wallTimed).toBe(1);
1521
});
1622

17-
it("leaves the work unknown when a task inside the frames lacks thread time", () => {
18-
const s = smoothness(rec([task(960_000, 4_000)]));
23+
it("leaves the work unknown without the end mark", () => {
24+
const s = smoothness(rec([task(960_000, 4_000, 4_000)]));
1925
expect(s.work).toBeNull();
20-
expect(s.unknown).toMatch(
21-
/^1 untimed task\(s\) in the frames, first RunTask 4000 us at 10 ms$/,
22-
);
26+
expect(s.unknown).toBe("no end mark in the trace");
2327
});
2428
});

0 commit comments

Comments
 (0)