@abide/harness 0.0.0-stage → 0.2.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.
@@ -0,0 +1,21 @@
1
+ // HOW BIG A THING IS. Two arms, one question, and the type decides which: a `Response`,
2
+ // a `Blob`, a string or a buffer is measured by its PAYLOAD — the document, the built
3
+ // bundle per entry — and anything else by JSC's shallow estimate, which is what
4
+ // "bytes retained under a stalled reader" is. "Allocates no queue" is not directly
5
+ // observable; the retention is.
6
+
7
+ import { estimateShallowMemoryUsageOf } from 'bun:jsc'
8
+
9
+ /** A payload's byte length, or JSC's shallow memory estimate for any other value. */
10
+ export async function bytes(of: unknown): Promise<number> {
11
+ // CLONED, because reading a `Response`'s body consumes it: measuring what a route
12
+ // returns and then serving the same object throws "Body already used" at the
13
+ // caller, one line away from the measurement and looking nothing like it.
14
+ if (of instanceof Response)
15
+ return (await of.clone().arrayBuffer()).byteLength
16
+ if (of instanceof Blob) return of.size
17
+ if (typeof of === 'string') return Buffer.byteLength(of)
18
+ if (ArrayBuffer.isView(of)) return of.byteLength
19
+ if (of instanceof ArrayBuffer) return of.byteLength
20
+ return estimateShallowMemoryUsageOf(of as object)
21
+ }
@@ -0,0 +1,18 @@
1
+ // A CLOSURE IS A PAIR, and this exists so nobody re-derives which pair. JSC reports
2
+ // one as a `Function` and a `JSLexicalEnvironment` together; confirmed under bun
3
+ // 1.4.2, five closures move the pair by exactly +5/+5 after a `fullGC`, and `Scope`
4
+ // never appears as a key at all.
5
+
6
+ import { retained } from './retained.ts'
7
+
8
+ /** The `Function` / `JSLexicalEnvironment` pair a body left reachable, from `retained()`. */
9
+ export function closures(body: () => unknown): {
10
+ Function: number
11
+ JSLexicalEnvironment: number
12
+ } {
13
+ const diff = retained(body)
14
+ return {
15
+ Function: diff.Function ?? 0,
16
+ JSLexicalEnvironment: diff.JSLexicalEnvironment ?? 0,
17
+ }
18
+ }
@@ -0,0 +1,16 @@
1
+ // Lane 3 of 3 — bytes, microtask turns and allocations, on bun.
2
+ // The lane that prices what ships and what the server spends getting it there.
3
+ //
4
+ // `harness/report` is a FOURTH entry and is not a fourth lane: a lane is a DEPENDENCY
5
+ // partition, and the leaf depends on nothing.
6
+ //
7
+ // There is no `calls()` here. `bun:test` already ships one —
8
+ // `spyOn(checker, 'check').mock.calls.length` is the compiler's wave gate exactly,
9
+ // with `mockRestore` for the teardown — and a second spelling would be a rename with
10
+ // a worse restore story.
11
+
12
+ export { allocated } from './allocated.ts'
13
+ export { bytes } from './bytes.ts'
14
+ export { closures } from './closures.ts'
15
+ export { retained } from './retained.ts'
16
+ export { ticks } from './ticks.ts'
@@ -0,0 +1,40 @@
1
+ // Live objects by JSC type, and the change between two readings — what `retained()` and
2
+ // `allocated()` both diff. Not on the lane's entry: each of the two says what its reading means.
3
+
4
+ import { fullGC, heapStats } from 'bun:jsc'
5
+
6
+ /**
7
+ * Live object counts by JSC type, after a full collection.
8
+ *
9
+ * Throws in a process with a DOM, naming the `caller` that read it — `retained`'s under 43.16.
10
+ */
11
+ export function liveCounts(caller: string): Record<string, number> {
12
+ // happy-dom's own implementation objects land in the same untyped `Object` bucket as the
13
+ // framework's, so a total taken in a preloaded process is worthless for any op that touches
14
+ // the DOM. `bunfig.toml` preloads a DOM into every `bun test` process, so the allocation gates
15
+ // get their own preload-free invocation and this converts the silent contamination into a
16
+ // failure.
17
+ if (typeof document !== 'undefined')
18
+ throw new Error(
19
+ `${caller}() ran in a process with a DOM. happy-dom buckets as \`Object\` exactly where the framework does, so the totals would move with the emulator. Run the allocation gates in a preload-free process.`,
20
+ )
21
+ fullGC()
22
+ return heapStats().objectTypeCounts as Record<string, number>
23
+ }
24
+
25
+ /** What moved from `before` to `after`, per type and divided by `per`, with every unmoved type left out. */
26
+ export function countsMoved(
27
+ before: Record<string, number>,
28
+ after: Record<string, number>,
29
+ per: number,
30
+ ): Record<string, number> {
31
+ const moved: Record<string, number> = {}
32
+ for (const type of new Set([
33
+ ...Object.keys(before),
34
+ ...Object.keys(after),
35
+ ])) {
36
+ const change = (after[type] ?? 0) - (before[type] ?? 0)
37
+ if (change !== 0) moved[type] = change / per
38
+ }
39
+ return moved
40
+ }
@@ -0,0 +1,41 @@
1
+ // WHAT AN OP LEFT REACHABLE, by JSC type, and the one thing `heapStats()` can
2
+ // actually tell you.
3
+ //
4
+ // `heapStats()` after a `fullGC()` counts what is still REACHABLE, which is not the
5
+ // same question as what was allocated. Measured: the same 2000-rep `new URL()` batch
6
+ // six times over came back 2000 every run with the results held in an outer array,
7
+ // and 2002 / 2000 / 2000 / 0 with them held in a block-scoped local — run 3 did not
8
+ // lose them to GC nondeterminism, JSC saw the local was never read after the loop and
9
+ // collected it before the `after` read. So whether a gate observes what it gates
10
+ // would depend on the optimizer's liveness analysis of a case body, invisible at the
11
+ // call site and flipping when the body is edited for an unrelated reason. This
12
+ // function answers the question that is stable; `allocated()` answers the other one
13
+ // and is tagged so it cannot be gated on.
14
+
15
+ import { countsMoved, liveCounts } from './liveCounts.ts'
16
+
17
+ // The keep-alive. What the body handed back stays reachable across the `after` read,
18
+ // so the diff is what the op RETAINS rather than what the optimizer let survive.
19
+ const ROOTS: unknown[] = []
20
+
21
+ /**
22
+ * The change in reachable objects by JSC type across one call of `body`, with its result held.
23
+ *
24
+ * Throws in a process with a DOM (43.16).
25
+ *
26
+ * JSC BUCKETS A PLAIN CLASS INSTANCE AS `Object`. A gate written against
27
+ * `objectTypeCounts.Scope` reads 0 forever and is green forever, and so does a gate reading a
28
+ * FILTERED diff — the seven-type filter this started as had no `Object` in it, and `Object` is
29
+ * exactly where `Scope` and `Call` land. So the whole diff comes back and the finding is a note
30
+ * rather than a filter.
31
+ */
32
+ export function retained(body: () => unknown): Record<string, number> {
33
+ const before = liveCounts('retained')
34
+ // ROOTED, not held in a local. A block-scoped local is what JSC's liveness
35
+ // analysis collected before the `after` read on run 3 of 6; a write to a
36
+ // module-level binding is an observable effect and cannot be reasoned away.
37
+ ROOTS.push(body())
38
+ const after = liveCounts('retained')
39
+ ROOTS.length = 0
40
+ return countsMoved(before, after, 1)
41
+ }
@@ -0,0 +1,34 @@
1
+ // MICROTASK TURNS an op took to settle. A re-queueing probe: it counts the checkpoints
2
+ // the engine passed through, which is what "≤1 promise per call" and "microtask ticks
3
+ // per row" are asserted against. Budgeting emitted code in three numbers — allocations
4
+ // per template node, microtask ticks per row, DOM nodes per list item — this is the
5
+ // second of the three.
6
+ //
7
+ // THE PROBE YIELDS TO THE EVENT LOOP, and it has to. The microtask queue is drained to
8
+ // EXHAUSTION before a timer or an I/O completion runs, so a probe that only ever
9
+ // re-queues a microtask starves any body that settles on a macrotask: measured, a body
10
+ // of `new Promise((resolve) => setTimeout(resolve, 10))` never returned, and the test's
11
+ // own 5s timeout never fired either, because the timeout is a timer too. A hung suite
12
+ // reports nothing at all — no failed test, no summary — which is the worst failure
13
+ // shape there is, so every TURNS_PER_YIELD turns the next probe goes through a
14
+ // macrotask instead. A body that settles in microtasks alone never reaches the bound
15
+ // and counts exactly what it counted before.
16
+ const TURNS_PER_YIELD = 256
17
+
18
+ /** How many microtask turns `body`'s result took to settle. */
19
+ export async function ticks(body: () => unknown): Promise<number> {
20
+ let turns = 0
21
+ let settled = false
22
+ const probe = (): void => {
23
+ if (settled) return
24
+ turns += 1
25
+ if (turns % TURNS_PER_YIELD === 0) setTimeout(probe, 0)
26
+ else queueMicrotask(probe)
27
+ }
28
+ const finished = Promise.resolve(body()).then(() => {
29
+ settled = true
30
+ })
31
+ queueMicrotask(probe)
32
+ await finished
33
+ return turns
34
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.