@ac-bench/measure-resource 0.2.0 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,129 @@
1
1
  # @ac-bench/measure-resource
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5c203e7: Browser entries. Each measure package and `@ac-bench/util` adds a `"browser"`
8
+ condition to its `"."` export (`index.browser.ts`, mirrored in
9
+ `publishConfig`), with the same registration API and no `node:` import, so a
10
+ suite mixing measures bundles for a page. What a page cannot do fails the case
11
+ alone with `UnsupportedInRuntimeError` (`unsupported-in-runtime`, new in
12
+ `@ac-bench/core/runner`): duration's spawned processes (per-iteration
13
+ processes, command cases, spawn baselines), jitter's event-loop monitor,
14
+ constant-time's nanosecond clock. Each plugin's `supportsRuntime` lets the
15
+ parent skip what cannot run without a launch: duration runs everywhere,
16
+ resource in Node and Chromium-family runtimes, jitter and constant-time in
17
+ Node only.
18
+
19
+ Resource measures in Chromium through `performance.memory` (allocation source
20
+ `browser-heap-growth`): its `scheduling.browser` asks for
21
+ `--enable-precise-memory-info` and `--js-flags=--expose-gc
22
+ --min-semi-space-size=64 --max-semi-space-size=64`, batches are capped below
23
+ half the young generation, an allocation self-check at the first case warns
24
+ `allocation-probe-failed` when the counter is off by more than 10 %, and CPU
25
+ time, which a page cannot read, is reported 0 with a `cpu-time-unavailable`
26
+ warning. Batches are sized by wall time where CPU time reads 0.
27
+
28
+ `@ac-bench/util` adds `assertSameResult(contender, actual, expected)`: an
29
+ equivalence check for suites meant for browsers (`Object.is` on numbers,
30
+ element-wise on arrays and typed arrays, `isDeepEqual` otherwise), naming the
31
+ contender and the first differing index. Its package now has an `exports` map:
32
+ deep imports are no longer reachable.
33
+
34
+ The measure packages' Node entries now have a side effect (installing their
35
+ Node host), declared in `sideEffects`.
36
+ - 5c203e7: Config sampling, condition results, a streamed archive.
37
+
38
+ - A config's `sampling` is now read: a record of each measure's default
39
+ sampling options (`{ duration: { minRuns: 20 }, resource: { warmup: 5 } }`),
40
+ validated as such. `runBench`'s new `sampling` option forwards a measure's
41
+ entry to each case as `measureOptions.sampling`, which duration and resource
42
+ read through `runSamplingDefaults` (`@ac-bench/core/runner`) under each
43
+ condition's own options. Migration: the old `sampling` type (condition
44
+ options) never had an effect; key it by measure id.
45
+ - A condition's `onEnd` hook receives a second argument,
46
+ `BenchConditionEndContext`, whose `setConditionResult` emits a
47
+ `"condition-result"` for the condition. Jitter uses it: its condition result
48
+ gains `eventLoopDelay` (`meanMs`, `p50Ms`, `p99Ms`, `maxMs`), the event
49
+ loop's own delay over the condition.
50
+ - The json reporter streams its archive: the header and each condition once
51
+ it settles (`ArchiveBuilder.settled()`), the run description, runtimes,
52
+ gates, verdicts and diagnostics at close, so a crashed run keeps every
53
+ settled condition; `recoverJsonArchive(text)` reads such a file back,
54
+ keeping every condition written whole and dropping one cut mid-way. A
55
+ complete file parses as before; its keys are in another order.
56
+
57
+ ### Patch Changes
58
+
59
+ - 5c203e7: The `homepage` points at the @ac-bench documentation site: `measure(1)` for the CLI, the API reference for `core`, `lib` and `gate`, the site's home for the others.
60
+ - Updated dependencies [5c203e7]
61
+ - Updated dependencies [5c203e7]
62
+ - Updated dependencies [5c203e7]
63
+ - Updated dependencies [5c203e7]
64
+ - Updated dependencies [5c203e7]
65
+ - Updated dependencies [5c203e7]
66
+ - Updated dependencies [5c203e7]
67
+ - Updated dependencies [5c203e7]
68
+ - Updated dependencies [5c203e7]
69
+ - Updated dependencies [5c203e7]
70
+ - Updated dependencies [5c203e7]
71
+ - Updated dependencies [5c203e7]
72
+ - Updated dependencies [5c203e7]
73
+ - Updated dependencies [5c203e7]
74
+ - Updated dependencies [5c203e7]
75
+ - Updated dependencies [5c203e7]
76
+ - Updated dependencies [5c203e7]
77
+ - Updated dependencies [5c203e7]
78
+ - Updated dependencies [5c203e7]
79
+ - Updated dependencies [5c203e7]
80
+ - Updated dependencies [5c203e7]
81
+ - Updated dependencies [5c203e7]
82
+ - Updated dependencies [5c203e7]
83
+ - Updated dependencies [5c203e7]
84
+ - @ac-kit/algo@0.7.0
85
+ - @ac-kit/app-report@0.4.0
86
+ - @ac-bench/core@0.5.0
87
+ - @ac-kit/core@0.14.0
88
+ - @ac-kit/math-stats@0.7.0
89
+ - @ac-kit/model-dataset@0.3.1
90
+
91
+ ## 0.2.1
92
+
93
+ ### Patch Changes
94
+
95
+ - Updated dependencies [0b4fe43]
96
+ - Updated dependencies [0b4fe43]
97
+ - Updated dependencies [0b4fe43]
98
+ - Updated dependencies [0b4fe43]
99
+ - Updated dependencies [0b4fe43]
100
+ - Updated dependencies [0b4fe43]
101
+ - Updated dependencies [0b4fe43]
102
+ - Updated dependencies [0b4fe43]
103
+ - Updated dependencies [0b4fe43]
104
+ - Updated dependencies [0b4fe43]
105
+ - Updated dependencies [0b4fe43]
106
+ - Updated dependencies [0b4fe43]
107
+ - Updated dependencies [0b4fe43]
108
+ - Updated dependencies [0b4fe43]
109
+ - Updated dependencies [0b4fe43]
110
+ - Updated dependencies [0b4fe43]
111
+ - Updated dependencies [0b4fe43]
112
+ - Updated dependencies [0b4fe43]
113
+ - Updated dependencies [0b4fe43]
114
+ - Updated dependencies [0b4fe43]
115
+ - Updated dependencies [0b4fe43]
116
+ - Updated dependencies [0b4fe43]
117
+ - Updated dependencies [0b4fe43]
118
+ - Updated dependencies [0b4fe43]
119
+ - Updated dependencies [0b4fe43]
120
+ - @ac-kit/algo@0.6.0
121
+ - @ac-bench/core@0.4.0
122
+ - @ac-kit/core@0.13.0
123
+ - @ac-kit/math-stats@0.6.0
124
+ - @ac-kit/model-dataset@0.3.0
125
+ - @ac-kit/app-report@0.3.1
126
+
3
127
  ## 0.2.0
4
128
 
5
129
  ### Minor Changes
package/README.md CHANGED
@@ -37,6 +37,20 @@ exact: each result says `offHeap: "lower-bound"` (and warns
37
37
  `offHeap: "unknown"` otherwise — no movement seen does not mean none was
38
38
  allocated.
39
39
 
40
- Host-bound: it reads Node's resource-usage and GC instrumentation.
40
+ Node: it reads the process's resource-usage and GC instrumentation.
41
+
42
+ ## In Chromium
43
+
44
+ The `"browser"` export condition selects a portable entry. In a Chromium page
45
+ (`@ac-bench/browser`), allocation is the heap growth of `performance.memory`
46
+ (`allocationSource: "browser-heap-growth"`) between two forced collections:
47
+ the measure asks Chromium for `--enable-precise-memory-info` and
48
+ `--js-flags=--expose-gc --min-semi-space-size=64 --max-semi-space-size=64`, and
49
+ caps each batch's garbage below half that young generation so no scavenge runs
50
+ inside it. An allocation self-check at the first case compares a known
51
+ structure's measured size with its layout and warns `allocation-probe-failed`
52
+ when it is off by more than 10 %. A page cannot read its CPU time, its
53
+ collections or its RSS: CPU reads 0 with a `cpu-time-unavailable` warning.
54
+ Firefox and WebKit expose no heap counter; their conditions are skipped.
41
55
 
42
56
  Run suites with the `measure` CLI from `@ac-bench/cli`.
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Portable half of the counters: the types every host's counters share and the
3
+ * calibration applied to whichever source a host offers. No `node:` import
4
+ * here; the Node sources live in `_counters.ts`, Chromium's in
5
+ * `_browser-host.ts`.
6
+ */
7
+ /** One reading of the counters every resource figure is derived from. */
8
+ export type ResourceCounters = {
9
+ cpuUserUs: number;
10
+ cpuSystemUs: number;
11
+ heapUsedBytes: number;
12
+ rssBytes: number;
13
+ };
14
+ export type AllocationCounterSource =
15
+ /**
16
+ * `v8.getHeapStatistics().total_allocated_bytes` (Node ≥ 26). Counts what the
17
+ * mutator allocated, collections included, but V8 advances it by whole
18
+ * allocation buffers, so a window is only exact to about a megabyte.
19
+ */
20
+ "v8-total-allocated"
21
+ /**
22
+ * Heap growth plus what each collection inside the window freed, from
23
+ * `v8.GCProfiler` (Node ≥ 18.15). Exact to the byte whatever the garbage.
24
+ */
25
+ | "gc-profiler"
26
+ /**
27
+ * Heap growth alone. Correct only while no collection runs in the window; a
28
+ * fallback, and one that says so through
29
+ * {@link AllocationCounter.undercounted}.
30
+ */
31
+ | "heap-growth"
32
+ /**
33
+ * `performance.memory.usedJSHeapSize` in Chromium; provided by the browser
34
+ * entry.
35
+ */
36
+ | "browser-heap-growth";
37
+ /**
38
+ * What a window's figure says about off-heap memory (typed array backing
39
+ * stores, buffers). No counter here sees a backing store allocated: they see
40
+ * the net movement of the process's external memory and what collections report
41
+ * freeing, and V8 can release one backing store while another is allocated, or
42
+ * after the collection reports. So no window's off-heap share is ever exact,
43
+ * and none is ever known to be zero:
44
+ *
45
+ * - `"lower-bound"`: external memory moved in the window; the figure includes
46
+ * that movement, and the bytes actually allocated off the heap may be more.
47
+ * - `"unknown"`: no movement was seen, which does not show that nothing was
48
+ * allocated off the heap — only that the net change stayed under 4 KiB.
49
+ */
50
+ export type OffHeapAccounting = "lower-bound" | "unknown";
51
+ export type AllocationCounter = {
52
+ readonly source: AllocationCounterSource;
53
+ /** Whether the last window may have lost bytes to a collection. */
54
+ readonly undercounted: boolean;
55
+ /**
56
+ * What the last window's figure says about off-heap memory; never that none
57
+ * was allocated. See {@link OffHeapAccounting}.
58
+ */
59
+ readonly offHeap: OffHeapAccounting;
60
+ /** Starts a window; one still open is discarded. */
61
+ begin(): void;
62
+ /**
63
+ * Bytes allocated since {@link AllocationCounter.begin}, minus the counter's
64
+ * own cost.
65
+ */
66
+ end(): number;
67
+ /** Discards an open window, for a batch that threw before `end`. */
68
+ abort(): void;
69
+ };
70
+ /**
71
+ * One reading source before calibration: reports the raw bytes of a window,
72
+ * counter bookkeeping included.
73
+ */
74
+ export type RawAllocationCounter = {
75
+ readonly source: AllocationCounterSource;
76
+ readonly undercounted: boolean;
77
+ readonly offHeap: OffHeapAccounting;
78
+ begin(): void;
79
+ end(): number;
80
+ abort(): void;
81
+ };
82
+ /**
83
+ * Wraps a raw counter so a window reports what the case allocated: the bytes
84
+ * the counter allocates itself are measured on empty windows and removed.
85
+ */
86
+ export declare function calibrateAllocationCounter(raw: RawAllocationCounter): AllocationCounter;
87
+ export declare function cpuTimeMs(before: ResourceCounters, after: ResourceCounters): number;
88
+ export type GcTotals = {
89
+ count: number;
90
+ pauseMs: number;
91
+ };
92
+ /** Running totals of observed collections; `read` is `null` when unobservable. */
93
+ export type GcReader = Disposable & {
94
+ read(): GcTotals | null;
95
+ };
96
+ export declare function gcSince(before: GcTotals | null, after: GcTotals | null): GcTotals | null;
97
+ //# sourceMappingURL=_allocation-counter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_allocation-counter.d.ts","sourceRoot":"","sources":["../src/_allocation-counter.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH,yEAAyE;AACzE,MAAM,MAAM,gBAAgB,GAAG;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,uBAAuB;AAClC;;;;GAIG;AACD,oBAAoB;AACtB;;;GAGG;GACD,aAAa;AACf;;;;GAIG;GACD,aAAa;AACf;;;GAGG;GACD,qBAAqB,CAAC;AAEzB;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,iBAAiB,GAAG,aAAa,GAAG,SAAS,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG;IAC/B,QAAQ,CAAC,MAAM,EAAE,uBAAuB,CAAC;IAEzC,mEAAmE;IACnE,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAE/B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC;IAEpC,oDAAoD;IACpD,KAAK,IAAI,IAAI,CAAC;IAEd;;;OAGG;IACH,GAAG,IAAI,MAAM,CAAC;IAEd,oEAAoE;IACpE,KAAK,IAAI,IAAI,CAAC;CACd,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG;IAClC,QAAQ,CAAC,MAAM,EAAE,uBAAuB,CAAC;IACzC,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC;IACpC,KAAK,IAAI,IAAI,CAAC;IACd,GAAG,IAAI,MAAM,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACd,CAAC;AAQF;;;GAGG;AACH,wBAAgB,0BAA0B,CACzC,GAAG,EAAE,oBAAoB,GACvB,iBAAiB,CA8BnB;AAED,wBAAgB,SAAS,CACxB,MAAM,EAAE,gBAAgB,EACxB,KAAK,EAAE,gBAAgB,GACrB,MAAM,CAOR;AAED,MAAM,MAAM,QAAQ,GAAG;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,kFAAkF;AAClF,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG;IACnC,IAAI,IAAI,QAAQ,GAAG,IAAI,CAAC;CACxB,CAAC;AAEF,wBAAgB,OAAO,CACtB,MAAM,EAAE,QAAQ,GAAG,IAAI,EACvB,KAAK,EAAE,QAAQ,GAAG,IAAI,GACpB,QAAQ,GAAG,IAAI,CASjB"}
@@ -0,0 +1,53 @@
1
+ import { median } from "@ac-kit/math-stats";
2
+ /** Empty windows run before the calibration is read, so the JIT has settled. */
3
+ const CALIBRATION_WARMUP_WINDOWS_ = 30;
4
+ /** Empty windows whose median is the counter's own cost. */
5
+ const CALIBRATION_WINDOWS_ = 31;
6
+ /**
7
+ * Wraps a raw counter so a window reports what the case allocated: the bytes
8
+ * the counter allocates itself are measured on empty windows and removed.
9
+ */
10
+ export function calibrateAllocationCounter(raw) {
11
+ for (let window = 0; window < CALIBRATION_WARMUP_WINDOWS_; window++) {
12
+ raw.begin();
13
+ raw.end();
14
+ }
15
+ const costs = [];
16
+ for (let window = 0; window < CALIBRATION_WINDOWS_; window++) {
17
+ raw.begin();
18
+ costs.push(raw.end());
19
+ }
20
+ // The median, because a collection that lands in a calibration window adds
21
+ // megabytes to that one reading.
22
+ const costBytes = median(costs);
23
+ return {
24
+ source: raw.source,
25
+ get undercounted() {
26
+ return raw.undercounted;
27
+ },
28
+ get offHeap() {
29
+ return raw.offHeap;
30
+ },
31
+ begin: () => raw.begin(),
32
+ abort: () => raw.abort(),
33
+ // Clamped: a window cannot allocate a negative amount, and a mean over
34
+ // batches would be meaningless with one.
35
+ end: () => Math.max(0, raw.end() - costBytes),
36
+ };
37
+ }
38
+ export function cpuTimeMs(before, after) {
39
+ return ((after.cpuUserUs -
40
+ before.cpuUserUs +
41
+ (after.cpuSystemUs - before.cpuSystemUs)) /
42
+ 1000);
43
+ }
44
+ export function gcSince(before, after) {
45
+ if (before === null || after === null) {
46
+ return null;
47
+ }
48
+ return {
49
+ count: after.count - before.count,
50
+ pauseMs: after.pauseMs - before.pauseMs,
51
+ };
52
+ }
53
+ //# sourceMappingURL=_allocation-counter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_allocation-counter.js","sourceRoot":"","sources":["../src/_allocation-counter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AA8F5C,gFAAgF;AAChF,MAAM,2BAA2B,GAAG,EAAE,CAAC;AAEvC,4DAA4D;AAC5D,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAEhC;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CACzC,GAAyB;IAEzB,KAAK,IAAI,MAAM,GAAG,CAAC,EAAE,MAAM,GAAG,2BAA2B,EAAE,MAAM,EAAE,EAAE,CAAC;QACrE,GAAG,CAAC,KAAK,EAAE,CAAC;QACZ,GAAG,CAAC,GAAG,EAAE,CAAC;IACX,CAAC;IAED,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,IAAI,MAAM,GAAG,CAAC,EAAE,MAAM,GAAG,oBAAoB,EAAE,MAAM,EAAE,EAAE,CAAC;QAC9D,GAAG,CAAC,KAAK,EAAE,CAAC;QACZ,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;IACvB,CAAC;IAED,2EAA2E;IAC3E,iCAAiC;IACjC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAEhC,OAAO;QACN,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,IAAI,YAAY;YACf,OAAO,GAAG,CAAC,YAAY,CAAC;QACzB,CAAC;QACD,IAAI,OAAO;YACV,OAAO,GAAG,CAAC,OAAO,CAAC;QACpB,CAAC;QACD,KAAK,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE;QACxB,KAAK,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE;QACxB,uEAAuE;QACvE,yCAAyC;QACzC,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;KAC7C,CAAC;AACH,CAAC;AAED,MAAM,UAAU,SAAS,CACxB,MAAwB,EACxB,KAAuB;IAEvB,OAAO,CACN,CAAC,KAAK,CAAC,SAAS;QACf,MAAM,CAAC,SAAS;QAChB,CAAC,KAAK,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;QAC1C,IAAI,CACJ,CAAC;AACH,CAAC;AAYD,MAAM,UAAU,OAAO,CACtB,MAAuB,EACvB,KAAsB;IAEtB,IAAI,MAAM,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACvC,OAAO,IAAI,CAAC;IACb,CAAC;IAED,OAAO;QACN,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK;QACjC,OAAO,EAAE,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO;KACvC,CAAC;AACH,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { type AllocationCounter } from "./_allocation-counter.js";
2
+ import type { ResourceHost } from "./_host.js";
3
+ import type { ResourceBenchWarning } from "./_warnings.js";
4
+ /**
5
+ * Allocates a known structure through `counter` and compares, so a Chromium
6
+ * whose `performance.memory` is quantised (no `--enable-precise-memory-info`)
7
+ * or whose young generation was not enlarged does not report a plausible lie.
8
+ *
9
+ * @returns The warning, or `null` when the counter is within tolerance.
10
+ */
11
+ export declare function checkAllocationCounter(counter: AllocationCounter, expectedBytes?: number): ResourceBenchWarning | null;
12
+ /**
13
+ * Chromium's page counters: heap growth of `performance.memory` between two
14
+ * forced collections, with batches capped below a scavenge. CPU time, RSS and
15
+ * collection events are not readable from a page.
16
+ *
17
+ * @returns The host, or `null` outside Chromium (no `performance.memory`).
18
+ */
19
+ export declare function createChromiumResourceHost(): ResourceHost | null;
20
+ //# sourceMappingURL=_browser-host.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_browser-host.d.ts","sourceRoot":"","sources":["../src/_browser-host.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,iBAAiB,EAKtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAmF3D;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACrC,OAAO,EAAE,iBAAiB,EAC1B,aAAa,GAAE,MAA8B,GAC3C,oBAAoB,GAAG,IAAI,CAoB7B;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,IAAI,YAAY,GAAG,IAAI,CA8ChE"}
@@ -0,0 +1,136 @@
1
+ import { calibrateAllocationCounter, } from "./_allocation-counter.js";
2
+ function readMemory_() {
3
+ return performance.memory;
4
+ }
5
+ /**
6
+ * Half the young generation the resource measure asks Chromium for
7
+ * (`--min-semi-space-size=64 --max-semi-space-size=64`, in MiB): a batch that
8
+ * allocates less never fills a semi-space, so no scavenge runs inside it.
9
+ */
10
+ const MAX_WINDOW_BYTES_ = 32 * 1024 * 1024;
11
+ /** Elements of the self-check: 100 000 triples of small integers. */
12
+ const PROBE_TRIPLES_ = 100000;
13
+ /** How far the self-check may miss before the counter is declared broken. */
14
+ const PROBE_TOLERANCE_ = 0.1;
15
+ /**
16
+ * Bytes V8 lays out for the self-check with pointer compression, as Chromium
17
+ * builds it on 64-bit hosts: the outer array's header (16) and backing store (8 +
18
+ * 4 per element), and per triple a `JSArray` header (16) and a `FixedArray` of
19
+ * three small integers (8 + 3 × 4).
20
+ */
21
+ const PROBE_EXPECTED_BYTES_ = 16 + 8 + 4 * PROBE_TRIPLES_ + PROBE_TRIPLES_ * (16 + 8 + 3 * 4);
22
+ function chromiumMemory_() {
23
+ const memory = readMemory_();
24
+ if (typeof memory?.usedJSHeapSize !== "number") {
25
+ return null;
26
+ }
27
+ return {
28
+ get usedJSHeapSize() {
29
+ return readMemory_().usedJSHeapSize;
30
+ },
31
+ get jsHeapSizeLimit() {
32
+ return readMemory_().jsHeapSizeLimit;
33
+ },
34
+ };
35
+ }
36
+ function collect_() {
37
+ globalThis.gc?.();
38
+ }
39
+ function createChromiumCounter_(memory) {
40
+ let heapStart = 0;
41
+ let undercounted = false;
42
+ return {
43
+ source: "browser-heap-growth",
44
+ get undercounted() {
45
+ return undercounted;
46
+ },
47
+ // `usedJSHeapSize` is the JS heap alone: backing stores are not in it.
48
+ offHeap: "unknown",
49
+ abort() { },
50
+ begin() {
51
+ undercounted = false;
52
+ heapStart = memory.usedJSHeapSize;
53
+ },
54
+ end() {
55
+ const growth = memory.usedJSHeapSize - heapStart;
56
+ undercounted = growth < 0;
57
+ return Math.max(0, growth);
58
+ },
59
+ };
60
+ }
61
+ /**
62
+ * Allocates a known structure through `counter` and compares, so a Chromium
63
+ * whose `performance.memory` is quantised (no `--enable-precise-memory-info`)
64
+ * or whose young generation was not enlarged does not report a plausible lie.
65
+ *
66
+ * @returns The warning, or `null` when the counter is within tolerance.
67
+ */
68
+ export function checkAllocationCounter(counter, expectedBytes = PROBE_EXPECTED_BYTES_) {
69
+ collect_();
70
+ counter.begin();
71
+ const outer = new Array(PROBE_TRIPLES_);
72
+ for (let index = 0; index < PROBE_TRIPLES_; index++) {
73
+ outer[index] = [index & 7, 1, 2];
74
+ }
75
+ const measured = counter.end();
76
+ // Read after `end`, so the structure stays alive through the window.
77
+ const kept = outer.length;
78
+ const error = Math.abs(measured - expectedBytes) / expectedBytes;
79
+ if (error <= PROBE_TOLERANCE_) {
80
+ return null;
81
+ }
82
+ return {
83
+ kind: "allocation-probe-failed",
84
+ message: `the allocation self-check measured ${measured} bytes for ${kept} triples where ${expectedBytes} were expected (${(error * 100).toFixed(0)}% off): run Chromium with --enable-precise-memory-info and the resource measure's V8 flags, or discard these allocation figures`,
85
+ };
86
+ }
87
+ /**
88
+ * Chromium's page counters: heap growth of `performance.memory` between two
89
+ * forced collections, with batches capped below a scavenge. CPU time, RSS and
90
+ * collection events are not readable from a page.
91
+ *
92
+ * @returns The host, or `null` outside Chromium (no `performance.memory`).
93
+ */
94
+ export function createChromiumResourceHost() {
95
+ const memory = chromiumMemory_();
96
+ if (memory === null) {
97
+ return null;
98
+ }
99
+ let counter = null;
100
+ let warnings = [];
101
+ const nullGc = {
102
+ read: () => null,
103
+ [Symbol.dispose]: () => { },
104
+ };
105
+ return {
106
+ readResourceCounters: () => ({
107
+ cpuUserUs: 0,
108
+ cpuSystemUs: 0,
109
+ heapUsedBytes: memory.usedJSHeapSize,
110
+ rssBytes: 0,
111
+ }),
112
+ readRssBytes: () => 0,
113
+ readHeapLimitBytes: () => memory.jsHeapSizeLimit,
114
+ selectAllocationCounter: () => {
115
+ if (counter === null) {
116
+ counter = calibrateAllocationCounter(createChromiumCounter_(memory));
117
+ const probe = checkAllocationCounter(counter);
118
+ warnings = [
119
+ {
120
+ kind: "cpu-time-unavailable",
121
+ message: "a page cannot read its CPU time: the CPU column is 0 and no CPU gate can be evaluated in this runtime",
122
+ },
123
+ ...(probe === null ? [] : [probe]),
124
+ ];
125
+ }
126
+ return counter;
127
+ },
128
+ hasExposedCollector: () => typeof globalThis.gc === "function",
129
+ forceCollection: collect_,
130
+ canObserveGc: () => false,
131
+ createGcRecorder: () => nullGc,
132
+ maxWindowBytes: MAX_WINDOW_BYTES_,
133
+ caseWarnings: () => warnings,
134
+ };
135
+ }
136
+ //# sourceMappingURL=_browser-host.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_browser-host.js","sourceRoot":"","sources":["../src/_browser-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,0BAA0B,GAI1B,MAAM,0BAA0B,CAAC;AAclC,SAAS,WAAW;IACnB,OAAQ,WAA4C,CAAC,MAAM,CAAC;AAC7D,CAAC;AAED;;;;GAIG;AACH,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AAE3C,qEAAqE;AACrE,MAAM,cAAc,GAAG,MAAO,CAAC;AAE/B,6EAA6E;AAC7E,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B;;;;;GAKG;AACH,MAAM,qBAAqB,GAC1B,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,cAAc,GAAG,cAAc,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;AAEjE,SAAS,eAAe;IACvB,MAAM,MAAM,GAAG,WAAW,EAAE,CAAC;IAC7B,IAAI,OAAO,MAAM,EAAE,cAAc,KAAK,QAAQ,EAAE,CAAC;QAChD,OAAO,IAAI,CAAC;IACb,CAAC;IAED,OAAO;QACN,IAAI,cAAc;YACjB,OAAO,WAAW,EAAG,CAAC,cAAc,CAAC;QACtC,CAAC;QACD,IAAI,eAAe;YAClB,OAAO,WAAW,EAAG,CAAC,eAAe,CAAC;QACvC,CAAC;KACD,CAAC;AACH,CAAC;AAED,SAAS,QAAQ;IACf,UAAkC,CAAC,EAAE,EAAE,EAAE,CAAC;AAC5C,CAAC;AAED,SAAS,sBAAsB,CAAC,MAAuB;IACtD,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,YAAY,GAAG,KAAK,CAAC;IAEzB,OAAO;QACN,MAAM,EAAE,qBAAqB;Q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B,OAAQ,UAA+B,CAAC,EAAE,KAAK,UAAU;QAC1D,eAAe,EAAE,QAAQ;QACzB,YAAY,EAAE,GAAG,EAAE,CAAC,KAAK;QACzB,gBAAgB,EAAE,GAAG,EAAE,CAAC,MAAM;QAC9B,cAAc,EAAE,iBAAiB;QACjC,YAAY,EAAE,GAAG,EAAE,CAAC,QAAQ;KAC5B,CAAC;AACH,CAAC"}
@@ -1,10 +1,6 @@
1
- /** One reading of the counters every resource figure is derived from. */
2
- export type ResourceCounters = {
3
- cpuUserUs: number;
4
- cpuSystemUs: number;
5
- heapUsedBytes: number;
6
- rssBytes: number;
7
- };
1
+ import { type AllocationCounter, type AllocationCounterSource, type GcReader, type GcTotals, type ResourceCounters } from "./_allocation-counter.js";
2
+ import type { ResourceHost } from "./_host.js";
3
+ export { type AllocationCounter, type AllocationCounterSource, cpuTimeMs, type GcTotals, gcSince, type OffHeapAccounting, type ResourceCounters, } from "./_allocation-counter.js";
8
4
  /**
9
5
  * Read twice per batch, so it allocates as little as the platform allows:
10
6
  * `v8.getHeapStatistics()` is deliberately not called here — it builds the
@@ -14,62 +10,6 @@ export type ResourceCounters = {
14
10
  export declare function readResourceCounters(): ResourceCounters;
15
11
  /** The heap ceiling this process was given, fixed for its lifetime. */
16
12
  export declare function readHeapLimitBytes(): number;
17
- export type AllocationCounterSource =
18
- /**
19
- * `v8.getHeapStatistics().total_allocated_bytes` (Node ≥ 26). Counts what the
20
- * mutator allocated, collections included, but V8 advances it by whole
21
- * allocation buffers, so a window is only exact to about a megabyte.
22
- */
23
- "v8-total-allocated"
24
- /**
25
- * Heap growth plus what each collection inside the window freed, from
26
- * `v8.GCProfiler` (Node ≥ 18.15). Exact to the byte whatever the garbage.
27
- */
28
- | "gc-profiler"
29
- /**
30
- * Heap growth alone. Correct only while no collection runs in the window; a
31
- * fallback, and one that says so through
32
- * {@link AllocationCounter.undercounted}.
33
- */
34
- | "heap-growth"
35
- /**
36
- * `performance.memory.usedJSHeapSize` in Chromium; provided by the browser
37
- * entry.
38
- */
39
- | "browser-heap-growth";
40
- /**
41
- * What a window's figure says about off-heap memory (typed array backing
42
- * stores, buffers). No counter here sees a backing store allocated: they see
43
- * the net movement of the process's external memory and what collections report
44
- * freeing, and V8 can release one backing store while another is allocated, or
45
- * after the collection reports. So no window's off-heap share is ever exact,
46
- * and none is ever known to be zero:
47
- *
48
- * - `"lower-bound"`: external memory moved in the window; the figure includes
49
- * that movement, and the bytes actually allocated off the heap may be more.
50
- * - `"unknown"`: no movement was seen, which does not show that nothing was
51
- * allocated off the heap — only that the net change stayed under 4 KiB.
52
- */
53
- export type OffHeapAccounting = "lower-bound" | "unknown";
54
- export type AllocationCounter = {
55
- readonly source: AllocationCounterSource;
56
- /** Whether the last window may have lost bytes to a collection. */
57
- readonly undercounted: boolean;
58
- /**
59
- * What the last window's figure says about off-heap memory; never that none
60
- * was allocated. See {@link OffHeapAccounting}.
61
- */
62
- readonly offHeap: OffHeapAccounting;
63
- /** Starts a window; one still open is discarded. */
64
- begin(): void;
65
- /**
66
- * Bytes allocated since {@link AllocationCounter.begin}, minus the counter's
67
- * own cost.
68
- */
69
- end(): number;
70
- /** Discards an open window, for a batch that threw before `end`. */
71
- abort(): void;
72
- };
73
13
  /**
74
14
  * The first of `sources` that works in this process, calibrated, or `null`.
75
15
  *
@@ -79,7 +19,6 @@ export type AllocationCounter = {
79
19
  */
80
20
  export declare function selectAllocationCounter(): AllocationCounter;
81
21
  export declare function selectAllocationCounter(sources: readonly AllocationCounterSource[]): AllocationCounter | null;
82
- export declare function cpuTimeMs(before: ResourceCounters, after: ResourceCounters): number;
83
22
  /** Whether the run was started with `--expose-gc`. */
84
23
  export declare function hasExposedCollector(): boolean;
85
24
  /**
@@ -90,10 +29,6 @@ export declare function hasExposedCollector(): boolean;
90
29
  * numbers are still comparable between cases in the same run.
91
30
  */
92
31
  export declare function forceCollection(): void;
93
- export type GcTotals = {
94
- count: number;
95
- pauseMs: number;
96
- };
97
32
  /**
98
33
  * Whether this runtime actually delivers `"gc"` performance entries.
99
34
  *
@@ -111,7 +46,7 @@ export declare function canObserveGc(): boolean;
111
46
  * observer must stay attached across every batch, and callers difference two
112
47
  * readings rather than restarting it.
113
48
  */
114
- export declare class GcRecorder {
49
+ export declare class GcRecorder implements GcReader {
115
50
  private readonly observer;
116
51
  private count_;
117
52
  private pauseMs_;
@@ -120,5 +55,6 @@ export declare class GcRecorder {
120
55
  [Symbol.dispose](): void;
121
56
  private absorb_;
122
57
  }
123
- export declare function gcSince(before: GcTotals | null, after: GcTotals | null): GcTotals | null;
58
+ /** What the Node entry installs: this process's own counters. */
59
+ export declare const NODE_RESOURCE_HOST: ResourceHost;
124
60
  //# sourceMappingURL=_counters.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"_counters.d.ts","sourceRoot":"","sources":["../src/_counters.ts"],"names":[],"mappings":"AAKA,yEAAyE;AACzE,MAAM,MAAM,gBAAgB,GAAG;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI,gBAAgB,CAUvD;AAED,uEAAuE;AACvE,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C;AAED,MAAM,MAAM,uBAAuB;AAClC;;;;GAIG;AACD,oBAAoB;AACtB;;;GAGG;GACD,aAAa;AACf;;;;GAIG;GACD,aAAa;AACf;;;GAGG;GACD,qBAAqB,CAAC;AAEzB;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,iBAAiB,GAAG,aAAa,GAAG,SAAS,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG;IAC/B,QAAQ,CAAC,MAAM,EAAE,uBAAuB,CAAC;IAEzC,mEAAmE;IACnE,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAE/B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,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