@player-devtools/profiler-plugin 0.13.1--canary.16.915
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ProfilerDevtoolsPlugin.native.js +2237 -0
- package/dist/ProfilerDevtoolsPlugin.native.js.map +1 -0
- package/dist/cjs/index.cjs +346 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/index.legacy-esm.js +325 -0
- package/dist/index.mjs +325 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +39 -0
- package/src/__tests__/__snapshots__/plugin.test.ts.snap +1484 -0
- package/src/__tests__/addProfilerInterceptorsToHooks.test.ts +60 -0
- package/src/__tests__/plugin.test.ts +168 -0
- package/src/addProfilerInterceptorsToHooks.ts +89 -0
- package/src/constants.ts +7 -0
- package/src/helpers/__tests__/hasHooks.test.ts +55 -0
- package/src/helpers/__tests__/isMatchingPaths.test.ts +24 -0
- package/src/helpers/__tests__/isRecordType.test.ts +31 -0
- package/src/helpers/__tests__/profiler.test.ts +282 -0
- package/src/helpers/__tests__/transformProfilerData.test.ts +151 -0
- package/src/helpers/hasHooks.ts +9 -0
- package/src/helpers/index.ts +5 -0
- package/src/helpers/isMatchingPaths.ts +5 -0
- package/src/helpers/isRecordType.ts +2 -0
- package/src/helpers/profiler.ts +114 -0
- package/src/helpers/transformProfilerData.ts +51 -0
- package/src/index.ts +1 -0
- package/src/plugin.ts +185 -0
- package/src/types.ts +16 -0
- package/types/addProfilerInterceptorsToHooks.d.ts +4 -0
- package/types/constants.d.ts +2 -0
- package/types/helpers/hasHooks.d.ts +5 -0
- package/types/helpers/index.d.ts +6 -0
- package/types/helpers/isMatchingPaths.d.ts +2 -0
- package/types/helpers/isRecordType.d.ts +2 -0
- package/types/helpers/profiler.d.ts +22 -0
- package/types/helpers/transformProfilerData.d.ts +3 -0
- package/types/index.d.ts +2 -0
- package/types/plugin.d.ts +20 -0
- package/types/types.d.ts +16 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
|
+
import { Profiler } from "../profiler";
|
|
3
|
+
|
|
4
|
+
let count = 2490.0;
|
|
5
|
+
vi.mock("@player-devtools/plugin", async () => {
|
|
6
|
+
const actual = await vi.importActual("@player-devtools/plugin");
|
|
7
|
+
return {
|
|
8
|
+
...actual,
|
|
9
|
+
getNowTime: vi.fn(() => {
|
|
10
|
+
count += 0.1;
|
|
11
|
+
return count;
|
|
12
|
+
}),
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe("Profiler", () => {
|
|
17
|
+
test("sequential top-level timers each become a separate rootNodes entry", () => {
|
|
18
|
+
const p = new Profiler();
|
|
19
|
+
|
|
20
|
+
p.start();
|
|
21
|
+
|
|
22
|
+
p.startTimer("hookA");
|
|
23
|
+
p.endTimer({ hookName: "hookA" });
|
|
24
|
+
|
|
25
|
+
p.startTimer("hookB");
|
|
26
|
+
p.endTimer({ hookName: "hookB" });
|
|
27
|
+
|
|
28
|
+
const { rootNodes } = p.stopProfiler();
|
|
29
|
+
|
|
30
|
+
expect(rootNodes).toHaveLength(2);
|
|
31
|
+
expect(rootNodes[0]!.name).toBe("hookA");
|
|
32
|
+
expect(rootNodes[1]!.name).toBe("hookB");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("nested timers become children of the outer timer", () => {
|
|
36
|
+
const p = new Profiler();
|
|
37
|
+
|
|
38
|
+
p.start();
|
|
39
|
+
|
|
40
|
+
p.startTimer("outer");
|
|
41
|
+
p.startTimer("inner1");
|
|
42
|
+
p.endTimer({ hookName: "inner1" });
|
|
43
|
+
p.startTimer("inner2");
|
|
44
|
+
p.endTimer({ hookName: "inner2" });
|
|
45
|
+
p.endTimer({ hookName: "outer" });
|
|
46
|
+
|
|
47
|
+
const { rootNodes } = p.stopProfiler();
|
|
48
|
+
|
|
49
|
+
expect(rootNodes).toHaveLength(1);
|
|
50
|
+
expect(rootNodes[0]!.name).toBe("outer");
|
|
51
|
+
expect(rootNodes[0]!.children).toHaveLength(2);
|
|
52
|
+
expect(rootNodes[0]!.children[0]!.name).toBe("inner1");
|
|
53
|
+
expect(rootNodes[0]!.children[1]!.name).toBe("inner2");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("endTimer for unknown name warns and does nothing", () => {
|
|
57
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
58
|
+
const p = new Profiler();
|
|
59
|
+
|
|
60
|
+
p.start();
|
|
61
|
+
p.startTimer("hookA");
|
|
62
|
+
p.endTimer({ hookName: "unknown" });
|
|
63
|
+
|
|
64
|
+
const { rootNodes } = p.stopProfiler();
|
|
65
|
+
|
|
66
|
+
// hookA is still on the stack — not finalized, so rootNodes has it but without endTime
|
|
67
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
68
|
+
expect.stringContaining("'unknown' not found in stack"),
|
|
69
|
+
);
|
|
70
|
+
// hookA was started but never ended — it's still a root node (was pushed to rootNodes on start)
|
|
71
|
+
expect(rootNodes[0]!.name).toBe("hookA");
|
|
72
|
+
expect(rootNodes[0]!.endTime).toBeUndefined();
|
|
73
|
+
|
|
74
|
+
warnSpy.mockRestore();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("endTimer for buried name pops and warns about intermediate timers", () => {
|
|
78
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
79
|
+
const p = new Profiler();
|
|
80
|
+
|
|
81
|
+
p.start();
|
|
82
|
+
|
|
83
|
+
p.startTimer("outer");
|
|
84
|
+
p.startTimer("middle");
|
|
85
|
+
p.startTimer("inner");
|
|
86
|
+
// End "outer" without ending "inner" or "middle" first
|
|
87
|
+
p.endTimer({ hookName: "outer" });
|
|
88
|
+
|
|
89
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
90
|
+
expect.stringContaining("popping 'inner'"),
|
|
91
|
+
);
|
|
92
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
93
|
+
expect.stringContaining("popping 'middle'"),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const { rootNodes } = p.stopProfiler();
|
|
97
|
+
|
|
98
|
+
// All three should be finalized
|
|
99
|
+
expect(rootNodes).toHaveLength(1);
|
|
100
|
+
expect(rootNodes[0]!.name).toBe("outer");
|
|
101
|
+
|
|
102
|
+
warnSpy.mockRestore();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("start() resets all state", () => {
|
|
106
|
+
const p = new Profiler();
|
|
107
|
+
|
|
108
|
+
p.start();
|
|
109
|
+
p.startTimer("hookA");
|
|
110
|
+
p.endTimer({ hookName: "hookA" });
|
|
111
|
+
|
|
112
|
+
p.start();
|
|
113
|
+
const { rootNodes } = p.stopProfiler();
|
|
114
|
+
|
|
115
|
+
expect(rootNodes).toHaveLength(0);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("calls onUpdate on start(), endTimer(), and clear(), not on startTimer()", () => {
|
|
119
|
+
const onUpdate = vi.fn();
|
|
120
|
+
const p = new Profiler(onUpdate);
|
|
121
|
+
|
|
122
|
+
expect(onUpdate.mock.calls.length).toBe(0);
|
|
123
|
+
|
|
124
|
+
p.start();
|
|
125
|
+
expect(onUpdate.mock.calls.length).toBe(1);
|
|
126
|
+
|
|
127
|
+
p.startTimer("hookA");
|
|
128
|
+
expect(onUpdate.mock.calls.length).toBe(1);
|
|
129
|
+
|
|
130
|
+
p.endTimer({ hookName: "hookA" });
|
|
131
|
+
expect(onUpdate.mock.calls.length).toBe(2);
|
|
132
|
+
|
|
133
|
+
p.startTimer("hookB");
|
|
134
|
+
p.endTimer({ hookName: "hookB" });
|
|
135
|
+
expect(onUpdate.mock.calls.length).toBe(3);
|
|
136
|
+
|
|
137
|
+
p.startTimer("hookC");
|
|
138
|
+
p.clear();
|
|
139
|
+
expect(onUpdate.mock.calls.length).toBe(4);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("getSnapshot returns a deep clone of rootNodes", () => {
|
|
143
|
+
const p = new Profiler();
|
|
144
|
+
|
|
145
|
+
p.start();
|
|
146
|
+
|
|
147
|
+
p.startTimer("slow");
|
|
148
|
+
p.endTimer({ hookName: "slow" });
|
|
149
|
+
|
|
150
|
+
p.startTimer("fast");
|
|
151
|
+
p.endTimer({ hookName: "fast" });
|
|
152
|
+
|
|
153
|
+
const snap = p.getSnapshot();
|
|
154
|
+
|
|
155
|
+
expect(snap.rootNodes).toHaveLength(2);
|
|
156
|
+
|
|
157
|
+
// Snapshot is a clone — adding to live tree doesn't affect it
|
|
158
|
+
p.startTimer("extra");
|
|
159
|
+
p.endTimer({ hookName: "extra" });
|
|
160
|
+
expect(snap.rootNodes).toHaveLength(2);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("getSnapshot sets endTime and value on in-flight nodes using current time", () => {
|
|
164
|
+
const p = new Profiler();
|
|
165
|
+
|
|
166
|
+
p.start();
|
|
167
|
+
|
|
168
|
+
// Finish one node so we have a reference
|
|
169
|
+
p.startTimer("finished");
|
|
170
|
+
p.endTimer({ hookName: "finished" });
|
|
171
|
+
|
|
172
|
+
// Leave this one in-flight
|
|
173
|
+
p.startTimer("inflight");
|
|
174
|
+
|
|
175
|
+
const snap = p.getSnapshot();
|
|
176
|
+
|
|
177
|
+
const finished = snap.rootNodes.find((n) => n.name === "finished");
|
|
178
|
+
const inflight = snap.rootNodes.find((n) => n.name === "inflight");
|
|
179
|
+
|
|
180
|
+
// Finished node is unchanged
|
|
181
|
+
expect(finished!.endTime).toBeDefined();
|
|
182
|
+
expect(finished!.value).toBeGreaterThan(0);
|
|
183
|
+
|
|
184
|
+
// In-flight node gets a synthetic endTime and value based on snapshot time
|
|
185
|
+
expect(inflight!.endTime).toBeDefined();
|
|
186
|
+
expect(inflight!.value).toBeGreaterThan(0);
|
|
187
|
+
|
|
188
|
+
// Live node is not mutated
|
|
189
|
+
const p2 = new Profiler();
|
|
190
|
+
p2.start();
|
|
191
|
+
p2.startTimer("live");
|
|
192
|
+
const liveBefore = p2.getSnapshot().rootNodes[0]!;
|
|
193
|
+
expect(liveBefore.endTime).toBeDefined(); // snapshot sets it
|
|
194
|
+
// but the original node in the stack must remain without endTime
|
|
195
|
+
// (verified indirectly: calling endTimer still works normally)
|
|
196
|
+
p2.endTimer({ hookName: "live" });
|
|
197
|
+
const liveAfter = p2.getSnapshot().rootNodes[0]!;
|
|
198
|
+
expect(liveAfter.endTime).toBeDefined();
|
|
199
|
+
expect(liveAfter.value).toBeGreaterThan(0);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe("clear()", () => {
|
|
203
|
+
test("clears rootNodes but leaves in-progress timers on the stack", () => {
|
|
204
|
+
const p = new Profiler();
|
|
205
|
+
p.start();
|
|
206
|
+
|
|
207
|
+
p.startTimer("finished");
|
|
208
|
+
p.endTimer({ hookName: "finished" });
|
|
209
|
+
|
|
210
|
+
p.startTimer("inflight");
|
|
211
|
+
|
|
212
|
+
p.clear();
|
|
213
|
+
|
|
214
|
+
const { rootNodes } = p.stopProfiler();
|
|
215
|
+
// rootNodes from before the clear are gone; the in-flight timer produces one new root
|
|
216
|
+
expect(rootNodes.find((n) => n.name === "finished")).toBeUndefined();
|
|
217
|
+
expect(rootNodes.find((n) => n.name === "inflight")).toBeDefined();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("resets startTime of in-progress timers to current time", () => {
|
|
221
|
+
const p = new Profiler();
|
|
222
|
+
p.start();
|
|
223
|
+
|
|
224
|
+
p.startTimer("inflight");
|
|
225
|
+
const beforeClear = p.getSnapshot().rootNodes[0]!.startTime!;
|
|
226
|
+
|
|
227
|
+
p.clear();
|
|
228
|
+
|
|
229
|
+
const afterClear = p.getSnapshot().rootNodes[0]!.startTime!;
|
|
230
|
+
expect(afterClear).toBeGreaterThan(beforeClear);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("fires onUpdate", () => {
|
|
234
|
+
const onUpdate = vi.fn();
|
|
235
|
+
const p = new Profiler(onUpdate);
|
|
236
|
+
p.start();
|
|
237
|
+
p.startTimer("hookA");
|
|
238
|
+
onUpdate.mockClear();
|
|
239
|
+
|
|
240
|
+
p.clear();
|
|
241
|
+
|
|
242
|
+
expect(onUpdate).toHaveBeenCalledTimes(1);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("endTimer still works normally for in-progress timers after clear", () => {
|
|
246
|
+
const p = new Profiler();
|
|
247
|
+
p.start();
|
|
248
|
+
|
|
249
|
+
p.startTimer("outer");
|
|
250
|
+
p.startTimer("inner");
|
|
251
|
+
|
|
252
|
+
p.clear();
|
|
253
|
+
|
|
254
|
+
p.endTimer({ hookName: "inner" });
|
|
255
|
+
p.endTimer({ hookName: "outer" });
|
|
256
|
+
|
|
257
|
+
const { rootNodes } = p.stopProfiler();
|
|
258
|
+
expect(rootNodes).toHaveLength(1);
|
|
259
|
+
expect(rootNodes[0]!.name).toBe("outer");
|
|
260
|
+
expect(rootNodes[0]!.children).toHaveLength(1);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("stopProfiler returns the full rootNodes forest", () => {
|
|
265
|
+
const p = new Profiler();
|
|
266
|
+
|
|
267
|
+
p.start();
|
|
268
|
+
|
|
269
|
+
p.startTimer("a");
|
|
270
|
+
p.startTimer("a.child");
|
|
271
|
+
p.endTimer({ hookName: "a.child" });
|
|
272
|
+
p.endTimer({ hookName: "a" });
|
|
273
|
+
|
|
274
|
+
p.startTimer("b");
|
|
275
|
+
p.endTimer({ hookName: "b" });
|
|
276
|
+
|
|
277
|
+
const { rootNodes } = p.stopProfiler();
|
|
278
|
+
|
|
279
|
+
expect(rootNodes).toHaveLength(2);
|
|
280
|
+
expect(rootNodes[0]!.children).toHaveLength(1);
|
|
281
|
+
});
|
|
282
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { transformProfilerData } from "../transformProfilerData";
|
|
3
|
+
import type { ProfilerNode } from "../../types";
|
|
4
|
+
|
|
5
|
+
const node = (
|
|
6
|
+
name: string,
|
|
7
|
+
startTime: number,
|
|
8
|
+
endTime: number,
|
|
9
|
+
children: ProfilerNode[] = [],
|
|
10
|
+
): ProfilerNode => ({
|
|
11
|
+
name,
|
|
12
|
+
startTime,
|
|
13
|
+
endTime,
|
|
14
|
+
value: Math.ceil((endTime - startTime) * 1000),
|
|
15
|
+
children,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("transformProfilerData", () => {
|
|
19
|
+
test("returns root with transformed children, preserving root fields", () => {
|
|
20
|
+
const root = node("root", 0, 1, [node("a", 0, 1)]);
|
|
21
|
+
const result = transformProfilerData(root);
|
|
22
|
+
|
|
23
|
+
expect(result.name).toBe("root");
|
|
24
|
+
expect(result.startTime).toBe(0);
|
|
25
|
+
expect(result.endTime).toBe(1);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("nodes with no gap between them produce no spacers", () => {
|
|
29
|
+
const root = node("root", 0, 2, [node("a", 0, 1), node("b", 1, 2)]);
|
|
30
|
+
const { children } = transformProfilerData(root);
|
|
31
|
+
|
|
32
|
+
expect(children).toHaveLength(2);
|
|
33
|
+
expect(children[0]!.name).toBe("a");
|
|
34
|
+
expect(children[1]!.name).toBe("b");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("gap before first child inserts a spacer", () => {
|
|
38
|
+
const root = node("root", 0, 2, [node("a", 1, 2)]);
|
|
39
|
+
const { children } = transformProfilerData(root);
|
|
40
|
+
|
|
41
|
+
expect(children).toHaveLength(2);
|
|
42
|
+
expect(children[0]!.name).toBe("(work)");
|
|
43
|
+
expect(children[0]!.value).toBe(1000);
|
|
44
|
+
expect(children[1]!.name).toBe("a");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("gap between children inserts a spacer between them", () => {
|
|
48
|
+
const root = node("root", 0, 3, [node("a", 0, 1), node("b", 2, 3)]);
|
|
49
|
+
const { children } = transformProfilerData(root);
|
|
50
|
+
|
|
51
|
+
expect(children).toHaveLength(3);
|
|
52
|
+
expect(children[0]!.name).toBe("a");
|
|
53
|
+
expect(children[1]!.name).toBe("(work)");
|
|
54
|
+
expect(children[1]!.value).toBe(1000);
|
|
55
|
+
expect(children[2]!.name).toBe("b");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("spacer value is gap duration in milliseconds (ceil)", () => {
|
|
59
|
+
// gap of 0.0015 seconds → ceil(0.0015 * 1000) = ceil(1.5) = 2
|
|
60
|
+
const root = node("root", 0, 2, [node("a", 0.0015, 2)]);
|
|
61
|
+
const { children } = transformProfilerData(root);
|
|
62
|
+
|
|
63
|
+
expect(children[0]!.name).toBe("(work)");
|
|
64
|
+
expect(children[0]!.value).toBe(2);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("spacer has correct styling fields", () => {
|
|
68
|
+
const root = node("root", 0, 2, [node("a", 1, 2)]);
|
|
69
|
+
const { children } = transformProfilerData(root);
|
|
70
|
+
const spacer = children[0]!;
|
|
71
|
+
|
|
72
|
+
expect(spacer.backgroundColor).toBe("#000000");
|
|
73
|
+
expect(spacer.color).toBe("#000000");
|
|
74
|
+
expect(spacer.tooltip).toBe("Placeholder time between hooks");
|
|
75
|
+
expect(spacer.children).toHaveLength(0);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("nodes missing startTime, endTime, or value are filtered out", () => {
|
|
79
|
+
const root: ProfilerNode = {
|
|
80
|
+
name: "root",
|
|
81
|
+
startTime: 0,
|
|
82
|
+
endTime: 2,
|
|
83
|
+
value: 2000,
|
|
84
|
+
children: [
|
|
85
|
+
{ name: "no-start", endTime: 1, value: 1000, children: [] },
|
|
86
|
+
{ name: "no-end", startTime: 0, value: 1000, children: [] },
|
|
87
|
+
{ name: "no-value", startTime: 0, endTime: 1, children: [] },
|
|
88
|
+
node("valid", 0, 1),
|
|
89
|
+
],
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const { children } = transformProfilerData(root);
|
|
93
|
+
expect(children).toHaveLength(1);
|
|
94
|
+
expect(children[0]!.name).toBe("valid");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("nodes with value of zero are filtered out", () => {
|
|
98
|
+
const root: ProfilerNode = {
|
|
99
|
+
name: "root",
|
|
100
|
+
startTime: 0,
|
|
101
|
+
endTime: 2,
|
|
102
|
+
value: 2000,
|
|
103
|
+
children: [
|
|
104
|
+
{
|
|
105
|
+
name: "zero-value",
|
|
106
|
+
startTime: 0,
|
|
107
|
+
endTime: 0,
|
|
108
|
+
value: 0,
|
|
109
|
+
children: [],
|
|
110
|
+
},
|
|
111
|
+
node("valid", 0, 1),
|
|
112
|
+
],
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const { children } = transformProfilerData(root);
|
|
116
|
+
expect(children).toHaveLength(1);
|
|
117
|
+
expect(children[0]!.name).toBe("valid");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("root with no children returns empty children array", () => {
|
|
121
|
+
const root = node("root", 0, 1);
|
|
122
|
+
const { children } = transformProfilerData(root);
|
|
123
|
+
expect(children).toHaveLength(0);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("nested children are recursively transformed", () => {
|
|
127
|
+
const inner = node("inner", 1.5, 2, []);
|
|
128
|
+
const outer = node("outer", 0, 2, [inner]);
|
|
129
|
+
const root = node("root", 0, 2, [outer]);
|
|
130
|
+
|
|
131
|
+
const result = transformProfilerData(root);
|
|
132
|
+
const outerResult = result.children[0]!;
|
|
133
|
+
|
|
134
|
+
// gap before inner inside outer → spacer inserted
|
|
135
|
+
expect(outerResult.children[0]!.name).toBe("(work)");
|
|
136
|
+
expect(outerResult.children[1]!.name).toBe("inner");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("multiple gaps each produce their own spacer", () => {
|
|
140
|
+
const root = node("root", 0, 6, [node("a", 1, 2), node("b", 4, 5)]);
|
|
141
|
+
|
|
142
|
+
const { children } = transformProfilerData(root);
|
|
143
|
+
|
|
144
|
+
// spacer(0→1), a, spacer(2→4), b
|
|
145
|
+
expect(children).toHaveLength(4);
|
|
146
|
+
expect(children[0]!.name).toBe("(work)");
|
|
147
|
+
expect(children[1]!.name).toBe("a");
|
|
148
|
+
expect(children[2]!.name).toBe("(work)");
|
|
149
|
+
expect(children[3]!.name).toBe("b");
|
|
150
|
+
});
|
|
151
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { isRecordType } from "./isRecordType";
|
|
2
|
+
|
|
3
|
+
export type ObjectWithHooks = {
|
|
4
|
+
hooks: Record<PropertyKey, unknown>;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const hasHooks = (obj: unknown): obj is ObjectWithHooks => {
|
|
8
|
+
return isRecordType(obj) && "hooks" in obj && isRecordType(obj.hooks);
|
|
9
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { ProfilerNode } from "../types";
|
|
2
|
+
import { getNowTime } from "@player-devtools/plugin";
|
|
3
|
+
|
|
4
|
+
export class Profiler {
|
|
5
|
+
private rootNodes: ProfilerNode[] = [];
|
|
6
|
+
private stack: ProfilerNode[] = [];
|
|
7
|
+
|
|
8
|
+
constructor(private readonly onUpdate?: () => void) {}
|
|
9
|
+
|
|
10
|
+
start(): void {
|
|
11
|
+
this.rootNodes = [];
|
|
12
|
+
this.stack = [];
|
|
13
|
+
this.onUpdate?.();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
clear(): void {
|
|
17
|
+
const now = getNowTime();
|
|
18
|
+
// Reset each in-progress node: fresh startTime, no accumulated children
|
|
19
|
+
for (const node of this.stack) {
|
|
20
|
+
node.startTime = now;
|
|
21
|
+
node.children = node.children.filter((x) => x.endTime === undefined);
|
|
22
|
+
}
|
|
23
|
+
// Re-wire parent→child links through the stack chain
|
|
24
|
+
for (let i = 1; i < this.stack.length; i++) {
|
|
25
|
+
this.stack[i - 1]!.children = [this.stack[i]!];
|
|
26
|
+
}
|
|
27
|
+
// rootNodes keeps only the outermost in-progress node (if any)
|
|
28
|
+
this.rootNodes = this.stack.slice(0, 1);
|
|
29
|
+
this.onUpdate?.();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private cloneNode(node: ProfilerNode, snapshotTime?: number): ProfilerNode {
|
|
33
|
+
const children = node.children.map((c) => this.cloneNode(c, snapshotTime));
|
|
34
|
+
const endTime =
|
|
35
|
+
node.endTime ?? (snapshotTime !== undefined ? snapshotTime : undefined);
|
|
36
|
+
const value =
|
|
37
|
+
node.value ??
|
|
38
|
+
(endTime !== undefined && node.startTime !== undefined
|
|
39
|
+
? Math.ceil((endTime - node.startTime) * 1000)
|
|
40
|
+
: children.reduce((prev, current) => prev + (current?.value ?? 0), 0));
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
...node,
|
|
44
|
+
endTime,
|
|
45
|
+
value,
|
|
46
|
+
children,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getSnapshot(): { rootNodes: ProfilerNode[] } {
|
|
51
|
+
const now = getNowTime();
|
|
52
|
+
return {
|
|
53
|
+
rootNodes: this.rootNodes.map((n) => this.cloneNode(n, now)),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
startTimer(hookName: string): void {
|
|
58
|
+
const node: ProfilerNode = {
|
|
59
|
+
name: hookName,
|
|
60
|
+
startTime: getNowTime(),
|
|
61
|
+
children: [],
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
if (this.stack.length > 0) {
|
|
65
|
+
this.stack[this.stack.length - 1]!.children.push(node);
|
|
66
|
+
} else {
|
|
67
|
+
this.rootNodes.push(node);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.stack.push(node);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private finalizeNode(node: ProfilerNode, endTime: number): void {
|
|
74
|
+
const duration =
|
|
75
|
+
node.startTime !== undefined ? endTime - node.startTime : 0.01;
|
|
76
|
+
node.endTime = endTime;
|
|
77
|
+
node.value = Math.ceil(duration * 1000);
|
|
78
|
+
node.tooltip = `${node.name}, ${duration.toFixed(4)} (ms)`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
endTimer({ hookName }: { hookName: string }): void {
|
|
82
|
+
const idx = [...this.stack].reverse().findIndex((n) => n.name === hookName);
|
|
83
|
+
|
|
84
|
+
if (idx === -1) {
|
|
85
|
+
console.warn(`endTimer: '${hookName}' not found in stack, ignoring`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// stack index of the target (reverse idx → forward idx)
|
|
90
|
+
const targetIdx = this.stack.length - 1 - idx;
|
|
91
|
+
const endTime = getNowTime();
|
|
92
|
+
|
|
93
|
+
// Pop and finalize everything above the target, from top down
|
|
94
|
+
for (let i = this.stack.length - 1; i > targetIdx; i--) {
|
|
95
|
+
const orphan = this.stack[i]!;
|
|
96
|
+
console.warn(
|
|
97
|
+
`endTimer: popping '${orphan.name}' — timer was never explicitly ended`,
|
|
98
|
+
);
|
|
99
|
+
this.finalizeNode(orphan, endTime);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Finalize the target
|
|
103
|
+
this.finalizeNode(this.stack[targetIdx]!, endTime);
|
|
104
|
+
|
|
105
|
+
// Truncate stack
|
|
106
|
+
this.stack.length = targetIdx;
|
|
107
|
+
|
|
108
|
+
this.onUpdate?.();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
stopProfiler(): { rootNodes: ProfilerNode[] } {
|
|
112
|
+
return { rootNodes: [...this.rootNodes] };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ProfilerNode } from "../types";
|
|
2
|
+
|
|
3
|
+
const createSpacer = (gap: number) => ({
|
|
4
|
+
name: "(work)",
|
|
5
|
+
value: Math.ceil(gap * 1000),
|
|
6
|
+
children: [],
|
|
7
|
+
backgroundColor: "#000000",
|
|
8
|
+
color: "#000000",
|
|
9
|
+
tooltip: "Placeholder time between hooks",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const transformProfilerData = (root: ProfilerNode): ProfilerNode => {
|
|
13
|
+
return {
|
|
14
|
+
...root,
|
|
15
|
+
children: transformProfilerDataHelper(root.children, root.startTime),
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const transformProfilerDataHelper = (
|
|
20
|
+
nodes: ProfilerNode[],
|
|
21
|
+
parentStart: number = 0,
|
|
22
|
+
): ProfilerNode[] => {
|
|
23
|
+
const merged: ProfilerNode[] = [];
|
|
24
|
+
let cursor = parentStart;
|
|
25
|
+
|
|
26
|
+
for (const node of nodes) {
|
|
27
|
+
if (
|
|
28
|
+
node.startTime === undefined ||
|
|
29
|
+
node.endTime === undefined ||
|
|
30
|
+
node.value === undefined ||
|
|
31
|
+
node.value <= 0
|
|
32
|
+
) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const next = {
|
|
37
|
+
...node,
|
|
38
|
+
children: transformProfilerDataHelper(node.children, node.startTime),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const gap = node.startTime - cursor;
|
|
42
|
+
if (gap > 0) {
|
|
43
|
+
merged.push(createSpacer(gap));
|
|
44
|
+
}
|
|
45
|
+
cursor = node.endTime;
|
|
46
|
+
|
|
47
|
+
merged.push(next);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return merged;
|
|
51
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./plugin";
|