@abide/harness 0.0.0-stage → 0.2.1

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.
@@ -0,0 +1,51 @@
1
+ const DEFAULT_DEADLINE_MILLISECONDS = 60_000
2
+
3
+ /**
4
+ * Runs `command` to its end and hands back its exit code and its whole output.
5
+ *
6
+ * AWAITED, NEVER SYNCHRONOUS. `Bun.spawnSync` in a `bun test --parallel` worker can lose a child's
7
+ * exit: the child is a zombie nobody reaped, and the wait spins at 100% CPU for good. The loop is
8
+ * synchronous, so neither the test's timeout nor `spawnSync`'s own `timeout` is ever reached, and
9
+ * the gate reports nothing at all. Awaited, the event loop stays free: past `deadline` the child is
10
+ * killed and the wait throws with the command in the message, so a lost exit is one failed test.
11
+ */
12
+ export async function spawned(
13
+ command: string[],
14
+ options: {
15
+ cwd?: string
16
+ env?: Record<string, string | undefined>
17
+ deadline?: number
18
+ } = {},
19
+ ): Promise<{ exitCode: number; stdout: string; stderr: string }> {
20
+ const child = Bun.spawn(command, {
21
+ cwd: options.cwd ?? process.cwd(),
22
+ env: options.env ?? process.env,
23
+ stdout: 'pipe',
24
+ stderr: 'pipe',
25
+ })
26
+ const deadline = options.deadline ?? DEFAULT_DEADLINE_MILLISECONDS
27
+ let timer: ReturnType<typeof setTimeout> | undefined
28
+ const expired = new Promise<never>((_, fail) => {
29
+ timer = setTimeout(() => {
30
+ child.kill()
31
+ fail(
32
+ new Error(
33
+ `\`${command.join(' ')}\` did not finish in ${deadline} ms`,
34
+ ),
35
+ )
36
+ }, deadline)
37
+ })
38
+ try {
39
+ const [exitCode, stdout, stderr] = await Promise.race([
40
+ Promise.all([
41
+ child.exited,
42
+ new Response(child.stdout).text(),
43
+ new Response(child.stderr).text(),
44
+ ]),
45
+ expired,
46
+ ])
47
+ return { exitCode, stdout, stderr }
48
+ } finally {
49
+ clearTimeout(timer)
50
+ }
51
+ }
@@ -0,0 +1,21 @@
1
+ // THE WIRE BETWEEN A SUBJECT AND THIS LANE, and it is a wire rather than an import on
2
+ // purpose: `harness/measure` may have nothing under test in its graph at all (43.1), so
3
+ // a subject that counts what a DOM patch cannot see — an effect rerun, a propagation
4
+ // walk — puts a record on the global and the lane reads it. The two halves cannot share
5
+ // a declaration, so what keeps them honest is a check at the read (`publishedBy`) rather
6
+ // than the type system.
7
+ //
8
+ // A leaf — no imports of its own — because the name crosses a seam and both sides read
9
+ // it (CLAUDE.md, "seams and imports"). It is also injected into the browser page as
10
+ // part of the measure bundle, so anything it reached for would be paid for there.
11
+
12
+ /**
13
+ * The global a subject publishes its counters under: one record per source, by the source's name.
14
+ *
15
+ * `globalThis.__HARNESS_WORK__ ??= {}` and then `.mylib = { renders: 0, ... }`, with every field a
16
+ * number the subject increments where the work happens.
17
+ */
18
+ export const PUBLISHED_WORK_KEY = '__HARNESS_WORK__'
19
+
20
+ /** What a subject publishes: one fixed-shape record of numbers per source. */
21
+ export type PublishedWork = Record<string, Record<string, number>>
@@ -0,0 +1,71 @@
1
+ import type { PublishedWork } from './PUBLISHED_WORK.ts'
2
+
3
+ /**
4
+ * What an op did to the DOM, one count per row, and what the subject published it did besides.
5
+ *
6
+ * Partitioned at the point of counting, and there is deliberately no accessor that adds the first
7
+ * three rows up. Moving a COMMENT is 0.11 µs and moving an ELEMENT with its subtree is 2.8 µs —
8
+ * 25x, because only the element is in the reflow — and on a 500-row reorder the reflow is 52% of
9
+ * the op and identical however many markers moved with it. A single `nodesMoved` total is exactly
10
+ * the number that made a marker-elision change worth 998 fewer records look worth writing; it
11
+ * measured +0.053 ms on one run and −0.033 on the next, under the noise floor, and was dropped.
12
+ *
13
+ * There is no DURATION field. The counter runs inside the path it counts, so a number that is
14
+ * both a count and a duration is a number taken with the instrument in the arm — `time()` throws
15
+ * while the counters are armed, and the two are mutually exclusive passes over the same case body.
16
+ */
17
+ export type Work = {
18
+ /** Elements moved, with their subtrees. THE REFLOW: the renderer's gate is on this row. */
19
+ elementsMoved: number
20
+ /** Comment markers moved. Free, reported, and never summed with the row above. */
21
+ markersMoved: number
22
+ textMoved: number
23
+ /**
24
+ * `create*`, `cloneNode` and `importNode`.
25
+ *
26
+ * The compiled arm makes every row by clone, so a patch set without `cloneNode` reports 0 for
27
+ * the whole arm while the vanilla arm reports 3 per row — the ratio upside down, not a small
28
+ * miscount.
29
+ */
30
+ nodesCreated: number
31
+ /** setAttribute / removeAttribute / toggleAttribute / the `className` setter. */
32
+ attributesSet: number
33
+ /**
34
+ * `classList` mutations, apart from `attributesSet` and from each other.
35
+ *
36
+ * A class write is the cheapest way to move a rendered page and the easiest to do
37
+ * redundantly, and it is the one write whose cost depends on whether a rule matches it —
38
+ * byte-identical page source ran 4.5x apart on exactly this.
39
+ */
40
+ classWrites: number
41
+ /**
42
+ * `style.setProperty`, `removeProperty` and the `cssText` setter.
43
+ *
44
+ * An inline style write skips the cascade and is a different cost from a class, so it is a
45
+ * different row.
46
+ */
47
+ styleWrites: number
48
+ dataWrites: number
49
+ /**
50
+ * A `.data` write whose value the node already had.
51
+ *
52
+ * The renderer orders this row, and it cannot be read off the one above.
53
+ */
54
+ redundantDataWrites: number
55
+ /**
56
+ * `addEventListener` calls.
57
+ *
58
+ * NOT "bindings per row" — a keyed row calls it in neither arm, so the two would agree at
59
+ * zero with the mechanism in AND out.
60
+ */
61
+ listenersBound: number
62
+ /**
63
+ * What each published source counted during the op, by source and then by field — the work a
64
+ * prototype patch cannot see (`PUBLISHED_WORK.ts`).
65
+ *
66
+ * A source that published nothing has NO KEY here rather than a record of zeroes: absent is
67
+ * "nothing was wired", never "the op did none of it" (43.19). Read a source through
68
+ * `publishedBy`, which throws naming a field the record lacks (43.20).
69
+ */
70
+ published: PublishedWork
71
+ }
@@ -0,0 +1,40 @@
1
+ // Lane 1 of 3 — DOM CALL COUNTS, taken from inside the page.
2
+ // The only lane that runs in BOTH substrates (bun's DOM and a real browser), which
3
+ // is what lets a hand-written arm and the arm under test share one clock and one
4
+ // batch size. This lane has nothing under test in its import graph, and that is
5
+ // the whole reason the harness is a package of its own.
6
+ //
7
+ // `harness/report` is a FOURTH entry and is not a fourth lane: a lane is a DEPENDENCY
8
+ // partition, and the leaf depends on nothing.
9
+
10
+ import { type Batched, type BatchSpec, batch } from '../report/batch.ts'
11
+ import { emulatedDom, underBun } from '../report/environment.ts'
12
+ import { install, isArmed } from './install.ts'
13
+
14
+ export { patchSet } from './install.ts'
15
+ export { measure, publishedBy } from './measure.ts'
16
+ export { PUBLISHED_WORK_KEY, type PublishedWork } from './PUBLISHED_WORK.ts'
17
+ export type { Work } from './Work.ts'
18
+
19
+ /**
20
+ * `batch()` with the lane's `emulated` flag filled in. Throws while the counters are armed.
21
+ *
22
+ * THE INTERLOCK, and it replaces a measurement rather than deferring one. The counter's own
23
+ * overhead was going to be the first number taken here; it is irrelevant to a count and fatal to
24
+ * a duration, and taking it under happy-dom guarantees the wrong answer — measured, interleaved,
25
+ * min-of-7, the PATCHED arm came out FASTER at 0.89x, because `appendChild` there is ~130 ns of
26
+ * JavaScript and a JS wrapper is a rounding error on it, where in Chromium the same member is
27
+ * native and a patch both adds a frame and deoptimizes the call site. So there is no threshold to
28
+ * hold the lane to: there is no duration field, and this throws while the counters are armed.
29
+ */
30
+ export function time(spec: Omit<BatchSpec, 'emulated'>): Batched {
31
+ if (isArmed())
32
+ throw new Error(
33
+ 'time() was called while the counters are armed. Counting and timing are mutually exclusive passes over the same case body — the counter runs inside the path it counts.',
34
+ )
35
+ return batch({ ...spec, emulated: emulatedDom() })
36
+ }
37
+
38
+ // Installed at import in a browser. Under bun the preload does it, next to
39
+ // `GlobalRegistrator.register`.
40
+ if (typeof document !== 'undefined' && !underBun()) install()
@@ -0,0 +1,21 @@
1
+ // THE BROWSER ARM'S ENTRY. Built to one file so `bun test` imports the source and a
2
+ // browser is injected with the same bytes — same code, same clock, same batch sizing
3
+ // in both substrates, which is the only reason this lane is worth having in two
4
+ // places at all.
5
+ //
6
+ // It publishes on the global rather than exporting: it runs as a classic script at
7
+ // document-start, before any page code, which is the only moment the patches can be
8
+ // installed.
9
+
10
+ import { install, patchSet } from './install.ts'
11
+ import { measure } from './measure.ts'
12
+
13
+ install()
14
+ ;(
15
+ globalThis as {
16
+ __HARNESS_MEASURE__?: {
17
+ measure: typeof measure
18
+ patchSet: typeof patchSet
19
+ }
20
+ }
21
+ ).__HARNESS_MEASURE__ = { measure, patchSet }