Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788import { describe, expect, it } from "vitest";
import { describeFrameSplit, describeLongFrame, type LoafEntry, type LoafScript } from "./perf";
/** * The instrument that reads the instrument. * * `[perf] longtask … unknown/window` — which is what the console looked like before this existed — * is a log line that has to be re-measured with a profiler attached before it means anything, and the * person staring at a storm of them has already done that mentally and given up. `long-animation-frame` * reports the script, by URL, because the dev server serves modules by path — so the formatting here * is what turns the storm into a file name, and it gets tested like anything else that is the only * evidence anybody has. */
const s = (over: Partial<LoafScript> = {}): LoafScript => ({ name: "script", duration: 10, sourceURL: "http://localhost:5173/src/lib/player/media.ts?t=171", ...over,});
const frame = (over: Partial<LoafEntry> = {}): LoafEntry => ({ name: "long-animation-frame", startTime: 1000, duration: 120, ...over, }) as LoafEntry;
describe("reading a long frame", () => { it("names the file, the function and the line, biggest first", () => { // The whole point: the dev server hands back module paths, so the log says where to go. expect( describeLongFrame([ s({ duration: 20, sourceFunctionName: "hydrate", sourceLocationLineNumber: 361 }), s({ duration: 74, sourceFunctionName: "readAll", sourceLocationLineNumber: 88 }), ]), ).toBe("src/lib/player/media.ts:readAll:88 74ms · src/lib/player/media.ts:hydrate:361 20ms"); });
it("folds the same place together rather than repeating it per entry", () => { // A frame can attribute the same function several times, and a list that repeats it reads as // several costs rather than one. expect( describeLongFrame([ s({ sourceFunctionName: "decode", duration: 30 }), s({ sourceFunctionName: "decode", duration: 40 }), ]), ).toBe("src/lib/player/media.ts:decode 70ms"); });
it("keeps three and drops the rest", () => { const many = Array.from({ length: 6 }, (_, i) => s({ sourceFunctionName: `f${i}`, duration: 30 - i }), ); expect(describeLongFrame(many).split(" · ")).toHaveLength(3); });
it("says so out loud when nothing in this code ran", () => { // The answer that used to be `unknown/window`: no script in the frame means the browser spent it // — gc over the megabytes the media cache holds, microtasks, or an extension's content script. expect(describeLongFrame(undefined)).toContain("no script attributed"); expect(describeLongFrame([])).toContain("no script attributed"); });
it("keeps a url the dev server never served", () => { // A worker, a blob, or an extension content script has no `/src/` in it. The line still has to // name something, because "unknown" is the answer this whole observer replaced. expect(describeLongFrame([s({ sourceURL: "blob:https://x/9f2", duration: 12 })])).toContain( "blob:https://x/9f2", ); });});
describe("splitting a frame between script and layout", () => { it("reports the browser's share when it is worth mentioning", () => { // This is the half that sends you to the DOM instead of to the code, and it is what a // `[Violation] Forced reflow` line was pointing at. expect(describeFrameSplit(frame({ styleAndLayoutStart: 1060 }))).toBe(" · style/layout 60ms"); });
it("stays quiet when the script half is the whole story", () => { expect(describeFrameSplit(frame({ styleAndLayoutStart: 1196 }))).toBe(""); expect(describeFrameSplit(frame())).toBe(""); });});