Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

report.test.ts

120 lines3.7 KB

1import { expect, it } from "@effect/vitest";
2​
3import {
4 flatten,
5 soakCaseDeltas,
6 soakGrowth,
7 stepOf,
8 type MobileMetrics,
9 type PerfWindow,
10} from "./report.ts";
11​
12const frames = (from: number, count: number, gap = 1000 / 60) =>
13 Array.from({ length: count }, (_, index) => from + index * gap);
14​
15const window = (overrides: Partial<PerfWindow>): PerfWindow => ({
16 startedAt: 0,
17 inputs: [],
18 commits: [],
19 content: [],
20 keys: [],
21 js: [],
22 ui: [],
23 renders: null,
24 heapBytes: null,
25 ...overrides,
26});
27​
28it("times a tap from its touch end, and counts only what happened after it", () => {
29 const step = stepOf(
30 window({
31 inputs: [
32 { kind: "touch-start", at: 100 },
33 { kind: "touch-end", at: 180 },
34 ],
35 // The first commit is the harness's own perfStart call, before the tap.
36 commits: [
37 { at: 50, ms: 9 },
38 { at: 260, ms: 30 },
39 { at: 420, ms: 12 },
40 ],
41 content: [
42 { thread: "env:other", at: 200, frameAt: 210 },
43 { thread: "env:target", at: 400, frameAt: 415 },
44 ],
45 js: frames(0, 60),
46 }),
47 { case: "switch-long", step: "open", visit: "first", round: 1 },
48 "target",
49 );
50 expect(step).toMatchObject({ contentMs: 235, settledMs: 240, commits: 2, commitMs: 42 });
51});
52​
53it("measures typing from the first key, and reports no content without a target", () => {
54 const step = stepOf(
55 window({
56 inputs: [
57 { kind: "key", at: 1000 },
58 { kind: "key", at: 1100 },
59 ],
60 commits: [{ at: 1010, ms: 4 }],
61 keys: [20, 12, 16],
62 }),
63 { case: "type", step: "keys", visit: "step", round: 1 },
64 null,
65 );
66 expect(step).toMatchObject({ contentMs: null, settledMs: 10, keyP50Ms: 16, keyP90Ms: 20 });
67});
68​
69it("takes each run's median as one sample, so steps of one launch never pass for runs", () => {
70 const step = (contentMs: number) =>
71 stepOf(
72 window({
73 inputs: [{ kind: "touch-end", at: 0 }],
74 content: [{ thread: "t", at: contentMs, frameAt: contentMs }],
75 }),
76 { case: "switch-short", step: "open", visit: "revisit", round: 1 },
77 "t",
78 );
79 const metrics: MobileMetrics = {
80 meta: {},
81 runs: [
82 { steps: [step(100), step(300), step(200)], memory: [] },
83 { steps: [step(400)], memory: [{ label: "launch", heapMB: 80, processMB: null }] },
84 ],
85 };
86 const flat = flatten(metrics);
87 expect(flat.get("switch-short.open.revisit.contentMs")).toEqual([200, 400]);
88 expect(flat.get("memory.launch.heapMB")).toEqual([80]);
89 expect(flat.has("memory.launch.processMB")).toBe(false);
90});
91​
92it("times a native menu item from its JS handler, since the tap never reaches JS as a touch", () => {
93 const step = stepOf(
94 window({
95 inputs: [{ kind: "menu-action", at: 500 }],
96 commits: [
97 { at: 520, ms: 10 },
98 { at: 700, ms: 40 },
99 ],
100 }),
101 { case: "diff", step: "review", visit: "step", round: 1 },
102 null,
103 );
104 expect(step).toMatchObject({ settledMs: 200, commits: 2 });
105});
106​
107it("fits soak growth past warm-up per iteration, and names the case that grew the heap", () => {
108 const samples = [
109 { iteration: 0, case: "start", heapMB: 90, processMB: null },
110 // Warm-up: the first iteration loads code and caches.
111 { iteration: 1, case: "a", heapMB: 140, processMB: null },
112 { iteration: 1, case: "b", heapMB: 150, processMB: null },
113 { iteration: 2, case: "a", heapMB: 150, processMB: null },
114 { iteration: 2, case: "b", heapMB: 152, processMB: null },
115 { iteration: 3, case: "a", heapMB: 152, processMB: null },
116 { iteration: 3, case: "b", heapMB: 154, processMB: null },
117 ];
118 expect(soakGrowth(samples)).toMatchObject({ heapMB: 2 });
119 expect(soakCaseDeltas(samples).find((entry) => entry.case === "b")?.medianMB).toBe(2);
120});