@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.
- package/package.json +70 -5
- package/scripts/buildInjectable.ts +27 -0
- package/src/abide/index.ts +53 -0
- package/src/abide/preload.ts +6 -0
- package/src/engine/Trace.ts +54 -0
- package/src/engine/index.ts +18 -0
- package/src/engine/shares.ts +143 -0
- package/src/engine/traceOp.ts +139 -0
- package/src/gate/inAFreshProcess.ts +42 -0
- package/src/gate/index.ts +95 -0
- package/src/gate/spawned.ts +51 -0
- package/src/measure/PUBLISHED_WORK.ts +21 -0
- package/src/measure/Work.ts +71 -0
- package/src/measure/index.ts +40 -0
- package/src/measure/injectable.ts +21 -0
- package/src/measure/install.ts +559 -0
- package/src/measure/measure.ts +102 -0
- package/src/measure/preload.ts +36 -0
- package/src/report/Sample.ts +76 -0
- package/src/report/THRESHOLDS.ts +17 -0
- package/src/report/batch.ts +339 -0
- package/src/report/clock.ts +60 -0
- package/src/report/environment.ts +22 -0
- package/src/report/index.ts +17 -0
- package/src/report/ratio.ts +144 -0
- package/src/report/substrate.ts +26 -0
- package/src/rows/index.ts +91 -0
- package/src/runner/index.ts +9 -0
- package/src/runner/rowsCommand.ts +33 -0
- package/src/runner/runSuite.ts +179 -0
- package/src/runner/testRows.ts +168 -0
- package/src/server/allocated.ts +56 -0
- package/src/server/bytes.ts +21 -0
- package/src/server/closures.ts +18 -0
- package/src/server/index.ts +16 -0
- package/src/server/liveCounts.ts +40 -0
- package/src/server/retained.ts +41 -0
- package/src/server/ticks.ts +34 -0
- package/README.md +0 -3
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// WHAT AN OP ALLOCATED, which `heapStats()` cannot see and this therefore does not
|
|
2
|
+
// pretend to report exactly. A batch mean, tagged, and the tag is the point: an exact
|
|
3
|
+
// integer is `retained()`'s to hand out, and a fractional reading like `Object:3.09`
|
|
4
|
+
// is a reachability artifact rather than a genuinely fractional quantity.
|
|
5
|
+
//
|
|
6
|
+
// What a fractional per-rep count IS good for is the other question: a non-integer
|
|
7
|
+
// means the allocation is conditional or amortized — a rehash, an IC transition, a
|
|
8
|
+
// once-per-batch structure — and a mean over the whole batch hides which. So the
|
|
9
|
+
// batch is halved and the two halves are reported apart. Zero growth across them is
|
|
10
|
+
// the leak check.
|
|
11
|
+
|
|
12
|
+
import { countsMoved, liveCounts } from './liveCounts.ts'
|
|
13
|
+
|
|
14
|
+
const DEFAULT_REPS = 1000
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Mean objects per rep by JSC type over a batch, and how that mean grew between its halves.
|
|
18
|
+
*
|
|
19
|
+
* A batch mean, never exact — gate exact counts on `retained()`. Throws in a process with a DOM.
|
|
20
|
+
*/
|
|
21
|
+
export function allocated(
|
|
22
|
+
body: () => unknown,
|
|
23
|
+
reps = DEFAULT_REPS,
|
|
24
|
+
): {
|
|
25
|
+
/**
|
|
26
|
+
* Always `'batch mean'`.
|
|
27
|
+
*
|
|
28
|
+
* A literal, so this record can never be `toMatchObject`'d against an exact integer the way a
|
|
29
|
+
* `retained()` diff is.
|
|
30
|
+
*/
|
|
31
|
+
tagged: 'batch mean'
|
|
32
|
+
reps: number
|
|
33
|
+
perRep: Record<string, number>
|
|
34
|
+
/**
|
|
35
|
+
* The last half's mean minus the first half's, per rep.
|
|
36
|
+
*
|
|
37
|
+
* A type that grows across the batch is retaining rather than allocating.
|
|
38
|
+
*/
|
|
39
|
+
growth: Record<string, number>
|
|
40
|
+
} {
|
|
41
|
+
const half = Math.max(1, Math.floor(reps / 2))
|
|
42
|
+
const start = liveCounts('allocated')
|
|
43
|
+
for (let rep = 0; rep < half; rep += 1) body()
|
|
44
|
+
const middle = liveCounts('allocated')
|
|
45
|
+
for (let rep = 0; rep < half; rep += 1) body()
|
|
46
|
+
const end = liveCounts('allocated')
|
|
47
|
+
|
|
48
|
+
const first = countsMoved(start, middle, half)
|
|
49
|
+
const second = countsMoved(middle, end, half)
|
|
50
|
+
return {
|
|
51
|
+
tagged: 'batch mean',
|
|
52
|
+
reps: half * 2,
|
|
53
|
+
perRep: countsMoved(start, end, half * 2),
|
|
54
|
+
growth: countsMoved(first, second, 1),
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -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