@ogygia/insights 0.0.0 → 0.14.0
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/LICENSE +21 -0
- package/README.md +37 -1
- package/dist/attr-rules.d.ts +15 -0
- package/dist/attr-rules.js +27 -0
- package/dist/barrel-left.d.ts +6 -0
- package/dist/barrel-left.js +34 -0
- package/dist/cpu.d.ts +82 -0
- package/dist/cpu.js +177 -0
- package/dist/fp-drift.d.ts +28 -0
- package/dist/fp-drift.js +170 -0
- package/dist/hole-segments.d.ts +26 -0
- package/dist/hole-segments.js +50 -0
- package/dist/html-diff.d.ts +25 -0
- package/dist/html-diff.js +156 -0
- package/dist/hydration-status.d.ts +2 -0
- package/dist/hydration-status.js +0 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +12 -0
- package/dist/page-assets.d.ts +211 -0
- package/dist/page-assets.js +565 -0
- package/dist/page-insights.d.ts +744 -0
- package/dist/page-insights.js +2289 -0
- package/dist/session-data.d.ts +124 -0
- package/dist/session-data.js +0 -0
- package/dist/session-insights.d.ts +121 -0
- package/dist/session-insights.js +575 -0
- package/dist/testing-version.d.ts +19 -0
- package/dist/testing-version.js +21 -0
- package/dist/third-party.d.ts +75 -0
- package/dist/third-party.js +168 -0
- package/package.json +58 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Puru Vijayvargia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
1
|
# @ogygia/insights
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The analysis engine behind ogygia's tools. It takes what the browser measured on a page visit, or
|
|
4
|
+
in a recorded session, and turns it into findings. Plain functions, no UI.
|
|
5
|
+
|
|
6
|
+
Published, because two published packages depend on it: `@ogygia/devtools` (the dock and the
|
|
7
|
+
profiler's report) and `@ogygia/testing`. One engine means the dock, the profiler and your tests
|
|
8
|
+
say the same thing about the same page. The docs app uses it too. Apps rarely import it themselves.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @ogygia/insights
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Use
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { html_diff, rate } from '@ogygia/insights';
|
|
20
|
+
|
|
21
|
+
// what changed between the server's HTML and what the browser ended up with
|
|
22
|
+
const d = html_diff('<p>rendered on the server</p>', '<p>rendered on the browser</p>');
|
|
23
|
+
d.removed; // 1
|
|
24
|
+
d.added; // 1
|
|
25
|
+
|
|
26
|
+
rate('lcp', 3100); // 'fair' (good up to 2.5 s, poor from 4 s)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The bigger entry points are `analyze_page()` (one visit) and `analyze_session()` (a recording).
|
|
30
|
+
|
|
31
|
+
## Work on it
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm --filter @ogygia/insights test # vitest
|
|
35
|
+
pnpm --filter @ogygia/insights check # tsc on src and test, then vitest
|
|
36
|
+
pnpm --filter @ogygia/insights build # tsdown
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Bench files are in `bench/`.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AttrChange } from "@ogygia/core/internal/instruments";
|
|
2
|
+
export type Pat = string | {
|
|
3
|
+
re: string;
|
|
4
|
+
flags?: string;
|
|
5
|
+
};
|
|
6
|
+
export interface AttrRule {
|
|
7
|
+
tag?: Pat;
|
|
8
|
+
name: Pat;
|
|
9
|
+
value?: Pat;
|
|
10
|
+
}
|
|
11
|
+
/** Split `changes` into those `rules` ignore and those left. */
|
|
12
|
+
export declare function apply_attr_rules(changes: AttrChange[], rules: AttrRule[]): {
|
|
13
|
+
left: AttrChange[];
|
|
14
|
+
ignored: AttrChange[];
|
|
15
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/attr-rules.ts
|
|
2
|
+
function matcher(p) {
|
|
3
|
+
if (p === void 0) return () => true;
|
|
4
|
+
if (typeof p === "string") return (s) => s === p;
|
|
5
|
+
const re = new RegExp(p.re, p.flags);
|
|
6
|
+
return (s) => s !== null && re.test(s);
|
|
7
|
+
}
|
|
8
|
+
/** Split `changes` into those `rules` ignore and those left. */
|
|
9
|
+
function apply_attr_rules(changes, rules) {
|
|
10
|
+
if (!rules.length) return {
|
|
11
|
+
left: changes,
|
|
12
|
+
ignored: []
|
|
13
|
+
};
|
|
14
|
+
const ms = rules.map((r) => ({
|
|
15
|
+
tag: matcher(r.tag),
|
|
16
|
+
name: matcher(r.name),
|
|
17
|
+
value: matcher(r.value)
|
|
18
|
+
}));
|
|
19
|
+
const left = [];
|
|
20
|
+
const ignored = [];
|
|
21
|
+
for (const c of changes) (ms.some((m) => m.tag(c.tag) && m.name(c.name) && m.value(c.to ?? c.from)) ? ignored : left).push(c);
|
|
22
|
+
return {
|
|
23
|
+
left,
|
|
24
|
+
ignored
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export { apply_attr_rules };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { BarrelLeftReason, ChunkBarrel, IslandBarrel } from "@ogygia/core/internal/instruments";
|
|
2
|
+
/** `barrels() left it: …`, or '' when the pass said nothing about this barrel. */
|
|
3
|
+
export declare function barrel_left_words(why: readonly BarrelLeftReason[] | undefined): string;
|
|
4
|
+
/** The fix for a barrel an island imports whole: by its reasons, or (none known) add the pass. */
|
|
5
|
+
export declare function barrel_left_fix(why: readonly BarrelLeftReason[] | undefined): string;
|
|
6
|
+
export type { BarrelLeftReason, ChunkBarrel, IslandBarrel };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/barrel-left.ts
|
|
2
|
+
const WORDS = {
|
|
3
|
+
impure: "it runs code of its own besides its re-exports",
|
|
4
|
+
kept: "barrels({ keep }) lists it",
|
|
5
|
+
"outside-project": "it is a package, and barrels({ packages }) does not list it",
|
|
6
|
+
opaque: "it re-exports from a module that cannot be read, so some names have no known home",
|
|
7
|
+
own: "the names are defined in the barrel itself",
|
|
8
|
+
ambiguous: "two of its `export *` lines give the same name",
|
|
9
|
+
pinned: "an import of it marks a region, and a region import stays as written",
|
|
10
|
+
namespace: "it is imported whole (`import * as`)",
|
|
11
|
+
unparsable: "a component that imports it could not be parsed"
|
|
12
|
+
};
|
|
13
|
+
const FIXES = {
|
|
14
|
+
impure: "move the barrel's own code into a module of its own, or list it in barrels({ force })",
|
|
15
|
+
kept: "take it out of barrels({ keep })",
|
|
16
|
+
"outside-project": "list the package in barrels({ packages })",
|
|
17
|
+
opaque: "import those names from their own files",
|
|
18
|
+
own: "move those names into files of their own",
|
|
19
|
+
ambiguous: "export the name from one place only",
|
|
20
|
+
pinned: "import the region from its own file",
|
|
21
|
+
namespace: "import the names the island uses, not the whole barrel",
|
|
22
|
+
unparsable: "fix the component the build log names (barrels_component_skipped)"
|
|
23
|
+
};
|
|
24
|
+
/** `barrels() left it: …`, or '' when the pass said nothing about this barrel. */
|
|
25
|
+
function barrel_left_words(why) {
|
|
26
|
+
return why?.length ? `barrels() left it: ${why.map((r) => WORDS[r]).join("; ")}` : "";
|
|
27
|
+
}
|
|
28
|
+
/** The fix for a barrel an island imports whole: by its reasons, or (none known) add the pass. */
|
|
29
|
+
function barrel_left_fix(why) {
|
|
30
|
+
const first = "Import what the island uses from its own file";
|
|
31
|
+
if (!why?.length) return `${first}, or add barrels() from @ogygia/barrels to the Vite plugins, beside ogygia().`;
|
|
32
|
+
return `${first}; or ${why.map((r) => FIXES[r]).join("; or ")}.`;
|
|
33
|
+
}
|
|
34
|
+
export { barrel_left_fix, barrel_left_words };
|
package/dist/cpu.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The main thread, from the browser's own sampler (the JS Self-Profiling API; runtime/beacon.ts keeps
|
|
3
|
+
* the trace in the page on a devtools build). Pure: a trace + the islands' hydrate windows in, out:
|
|
4
|
+
* busy time by kind of code (the app, ogygia, Svelte, packages, a page script, the browser itself),
|
|
5
|
+
* the functions and files that ran, the CPU inside each island's hydrate window and what it was,
|
|
6
|
+
* and what ran in long tasks outside any island. The Page tab shows it; the findings quote it.
|
|
7
|
+
*
|
|
8
|
+
* The trace's timestamps share `performance.now()`'s clock, so the windows line up with the
|
|
9
|
+
* beacon's island timings without conversion.
|
|
10
|
+
*/
|
|
11
|
+
export interface SelfProfileTrace {
|
|
12
|
+
frames: {
|
|
13
|
+
name?: string;
|
|
14
|
+
resourceId?: number;
|
|
15
|
+
line?: number;
|
|
16
|
+
column?: number;
|
|
17
|
+
}[];
|
|
18
|
+
resources: string[];
|
|
19
|
+
samples: {
|
|
20
|
+
timestamp: number;
|
|
21
|
+
stackId?: number;
|
|
22
|
+
}[];
|
|
23
|
+
stacks: {
|
|
24
|
+
frameId: number;
|
|
25
|
+
parentId?: number;
|
|
26
|
+
}[];
|
|
27
|
+
}
|
|
28
|
+
export type CodeKind = "app" | "ogygia" | "svelte" | "dependency" | "page script" | "browser" | "devtools";
|
|
29
|
+
export interface CpuFn {
|
|
30
|
+
name: string;
|
|
31
|
+
file: string;
|
|
32
|
+
line: number | null;
|
|
33
|
+
kind: CodeKind;
|
|
34
|
+
self_ms: number;
|
|
35
|
+
total_ms: number;
|
|
36
|
+
}
|
|
37
|
+
export interface CpuSummary {
|
|
38
|
+
/** sampled span (first to last sample) */
|
|
39
|
+
window_ms: number;
|
|
40
|
+
busy_ms: number;
|
|
41
|
+
by_kind: {
|
|
42
|
+
kind: CodeKind;
|
|
43
|
+
ms: number;
|
|
44
|
+
}[];
|
|
45
|
+
fns: CpuFn[];
|
|
46
|
+
files: {
|
|
47
|
+
file: string;
|
|
48
|
+
kind: CodeKind;
|
|
49
|
+
ms: number;
|
|
50
|
+
}[];
|
|
51
|
+
/** per island fingerprint: CPU inside its hydrate window and the functions that took it */
|
|
52
|
+
islands: Record<string, {
|
|
53
|
+
ms: number;
|
|
54
|
+
top: CpuFn[];
|
|
55
|
+
}>;
|
|
56
|
+
/** long tasks outside every island's hydrate window */
|
|
57
|
+
outside: {
|
|
58
|
+
ms: number;
|
|
59
|
+
top: CpuFn[];
|
|
60
|
+
};
|
|
61
|
+
/** the sampling interval seen (ms) */
|
|
62
|
+
interval_ms: number;
|
|
63
|
+
/** main-thread ms per other origin (the nearest frame with a script URL decides) */
|
|
64
|
+
by_host?: Record<string, number>;
|
|
65
|
+
}
|
|
66
|
+
export interface IslandWindow {
|
|
67
|
+
fp: string;
|
|
68
|
+
from: number;
|
|
69
|
+
to: number;
|
|
70
|
+
}
|
|
71
|
+
/** A frame's script, readable: origin, Vite's `/@fs` / `/@id/` prefixes and `?v=` queries off. */
|
|
72
|
+
export declare function clean_file(url: string, page_origin?: string): string;
|
|
73
|
+
/** What kind of code a frame is, from its script URL (and name, for the browser's own). */
|
|
74
|
+
export declare function kind_of(url: string, page_url: string): CodeKind;
|
|
75
|
+
export declare function analyze_cpu(trace: SelfProfileTrace, windows: IslandWindow[], longtasks: {
|
|
76
|
+
t: number;
|
|
77
|
+
ms: number;
|
|
78
|
+
}[], page_url: string): CpuSummary;
|
|
79
|
+
/** `name (file:line)` for a finding. */
|
|
80
|
+
export declare function fn_label(f: CpuFn): string;
|
|
81
|
+
/** Is this a trace the analyzer can read. */
|
|
82
|
+
export declare function is_trace(x: unknown): x is SelfProfileTrace;
|
package/dist/cpu.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// src/cpu.ts
|
|
2
|
+
/** A frame's script, readable: origin, Vite's `/@fs` / `/@id/` prefixes and `?v=` queries off. */
|
|
3
|
+
function clean_file(url, page_origin = "") {
|
|
4
|
+
let u = url;
|
|
5
|
+
if (page_origin && u.startsWith(page_origin)) u = u.slice(page_origin.length);
|
|
6
|
+
const q = u.indexOf("?");
|
|
7
|
+
if (q !== -1) u = u.slice(0, q);
|
|
8
|
+
if (u.startsWith("/@fs")) u = u.slice(4);
|
|
9
|
+
if (u.startsWith("/@id/")) u = u.slice(5);
|
|
10
|
+
if (u.startsWith("__x00__")) u = u.slice(7);
|
|
11
|
+
for (const mark of [
|
|
12
|
+
"/node_modules/.vite/deps/",
|
|
13
|
+
"/node_modules/",
|
|
14
|
+
"/packages/",
|
|
15
|
+
"/src/"
|
|
16
|
+
]) {
|
|
17
|
+
const at = u.lastIndexOf(mark);
|
|
18
|
+
if (at !== -1) return mark === "/src/" ? u.slice(at + 1) : u.slice(at + (mark === "/packages/" ? 10 : mark.length));
|
|
19
|
+
}
|
|
20
|
+
return u;
|
|
21
|
+
}
|
|
22
|
+
/** ogygia's own instruments: the devtools package (the dock, the beacon), the event layer, the analysis engine */
|
|
23
|
+
const OWN_INSTRUMENT_RE = /\/(?:(?:ogygia|@ogygia\/core|packages\/core)\/(?:src|dist)\/(?:devtools|events|insights)|(?:@ogygia|packages)\/devtools\/(?:src|dist))\//;
|
|
24
|
+
/** What kind of code a frame is, from its script URL (and name, for the browser's own). */
|
|
25
|
+
function kind_of(url, page_url) {
|
|
26
|
+
if (!url) return "browser";
|
|
27
|
+
const path = url.split("?")[0];
|
|
28
|
+
if (page_url && path === page_url.split("?")[0].split("#")[0]) return "page script";
|
|
29
|
+
if (path.includes("/.vite/deps/")) return path.slice(path.lastIndexOf("/") + 1).startsWith("svelte") ? "svelte" : "dependency";
|
|
30
|
+
if (path.includes("/svelte/src/") || path.includes("/node_modules/svelte/")) return "svelte";
|
|
31
|
+
if (OWN_INSTRUMENT_RE.test(path)) return "devtools";
|
|
32
|
+
if (path.includes("/packages/ogygia/") || path.includes("/packages/core/") || path.includes("/node_modules/ogygia/") || path.includes("/node_modules/@ogygia/core/") || path.includes("ogygia:")) return "ogygia";
|
|
33
|
+
if (path.includes("/node_modules/")) return "dependency";
|
|
34
|
+
if (path.includes("/src/")) return "app";
|
|
35
|
+
return "dependency";
|
|
36
|
+
}
|
|
37
|
+
const round1 = (n) => Math.round(n * 10) / 10;
|
|
38
|
+
function analyze_cpu(trace, windows, longtasks, page_url) {
|
|
39
|
+
const { frames, resources, samples, stacks } = trace;
|
|
40
|
+
const origin = page_url ? new URL(page_url).origin : "";
|
|
41
|
+
const gaps = [];
|
|
42
|
+
for (let i = 1; i < samples.length; i++) gaps.push(samples[i].timestamp - samples[i - 1].timestamp);
|
|
43
|
+
gaps.sort((a, b) => a - b);
|
|
44
|
+
const interval = gaps.length ? Math.max(1, gaps[gaps.length >> 1]) : 10;
|
|
45
|
+
const cap = interval * 3;
|
|
46
|
+
const fn_self = /* @__PURE__ */ new Map();
|
|
47
|
+
const fn_total = /* @__PURE__ */ new Map();
|
|
48
|
+
const kind_ms = /* @__PURE__ */ new Map();
|
|
49
|
+
const file_ms = /* @__PURE__ */ new Map();
|
|
50
|
+
const host_ms = /* @__PURE__ */ new Map();
|
|
51
|
+
const page_host = host_of(page_url);
|
|
52
|
+
const win_self = windows.map(() => /* @__PURE__ */ new Map());
|
|
53
|
+
const win_ms = windows.map(() => 0);
|
|
54
|
+
const out_self = /* @__PURE__ */ new Map();
|
|
55
|
+
let out_ms = 0;
|
|
56
|
+
let busy = 0;
|
|
57
|
+
const frame_kind = (fid) => {
|
|
58
|
+
const f = frames[fid];
|
|
59
|
+
return kind_of(f?.resourceId !== void 0 ? resources[f.resourceId] ?? "" : "", page_url);
|
|
60
|
+
};
|
|
61
|
+
for (let i = 0; i < samples.length; i++) {
|
|
62
|
+
const s = samples[i];
|
|
63
|
+
if (s.stackId === void 0) continue;
|
|
64
|
+
const next = samples[i + 1];
|
|
65
|
+
const dt = next ? Math.min(cap, Math.max(0, next.timestamp - s.timestamp)) : interval;
|
|
66
|
+
if (dt <= 0) continue;
|
|
67
|
+
busy += dt;
|
|
68
|
+
const leaf = stacks[s.stackId]?.frameId;
|
|
69
|
+
if (leaf === void 0) continue;
|
|
70
|
+
fn_self.set(leaf, (fn_self.get(leaf) ?? 0) + dt);
|
|
71
|
+
const k = frame_kind(leaf);
|
|
72
|
+
kind_ms.set(k, (kind_ms.get(k) ?? 0) + dt);
|
|
73
|
+
const lf = frames[leaf];
|
|
74
|
+
if (lf?.resourceId !== void 0) {
|
|
75
|
+
const file = clean_file(resources[lf.resourceId] ?? "", origin);
|
|
76
|
+
const e = file_ms.get(file) ?? {
|
|
77
|
+
kind: k,
|
|
78
|
+
ms: 0
|
|
79
|
+
};
|
|
80
|
+
e.ms += dt;
|
|
81
|
+
file_ms.set(file, e);
|
|
82
|
+
}
|
|
83
|
+
for (let sid = s.stackId, guard = 0; sid !== void 0 && guard < 512; guard++) {
|
|
84
|
+
const st = stacks[sid];
|
|
85
|
+
if (!st) break;
|
|
86
|
+
const rid = frames[st.frameId]?.resourceId;
|
|
87
|
+
if (rid !== void 0) {
|
|
88
|
+
const host = host_of(resources[rid] ?? "");
|
|
89
|
+
if (host && host !== page_host) host_ms.set(host, (host_ms.get(host) ?? 0) + dt);
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
sid = st.parentId;
|
|
93
|
+
}
|
|
94
|
+
const seen = /* @__PURE__ */ new Set();
|
|
95
|
+
for (let sid = s.stackId, guard = 0; sid !== void 0 && guard < 512; guard++) {
|
|
96
|
+
const st = stacks[sid];
|
|
97
|
+
if (!st) break;
|
|
98
|
+
if (!seen.has(st.frameId)) {
|
|
99
|
+
seen.add(st.frameId);
|
|
100
|
+
fn_total.set(st.frameId, (fn_total.get(st.frameId) ?? 0) + dt);
|
|
101
|
+
}
|
|
102
|
+
sid = st.parentId;
|
|
103
|
+
}
|
|
104
|
+
let hit = false;
|
|
105
|
+
for (let w = 0; w < windows.length; w++) if (s.timestamp >= windows[w].from && s.timestamp <= windows[w].to) {
|
|
106
|
+
win_ms[w] += dt;
|
|
107
|
+
win_self[w].set(leaf, (win_self[w].get(leaf) ?? 0) + dt);
|
|
108
|
+
hit = true;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (!hit && longtasks.some((t) => s.timestamp >= t.t && s.timestamp <= t.t + t.ms)) {
|
|
112
|
+
out_ms += dt;
|
|
113
|
+
out_self.set(leaf, (out_self.get(leaf) ?? 0) + dt);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const fn_of = (fid, self, total) => {
|
|
117
|
+
const f = frames[fid] ?? {};
|
|
118
|
+
const url = f.resourceId !== void 0 ? resources[f.resourceId] ?? "" : "";
|
|
119
|
+
const path = url.split("?")[0];
|
|
120
|
+
const stem = path.endsWith(".svelte") ? path.slice(path.lastIndexOf("/") + 1, -7) : "";
|
|
121
|
+
return {
|
|
122
|
+
name: f.name || stem || (url ? "(anonymous)" : "(browser)"),
|
|
123
|
+
file: url ? clean_file(url, origin) : "",
|
|
124
|
+
line: typeof f.line === "number" ? f.line : null,
|
|
125
|
+
kind: frame_kind(fid),
|
|
126
|
+
self_ms: round1(self),
|
|
127
|
+
total_ms: round1(total)
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
const top = (m, n) => [...m].sort((a, b) => b[1] - a[1]).slice(0, n).map(([fid, ms]) => fn_of(fid, ms, fn_total.get(fid) ?? ms));
|
|
131
|
+
const islands = {};
|
|
132
|
+
windows.forEach((w, i) => {
|
|
133
|
+
if (win_ms[i] > 0) islands[w.fp] = {
|
|
134
|
+
ms: round1(win_ms[i]),
|
|
135
|
+
top: top(win_self[i], 3)
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
return {
|
|
139
|
+
window_ms: samples.length ? round1(samples[samples.length - 1].timestamp - samples[0].timestamp) : 0,
|
|
140
|
+
busy_ms: round1(busy),
|
|
141
|
+
by_kind: [...kind_ms].map(([kind, ms]) => ({
|
|
142
|
+
kind,
|
|
143
|
+
ms: round1(ms)
|
|
144
|
+
})).sort((a, b) => b.ms - a.ms),
|
|
145
|
+
fns: top(new Map([...fn_self].filter(([fid]) => frame_kind(fid) !== "devtools")), 25),
|
|
146
|
+
files: [...file_ms].map(([file, e]) => ({
|
|
147
|
+
file,
|
|
148
|
+
kind: e.kind,
|
|
149
|
+
ms: round1(e.ms)
|
|
150
|
+
})).sort((a, b) => b.ms - a.ms).slice(0, 15),
|
|
151
|
+
islands,
|
|
152
|
+
outside: {
|
|
153
|
+
ms: round1(out_ms),
|
|
154
|
+
top: top(out_self, 3)
|
|
155
|
+
},
|
|
156
|
+
interval_ms: round1(interval),
|
|
157
|
+
by_host: Object.fromEntries([...host_ms].map(([h, ms]) => [h, round1(ms)]))
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function host_of(url) {
|
|
161
|
+
try {
|
|
162
|
+
return url ? new URL(url).host : "";
|
|
163
|
+
} catch {
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** `name (file:line)` for a finding. */
|
|
168
|
+
function fn_label(f) {
|
|
169
|
+
const where = f.file ? ` (${f.file.slice(f.file.lastIndexOf("/") + 1)}${f.line !== null ? `:${f.line}` : ""})` : "";
|
|
170
|
+
return `${f.name}${where}`;
|
|
171
|
+
}
|
|
172
|
+
/** Is this a trace the analyzer can read. */
|
|
173
|
+
function is_trace(x) {
|
|
174
|
+
const t = x;
|
|
175
|
+
return !!t && Array.isArray(t.frames) && Array.isArray(t.samples) && Array.isArray(t.stacks) && Array.isArray(t.resources);
|
|
176
|
+
}
|
|
177
|
+
export { analyze_cpu, clean_file, fn_label, is_trace, kind_of };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface FpDrift {
|
|
2
|
+
entry: string;
|
|
3
|
+
fp_was: string;
|
|
4
|
+
fp_now: string;
|
|
5
|
+
/** the first differing path in the props (`stamp`, `items[2].id`), when the props parse */
|
|
6
|
+
path?: string;
|
|
7
|
+
/** the values there, clipped (or the text around the first difference) */
|
|
8
|
+
was?: string;
|
|
9
|
+
now?: string;
|
|
10
|
+
}
|
|
11
|
+
/** One island as one load (or render) had it. */
|
|
12
|
+
export interface KeptIsland {
|
|
13
|
+
entry: string;
|
|
14
|
+
fp: string;
|
|
15
|
+
props?: string;
|
|
16
|
+
}
|
|
17
|
+
type Kept = KeptIsland;
|
|
18
|
+
/** Islands matched by entry and their place among its copies; a moved fingerprint, explained. */
|
|
19
|
+
export declare function compare(was: Kept[], now: Kept[]): FpDrift[];
|
|
20
|
+
/** Where two props texts differ: the first differing path when both parse as JSON, else the text
|
|
21
|
+
* around the first differing character — what is random in ogygia's own ids (a store's, a class
|
|
22
|
+
* instance's: the render's fence, by design) left out. `null` when that is all that differs. */
|
|
23
|
+
export declare function props_diff(a: string, b: string): {
|
|
24
|
+
path?: string;
|
|
25
|
+
was?: string;
|
|
26
|
+
now?: string;
|
|
27
|
+
} | null;
|
|
28
|
+
export {};
|
package/dist/fp-drift.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { unflatten } from "devalue";
|
|
2
|
+
// src/fp-drift.ts
|
|
3
|
+
/**
|
|
4
|
+
* ISLANDS WHOSE FINGERPRINT MOVES BETWEEN TWO LOADS OF THE SAME PAGE. An island's `data-og-fp` is
|
|
5
|
+
* the hash of its props: what the router compares to keep a live island across a navigation, and
|
|
6
|
+
* part of the page's bytes (a host's post-render cache, a postrender store, an ETag key on them). A prop
|
|
7
|
+
* made fresh on every render — a time, a random id — moves it every time, so the island is patched on
|
|
8
|
+
* every navigation and every such cache misses. ogygia's own ids are stable now; this finds the
|
|
9
|
+
* app's.
|
|
10
|
+
*
|
|
11
|
+
* Per tab (sessionStorage): each page's islands as last loaded — entry, fingerprint, props text.
|
|
12
|
+
* On the next load of the same page (or an in-app navigation back to it), each island is matched by
|
|
13
|
+
* its entry and its place among copies of it; a moved fingerprint is named with the prop that
|
|
14
|
+
* differs (the first differing path on the JSON lane, else the first differing text).
|
|
15
|
+
*
|
|
16
|
+
* PURE (no DOM): the devtools record each load (page.ts `fp_drift`), the profiler compares its own
|
|
17
|
+
* renders of a page (report.ts) — one comparison, one rule.
|
|
18
|
+
*/
|
|
19
|
+
/** Islands matched by entry and their place among its copies; a moved fingerprint, explained. */
|
|
20
|
+
function compare(was, now) {
|
|
21
|
+
const nth = (list) => {
|
|
22
|
+
const seen = /* @__PURE__ */ new Map();
|
|
23
|
+
return list.map((k) => {
|
|
24
|
+
const n = seen.get(k.entry) ?? 0;
|
|
25
|
+
seen.set(k.entry, n + 1);
|
|
26
|
+
return `${k.entry}#${n}`;
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const before = new Map(nth(was).map((key, i) => [key, was[i]]));
|
|
30
|
+
const out = [];
|
|
31
|
+
const keys = nth(now);
|
|
32
|
+
for (let i = 0; i < now.length; i++) {
|
|
33
|
+
const a = before.get(keys[i]);
|
|
34
|
+
const b = now[i];
|
|
35
|
+
if (!a || a.fp === b.fp) continue;
|
|
36
|
+
if (a.props !== void 0 && b.props !== void 0) {
|
|
37
|
+
const d = props_diff(a.props, b.props);
|
|
38
|
+
if (!d) continue;
|
|
39
|
+
out.push({
|
|
40
|
+
entry: b.entry,
|
|
41
|
+
fp_was: a.fp,
|
|
42
|
+
fp_now: b.fp,
|
|
43
|
+
...d
|
|
44
|
+
});
|
|
45
|
+
} else out.push({
|
|
46
|
+
entry: b.entry,
|
|
47
|
+
fp_was: a.fp,
|
|
48
|
+
fp_now: b.fp
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
/** Where two props texts differ: the first differing path when both parse as JSON, else the text
|
|
54
|
+
* around the first differing character — what is random in ogygia's own ids (a store's, a class
|
|
55
|
+
* instance's: the render's fence, by design) left out. `null` when that is all that differs. */
|
|
56
|
+
function props_diff(a, b) {
|
|
57
|
+
if (a === b) return null;
|
|
58
|
+
try {
|
|
59
|
+
return first_path(revive(JSON.parse(a)), revive(JSON.parse(b)), "");
|
|
60
|
+
} catch {}
|
|
61
|
+
const ma = mask_ids(a);
|
|
62
|
+
const mb = mask_ids(b);
|
|
63
|
+
if (ma === mb) return null;
|
|
64
|
+
let i = 0;
|
|
65
|
+
while (i < ma.length && i < mb.length && ma.charCodeAt(i) === mb.charCodeAt(i)) i++;
|
|
66
|
+
const from = Math.max(0, i - 20);
|
|
67
|
+
return {
|
|
68
|
+
was: ma.slice(from, i + 40),
|
|
69
|
+
now: mb.slice(from, i + 40)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The props as values, for naming a path. Props are an object, so a parsed ARRAY is the devalue
|
|
74
|
+
* lane's flat table — `[7]` would name a slot, not a prop: unflattened back into the props, every
|
|
75
|
+
* custom type (a transportable, a ref) kept as `{ "<type>": value }` (any reviver, so none throws).
|
|
76
|
+
*/
|
|
77
|
+
function revive(parsed) {
|
|
78
|
+
return Array.isArray(parsed) ? unflatten(parsed, ANY_REVIVER) : parsed;
|
|
79
|
+
}
|
|
80
|
+
const keep_typed = (type) => (value) => ({ [`<${type}>`]: value });
|
|
81
|
+
/** A revivers table that has every type (devalue asks `Object.hasOwn`, then calls it). */
|
|
82
|
+
const ANY_REVIVER = new Proxy({}, {
|
|
83
|
+
getOwnPropertyDescriptor: (_, k) => ({
|
|
84
|
+
value: keep_typed(String(k)),
|
|
85
|
+
configurable: true,
|
|
86
|
+
enumerable: true,
|
|
87
|
+
writable: false
|
|
88
|
+
}),
|
|
89
|
+
get: (_, k) => keep_typed(String(k))
|
|
90
|
+
});
|
|
91
|
+
/** ogygia's mint id: a UUID (`8-4-4-4-12` hex). */
|
|
92
|
+
function is_mint_id(v) {
|
|
93
|
+
if (typeof v !== "string" || v.length !== 36) return false;
|
|
94
|
+
for (let i = 0; i < 36; i++) {
|
|
95
|
+
const c = v.charCodeAt(i);
|
|
96
|
+
if (i === 8 || i === 13 || i === 18 || i === 23) {
|
|
97
|
+
if (c !== 45) return false;
|
|
98
|
+
} else if (!(c >= 48 && c <= 57 || c >= 97 && c <= 102)) return false;
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/** A server render's ref id (ref.ts RefNames): its fence (16 hex, random per render), `~`, its
|
|
103
|
+
* place. Only the place says anything about the props. */
|
|
104
|
+
function fence_end(v, at) {
|
|
105
|
+
if (v.charCodeAt(at + 16) !== 126) return -1;
|
|
106
|
+
for (let i = at; i < at + 16; i++) {
|
|
107
|
+
const c = v.charCodeAt(i);
|
|
108
|
+
if (!(c >= 48 && c <= 57 || c >= 97 && c <= 102)) return -1;
|
|
109
|
+
}
|
|
110
|
+
return at + 16;
|
|
111
|
+
}
|
|
112
|
+
const is_named_id = (v) => typeof v === "string" && v.length > 17 && fence_end(v, 0) === 16;
|
|
113
|
+
/** Two ids that name the same thing to the fingerprint: both random, or the same place. */
|
|
114
|
+
function same_ref(a, b) {
|
|
115
|
+
if (is_mint_id(a) && is_mint_id(b)) return true;
|
|
116
|
+
return is_named_id(a) && is_named_id(b) && a.slice(16) === b.slice(16);
|
|
117
|
+
}
|
|
118
|
+
/** The text with every UUID replaced by a fixed one, and every render's fence dropped (for the
|
|
119
|
+
* non-JSON compare). */
|
|
120
|
+
function mask_ids(s) {
|
|
121
|
+
const out = [];
|
|
122
|
+
let last = 0;
|
|
123
|
+
for (let i = 0; i + 17 <= s.length; i++) if (i + 36 <= s.length && s.charCodeAt(i + 8) === 45 && is_mint_id(s.slice(i, i + 36))) {
|
|
124
|
+
out.push(s.slice(last, i), "<id>");
|
|
125
|
+
last = i + 36;
|
|
126
|
+
i += 35;
|
|
127
|
+
} else if (s.charCodeAt(i + 16) === 126 && fence_end(s, i) !== -1) {
|
|
128
|
+
out.push(s.slice(last, i));
|
|
129
|
+
last = i + 16;
|
|
130
|
+
i += 15;
|
|
131
|
+
}
|
|
132
|
+
if (last === 0) return s;
|
|
133
|
+
out.push(s.slice(last));
|
|
134
|
+
return out.join("");
|
|
135
|
+
}
|
|
136
|
+
function first_path(a, b, path) {
|
|
137
|
+
if (a === b) return null;
|
|
138
|
+
if (same_ref(a, b)) return null;
|
|
139
|
+
const clip = (v) => {
|
|
140
|
+
const s = JSON.stringify(v) ?? String(v);
|
|
141
|
+
return s.length > 60 ? `${s.slice(0, 60)}…` : s;
|
|
142
|
+
};
|
|
143
|
+
if (a && b && typeof a === "object" && typeof b === "object" && Array.isArray(a) === Array.isArray(b)) {
|
|
144
|
+
if (Array.isArray(a)) {
|
|
145
|
+
const bb = b;
|
|
146
|
+
for (let i = 0; i < Math.max(a.length, bb.length); i++) {
|
|
147
|
+
const d = first_path(a[i], bb[i], `${path}[${i}]`);
|
|
148
|
+
if (d) return d;
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
153
|
+
for (const k of keys) {
|
|
154
|
+
const d = first_path(a[k], b[k], path ? `${path}.${k}` : k);
|
|
155
|
+
if (d) return d;
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
path: tidy_path(path) || "(the props)",
|
|
161
|
+
was: clip(a),
|
|
162
|
+
now: clip(b)
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/** A path as the author wrote the props: ogygia's own ref wrapper (a store, a shared class — the
|
|
166
|
+
* value inside it is `.d`) left out, so `cart.<OgygiaRef>.d.serverStamp` reads `cart.serverStamp`. */
|
|
167
|
+
function tidy_path(path) {
|
|
168
|
+
return path.split(".<OgygiaRef>.d").join("").split(".<OgygiaRef>").join("");
|
|
169
|
+
}
|
|
170
|
+
export { compare, props_diff };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A HOLE'S ANSWER, CUT INTO ITS PARTS — the one cut the devtools Page tab's waterfall and the
|
|
3
|
+
* profiler report's One clock both draw. Where the browser (Resource Timing) and the server
|
|
4
|
+
* (Server-Timing `og-queue`, `og-render`) timed it: before its request left (a busy page, the
|
|
5
|
+
* runtime's queue), the server's wait for a render slot, the server render (or, untimed, the wait
|
|
6
|
+
* for the first byte), then the rest (network, the body, the swap). Page times, ms.
|
|
7
|
+
*/
|
|
8
|
+
export interface HoleTimes {
|
|
9
|
+
/** when its fallback began to count (the first paint, or the hole's own start) */
|
|
10
|
+
shown_at?: number;
|
|
11
|
+
/** how long its fallback stood: from `shown_at` to the swap */
|
|
12
|
+
wait_ms: number;
|
|
13
|
+
left_at?: number;
|
|
14
|
+
first_at?: number;
|
|
15
|
+
server_queue_ms?: number;
|
|
16
|
+
server_ms?: number;
|
|
17
|
+
}
|
|
18
|
+
export type HoleSegKind = "wait" | "before" | "slot" | "render" | "server" | "rest";
|
|
19
|
+
export declare const HOLE_SEG_LABEL: Record<HoleSegKind, string>;
|
|
20
|
+
/** The segments, in order. Without a timing, one: the wait. A request the document preloaded
|
|
21
|
+
* starts before the fallback counts: its segments start where it left. */
|
|
22
|
+
export declare function hole_segments(h: HoleTimes): {
|
|
23
|
+
k: HoleSegKind;
|
|
24
|
+
a: number;
|
|
25
|
+
b: number;
|
|
26
|
+
}[];
|