@listen-fire/engine 0.15.3 → 0.16.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/dist/bench/wasm_boundary/child.d.ts +1 -0
- package/dist/bench/wasm_boundary/child.js +126 -0
- package/dist/bench/wasm_boundary/instrument.d.ts +43 -0
- package/dist/bench/wasm_boundary/instrument.js +326 -0
- package/dist/bench/wasm_boundary/main.d.ts +1 -0
- package/dist/bench/wasm_boundary/main.js +276 -0
- package/dist/bench/wasm_boundary/runs.d.ts +9 -0
- package/dist/bench/wasm_boundary/runs.js +198 -0
- package/dist/public/host.d.ts +1 -1
- package/dist/public/host.js +1 -2
- package/dist/public/log.d.ts +1 -1
- package/dist/public/log.js +2 -2
- package/dist/public/movement-engine.d.ts +2 -1
- package/dist/public/movement-engine.js +2 -1
- package/dist/services/embed/in_process_host.d.ts +0 -7
- package/dist/services/embed/in_process_host.js +1 -22
- package/dist/services/logger.d.ts +2 -1
- package/dist/services/logger.js +7 -5
- package/dist/services/movement_engine/host.d.ts +12 -24
- package/dist/services/movement_engine/host_adapter.js +0 -2
- package/dist/services/movement_engine/host_kit.d.ts +1 -12
- package/dist/services/movement_engine/host_kit.js +0 -11
- package/dist/services/movement_engine/program_check.d.ts +13 -3
- package/dist/services/movement_engine/program_check.js +20 -5
- package/dist/services/movement_engine/rust_engine.d.ts +2 -1
- package/dist/services/movement_engine/rust_engine.js +3 -2
- package/dist/services/movement_engine/rust_host.js +16 -23
- package/dist/services/movement_engine/segment_context.d.ts +2 -5
- package/dist/services/movement_engine/segment_context.js +0 -1
- package/dist/services/movement_engine/test_host.d.ts +1 -7
- package/dist/services/movement_engine/test_host.js +1 -2
- package/dist/services/plugins/registry.js +2 -4
- package/dist/services/translation_graph/adapters/remote/index.d.ts +3 -9
- package/dist/services/translation_graph/adapters/remote/index.js +3 -11
- package/dist/services/translation_graph/engine/adapter_table.js +0 -2
- package/dist/services/translation_graph/movement/catalog_snapshot.d.ts +1 -1
- package/dist/services/translation_graph/movement/catalog_snapshot.js +1 -0
- package/dist/services/translation_graph/movement/schema_projection.d.ts +1 -1
- package/dist/services/translation_graph/movement/schema_projection.js +6 -1
- package/dist/services/translation_graph/triggers/listen_plans.d.ts +39 -13
- package/dist/services/translation_graph/triggers/listen_plans.js +52 -20
- package/package.json +3 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// One pass of one run, in a process of its own (main.ts spawns it):
|
|
3
|
+
// WASM_BOUNDARY_MODE picks the pass, argv the run. Warms the run up, then
|
|
4
|
+
// runs it `iterations` times, garbage-collecting between runs (never during
|
|
5
|
+
// one), and prints the per-run medians as one JSON line. The capture pass runs
|
|
6
|
+
// once after the warm-up and writes every payload to `--out`.
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
// First: the loader instantiates the parser as `movement-lang` loads.
|
|
12
|
+
const instrument_1 = require("./instrument");
|
|
13
|
+
const node_fs_1 = require("node:fs");
|
|
14
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
15
|
+
const log_1 = require("../../public/log");
|
|
16
|
+
const movement_engine_1 = require("../../public/movement-engine");
|
|
17
|
+
const runs_1 = require("./runs");
|
|
18
|
+
/** Present under --expose-gc (main.ts passes it). */
|
|
19
|
+
const gc = Reflect.get(globalThis, 'gc');
|
|
20
|
+
function readArgs(argv) {
|
|
21
|
+
const value = (flag) => {
|
|
22
|
+
const at = argv.indexOf(flag);
|
|
23
|
+
return at === -1 ? undefined : argv[at + 1];
|
|
24
|
+
};
|
|
25
|
+
const run = value('--run');
|
|
26
|
+
if (run === undefined)
|
|
27
|
+
throw new Error('child: --run <name>');
|
|
28
|
+
return {
|
|
29
|
+
run,
|
|
30
|
+
warmup: Number(value('--warmup') ?? 10),
|
|
31
|
+
iterations: Number(value('--iterations') ?? 20),
|
|
32
|
+
...(value('--out') !== undefined ? { out: value('--out') } : {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function median(values) {
|
|
36
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
37
|
+
const mid = sorted.length >> 1;
|
|
38
|
+
return sorted.length % 2 === 1 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
|
|
39
|
+
}
|
|
40
|
+
/** Each key's median over the samples. */
|
|
41
|
+
function medians(samples) {
|
|
42
|
+
const keys = new Set(samples.flatMap((s) => Object.keys(s)));
|
|
43
|
+
return Object.fromEntries([...keys].map((k) => [k, median(samples.map((s) => s[k] ?? 0))]));
|
|
44
|
+
}
|
|
45
|
+
/** What one run's pass measured, flat: `wall`, and the pass's own keys. */
|
|
46
|
+
function sample(wallMs) {
|
|
47
|
+
const out = { wall: wallMs };
|
|
48
|
+
if (instrument_1.MODE === 'wasm') {
|
|
49
|
+
for (const [name, ms] of Object.entries(instrument_1.wasmTotals.selfMs))
|
|
50
|
+
out[`wasm.${name}`] = ms;
|
|
51
|
+
for (const [name, n] of Object.entries(instrument_1.wasmTotals.calls))
|
|
52
|
+
out[`calls.${name}`] = n;
|
|
53
|
+
const self = Object.entries(instrument_1.wasmTotals.selfMs);
|
|
54
|
+
const sum = (keep) => self.filter(([n]) => keep(n)).reduce((t, [, ms]) => t + ms, 0);
|
|
55
|
+
out['wasm.total'] = sum(() => true);
|
|
56
|
+
out['wasm.engine'] = sum((n) => n.startsWith('engine_'));
|
|
57
|
+
out['wasm.buffers'] = sum((n) => n === 'alloc' || n === 'dealloc' || n === 'last_panic');
|
|
58
|
+
out['wasm.checker'] = out['wasm.total'] - out['wasm.engine'] - out['wasm.buffers'];
|
|
59
|
+
out['env.ms'] = instrument_1.wasmTotals.envMs;
|
|
60
|
+
out['env.calls'] = instrument_1.wasmTotals.envCalls;
|
|
61
|
+
out.frames = instrument_1.wasmTotals.frames;
|
|
62
|
+
}
|
|
63
|
+
if (instrument_1.MODE === 'json') {
|
|
64
|
+
for (const [bucket, ops] of Object.entries(instrument_1.jsonTotals.ms)) {
|
|
65
|
+
for (const [op, ms] of Object.entries(ops))
|
|
66
|
+
out[`json.${bucket}.${op}`] = ms;
|
|
67
|
+
}
|
|
68
|
+
for (const [bucket, ops] of Object.entries(instrument_1.jsonTotals.ops)) {
|
|
69
|
+
for (const [op, n] of Object.entries(ops))
|
|
70
|
+
out[`ops.${bucket}.${op}`] = n;
|
|
71
|
+
}
|
|
72
|
+
let total = 0;
|
|
73
|
+
for (const [bucket, ops] of Object.entries(instrument_1.jsonTotals.ms)) {
|
|
74
|
+
const inBucket = Object.values(ops).reduce((t, ms) => t + ms, 0);
|
|
75
|
+
out[`json.${bucket}`] = inBucket;
|
|
76
|
+
total += inBucket;
|
|
77
|
+
}
|
|
78
|
+
out['json.total'] = total;
|
|
79
|
+
out.unmatched = instrument_1.jsonTotals.unmatched;
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
async function main() {
|
|
84
|
+
(0, log_1.routeEngineLogsTo)(({ level, message }) => {
|
|
85
|
+
if (level === 'error' || level === 'warn')
|
|
86
|
+
process.stderr.write(`${level}: ${message}\n`);
|
|
87
|
+
});
|
|
88
|
+
const args = readArgs(process.argv.slice(2));
|
|
89
|
+
const run = runs_1.RUNS.find((r) => r.name === args.run);
|
|
90
|
+
if (run === undefined)
|
|
91
|
+
throw new Error(`child: no run ${args.run} (${runs_1.RUNS.map((r) => r.name).join(', ')})`);
|
|
92
|
+
const once = async () => {
|
|
93
|
+
const input = run.input();
|
|
94
|
+
(0, instrument_1.resetTotals)();
|
|
95
|
+
instrument_1.captured.length = 0;
|
|
96
|
+
const start = performance.now();
|
|
97
|
+
const result = await (0, movement_engine_1.runMovement)(input);
|
|
98
|
+
const wall = performance.now() - start;
|
|
99
|
+
if (result.writes.length !== run.writes) {
|
|
100
|
+
throw new Error(`run ${run.name} made ${result.writes.length} writes, not ${run.writes}`);
|
|
101
|
+
}
|
|
102
|
+
return wall;
|
|
103
|
+
};
|
|
104
|
+
for (let i = 0; i < args.warmup; i += 1)
|
|
105
|
+
await once();
|
|
106
|
+
if (instrument_1.MODE === 'capture') {
|
|
107
|
+
if (args.out === undefined)
|
|
108
|
+
throw new Error('child: the capture pass needs --out <dir>');
|
|
109
|
+
await once();
|
|
110
|
+
(0, node_fs_1.mkdirSync)(args.out, { recursive: true });
|
|
111
|
+
(0, node_fs_1.writeFileSync)(node_path_1.default.join(args.out, `${run.name}.json`), JSON.stringify({ run: run.name, calls: instrument_1.captured }));
|
|
112
|
+
process.stdout.write(`${JSON.stringify({ run: run.name, mode: instrument_1.MODE, calls: instrument_1.captured.length })}\n`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const samples = [];
|
|
116
|
+
for (let i = 0; i < args.iterations; i += 1) {
|
|
117
|
+
if (typeof gc === 'function')
|
|
118
|
+
gc();
|
|
119
|
+
samples.push(sample(await once()));
|
|
120
|
+
}
|
|
121
|
+
process.stdout.write(`${JSON.stringify({ run: run.name, mode: instrument_1.MODE, iterations: args.iterations, timerMs: (0, instrument_1.timerCostMs)(), medians: medians(samples) })}\n`);
|
|
122
|
+
}
|
|
123
|
+
main().catch((error) => {
|
|
124
|
+
process.stderr.write(`${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`);
|
|
125
|
+
process.exit(1);
|
|
126
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type Mode = 'clean' | 'wasm' | 'json' | 'capture';
|
|
2
|
+
export declare const MODE: Mode;
|
|
3
|
+
/** Where a call's time and payloads are counted. */
|
|
4
|
+
export type Bucket = 'engine' | 'env' | 'checker';
|
|
5
|
+
export interface WasmTotals {
|
|
6
|
+
/** Self time inside wasm, ms, by export (buffer management included). */
|
|
7
|
+
selfMs: Record<string, number>;
|
|
8
|
+
calls: Record<string, number>;
|
|
9
|
+
/** The environment calls' self time in JavaScript, ms. */
|
|
10
|
+
envMs: number;
|
|
11
|
+
envCalls: number;
|
|
12
|
+
/** Timed frames: each costs about one timer read of overhead. */
|
|
13
|
+
frames: number;
|
|
14
|
+
}
|
|
15
|
+
export interface JsonTotals {
|
|
16
|
+
/** ms, by bucket and operation. */
|
|
17
|
+
ms: Record<Bucket, Record<JsonOp, number>>;
|
|
18
|
+
ops: Record<Bucket, Record<JsonOp, number>>;
|
|
19
|
+
/** Boundary inputs and outputs the attribution could not match (should be 0). */
|
|
20
|
+
unmatched: number;
|
|
21
|
+
}
|
|
22
|
+
export type JsonOp = 'stringify' | 'parse' | 'encode' | 'decode';
|
|
23
|
+
export declare const wasmTotals: WasmTotals;
|
|
24
|
+
export declare const jsonTotals: JsonTotals;
|
|
25
|
+
export declare function resetTotals(): void;
|
|
26
|
+
export interface CapturedEnvCall {
|
|
27
|
+
request: string;
|
|
28
|
+
reply: string;
|
|
29
|
+
status: number;
|
|
30
|
+
}
|
|
31
|
+
export interface CapturedCall {
|
|
32
|
+
export: string;
|
|
33
|
+
/** The scalar arguments besides the buffers (engine_answer's id). */
|
|
34
|
+
scalars: number[];
|
|
35
|
+
inputs: string[];
|
|
36
|
+
/** Absent for an export that writes no output (engine_finish). */
|
|
37
|
+
output?: string;
|
|
38
|
+
status?: number;
|
|
39
|
+
env: CapturedEnvCall[];
|
|
40
|
+
}
|
|
41
|
+
export declare const captured: CapturedCall[];
|
|
42
|
+
/** One timer read's cost, ms: the floor under every timed frame or op. */
|
|
43
|
+
export declare function timerCostMs(): number;
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// What the boundary benchmark observes, from outside the product code: it
|
|
3
|
+
// replaces `WebAssembly.Instance` (so every instance the loader makes, the
|
|
4
|
+
// parser's and each engine segment's, hands out wrapped exports and gets a
|
|
5
|
+
// wrapped `movement_env.call`), and in the JSON pass `JSON.parse`,
|
|
6
|
+
// `JSON.stringify` and UTF-8 encode/decode. It must be imported before
|
|
7
|
+
// `movement-lang`: the loader instantiates the parser as it loads.
|
|
8
|
+
//
|
|
9
|
+
// One pass per process, chosen by WASM_BOUNDARY_MODE, so no pass's
|
|
10
|
+
// observation weighs on another's numbers:
|
|
11
|
+
//
|
|
12
|
+
// - clean: nothing replaced; the wall time alone.
|
|
13
|
+
// - wasm: each export call and each environment call timed, self time
|
|
14
|
+
// (an export's time less the environment calls inside it).
|
|
15
|
+
// - json: the JSON and UTF-8 work on boundary payloads timed where it
|
|
16
|
+
// happens (attribution below), nothing else.
|
|
17
|
+
// - capture: every payload copied out, for the Rust replay; nothing timed.
|
|
18
|
+
//
|
|
19
|
+
// Attribution in the JSON pass. An input is encoded, then `alloc`ed, then
|
|
20
|
+
// the export is called: the encode whose byte length matches an input's
|
|
21
|
+
// allocation since the previous export call is the input's, and the
|
|
22
|
+
// stringify whose result is that very text is its JSON. An output is decoded
|
|
23
|
+
// right after its export returns, and the parse of exactly that text is its
|
|
24
|
+
// JSON. Inside an environment call everything is the call's.
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.captured = exports.jsonTotals = exports.wasmTotals = exports.MODE = void 0;
|
|
27
|
+
exports.resetTotals = resetTotals;
|
|
28
|
+
exports.timerCostMs = timerCostMs;
|
|
29
|
+
function readMode(value) {
|
|
30
|
+
switch (value) {
|
|
31
|
+
case undefined:
|
|
32
|
+
case 'clean':
|
|
33
|
+
return 'clean';
|
|
34
|
+
case 'wasm':
|
|
35
|
+
case 'json':
|
|
36
|
+
case 'capture':
|
|
37
|
+
return value;
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`WASM_BOUNDARY_MODE is clean, wasm, json or capture, not ${value}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.MODE = readMode(process.env.WASM_BOUNDARY_MODE);
|
|
43
|
+
const now = () => performance.now();
|
|
44
|
+
const BUFFER_EXPORTS = new Set(['alloc', 'dealloc', 'last_panic']);
|
|
45
|
+
const ENGINE_EXPORTS = new Set(['engine_start', 'engine_answer', 'engine_finish']);
|
|
46
|
+
const bucketOf = (name) => (ENGINE_EXPORTS.has(name) ? 'engine' : 'checker');
|
|
47
|
+
const zeroOps = () => ({ stringify: 0, parse: 0, encode: 0, decode: 0 });
|
|
48
|
+
exports.wasmTotals = { selfMs: {}, calls: {}, envMs: 0, envCalls: 0, frames: 0 };
|
|
49
|
+
exports.jsonTotals = {
|
|
50
|
+
ms: { engine: zeroOps(), env: zeroOps(), checker: zeroOps() },
|
|
51
|
+
ops: { engine: zeroOps(), env: zeroOps(), checker: zeroOps() },
|
|
52
|
+
unmatched: 0,
|
|
53
|
+
};
|
|
54
|
+
function resetTotals() {
|
|
55
|
+
exports.wasmTotals.selfMs = {};
|
|
56
|
+
exports.wasmTotals.calls = {};
|
|
57
|
+
exports.wasmTotals.envMs = 0;
|
|
58
|
+
exports.wasmTotals.envCalls = 0;
|
|
59
|
+
exports.wasmTotals.frames = 0;
|
|
60
|
+
for (const bucket of ['engine', 'env', 'checker']) {
|
|
61
|
+
exports.jsonTotals.ms[bucket] = zeroOps();
|
|
62
|
+
exports.jsonTotals.ops[bucket] = zeroOps();
|
|
63
|
+
}
|
|
64
|
+
exports.jsonTotals.unmatched = 0;
|
|
65
|
+
}
|
|
66
|
+
function addJson(bucket, op, ms) {
|
|
67
|
+
exports.jsonTotals.ms[bucket][op] += ms;
|
|
68
|
+
exports.jsonTotals.ops[bucket][op] += 1;
|
|
69
|
+
}
|
|
70
|
+
exports.captured = [];
|
|
71
|
+
const exactDecoder = new TextDecoder('utf-8', { fatal: true });
|
|
72
|
+
const frames = [];
|
|
73
|
+
function enter() {
|
|
74
|
+
frames.push({ start: now(), child: 0 });
|
|
75
|
+
}
|
|
76
|
+
/** The frame's self time. */
|
|
77
|
+
function leave() {
|
|
78
|
+
const frame = frames.pop();
|
|
79
|
+
if (frame === undefined)
|
|
80
|
+
throw new Error('bench: a frame left that never entered');
|
|
81
|
+
const total = now() - frame.start;
|
|
82
|
+
const parent = frames[frames.length - 1];
|
|
83
|
+
if (parent !== undefined)
|
|
84
|
+
parent.child += total;
|
|
85
|
+
exports.wasmTotals.frames += 1;
|
|
86
|
+
return total - frame.child;
|
|
87
|
+
}
|
|
88
|
+
const PENDING_LIMIT = 32;
|
|
89
|
+
let pendingStringify = [];
|
|
90
|
+
let pendingEncode = [];
|
|
91
|
+
let allocsSinceCall = [];
|
|
92
|
+
let inEnv = false;
|
|
93
|
+
let expectDecode;
|
|
94
|
+
let expectParse;
|
|
95
|
+
function remember(list, entry) {
|
|
96
|
+
list.push(entry);
|
|
97
|
+
if (list.length > PENDING_LIMIT)
|
|
98
|
+
list.shift();
|
|
99
|
+
}
|
|
100
|
+
/** At an export call: which pending encodes (and their stringifies) were
|
|
101
|
+
* its inputs. */
|
|
102
|
+
function attributeInputs(bucket, inputs) {
|
|
103
|
+
for (const input of inputs) {
|
|
104
|
+
const at = findLast(pendingEncode, (e) => e.value.bytes === input.len);
|
|
105
|
+
if (at === -1) {
|
|
106
|
+
exports.jsonTotals.unmatched += 1;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const [encode] = pendingEncode.splice(at, 1);
|
|
110
|
+
addJson(bucket, 'encode', encode.ms);
|
|
111
|
+
const json = findLast(pendingStringify, (s) => s.value === encode.value.text);
|
|
112
|
+
if (json !== -1) {
|
|
113
|
+
const [stringify] = pendingStringify.splice(json, 1);
|
|
114
|
+
addJson(bucket, 'stringify', stringify.ms);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
pendingEncode = [];
|
|
118
|
+
pendingStringify = [];
|
|
119
|
+
}
|
|
120
|
+
function findLast(list, test) {
|
|
121
|
+
for (let i = list.length - 1; i >= 0; i -= 1)
|
|
122
|
+
if (test(list[i]))
|
|
123
|
+
return i;
|
|
124
|
+
return -1;
|
|
125
|
+
}
|
|
126
|
+
function installJsonHooks() {
|
|
127
|
+
const realStringify = JSON.stringify;
|
|
128
|
+
const realParse = JSON.parse;
|
|
129
|
+
const realEncode = TextEncoder.prototype.encode;
|
|
130
|
+
const realDecode = TextDecoder.prototype.decode;
|
|
131
|
+
Reflect.set(JSON, 'stringify', (...args) => {
|
|
132
|
+
const start = now();
|
|
133
|
+
const result = Reflect.apply(realStringify, JSON, args);
|
|
134
|
+
const ms = now() - start;
|
|
135
|
+
if (inEnv)
|
|
136
|
+
addJson('env', 'stringify', ms);
|
|
137
|
+
else if (typeof result === 'string')
|
|
138
|
+
remember(pendingStringify, { value: result, ms });
|
|
139
|
+
return result;
|
|
140
|
+
});
|
|
141
|
+
Reflect.set(JSON, 'parse', (...args) => {
|
|
142
|
+
const text = args[0];
|
|
143
|
+
const expected = expectParse;
|
|
144
|
+
if (!inEnv && (expected === undefined || text !== expected.text))
|
|
145
|
+
return Reflect.apply(realParse, JSON, args);
|
|
146
|
+
const start = now();
|
|
147
|
+
const result = Reflect.apply(realParse, JSON, args);
|
|
148
|
+
const ms = now() - start;
|
|
149
|
+
if (inEnv)
|
|
150
|
+
addJson('env', 'parse', ms);
|
|
151
|
+
else if (expected !== undefined) {
|
|
152
|
+
addJson(expected.bucket, 'parse', ms);
|
|
153
|
+
expectParse = undefined;
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
});
|
|
157
|
+
Reflect.set(TextEncoder.prototype, 'encode', function (...args) {
|
|
158
|
+
const start = now();
|
|
159
|
+
const result = Reflect.apply(realEncode, this, args);
|
|
160
|
+
const ms = now() - start;
|
|
161
|
+
const text = args[0];
|
|
162
|
+
if (inEnv)
|
|
163
|
+
addJson('env', 'encode', ms);
|
|
164
|
+
else if (typeof text === 'string' && result instanceof Uint8Array) {
|
|
165
|
+
remember(pendingEncode, { value: { text, bytes: result.length }, ms });
|
|
166
|
+
}
|
|
167
|
+
return result;
|
|
168
|
+
});
|
|
169
|
+
Reflect.set(TextDecoder.prototype, 'decode', function (...args) {
|
|
170
|
+
const bucket = inEnv ? 'env' : expectDecode;
|
|
171
|
+
if (bucket === undefined)
|
|
172
|
+
return Reflect.apply(realDecode, this, args);
|
|
173
|
+
const start = now();
|
|
174
|
+
const result = Reflect.apply(realDecode, this, args);
|
|
175
|
+
const ms = now() - start;
|
|
176
|
+
addJson(bucket, 'decode', ms);
|
|
177
|
+
if (!inEnv) {
|
|
178
|
+
expectDecode = undefined;
|
|
179
|
+
if (typeof result === 'string')
|
|
180
|
+
expectParse = { bucket, text: result };
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
function memoryOf(exports) {
|
|
186
|
+
const memory = exports.memory;
|
|
187
|
+
const buffer = typeof memory === 'object' && memory !== null ? Reflect.get(memory, 'buffer') : undefined;
|
|
188
|
+
if (!(buffer instanceof ArrayBuffer))
|
|
189
|
+
throw new Error('bench: the instance exports no memory');
|
|
190
|
+
return new Uint8Array(buffer);
|
|
191
|
+
}
|
|
192
|
+
function u32At(exports, at) {
|
|
193
|
+
const bytes = memoryOf(exports);
|
|
194
|
+
return new DataView(bytes.buffer).getUint32(at, true);
|
|
195
|
+
}
|
|
196
|
+
function text(exports, ptr, len) {
|
|
197
|
+
return exactDecoder.decode(memoryOf(exports).slice(ptr, ptr + len));
|
|
198
|
+
}
|
|
199
|
+
function wrapExport(name, fn, exportsOf) {
|
|
200
|
+
if (BUFFER_EXPORTS.has(name)) {
|
|
201
|
+
return (...args) => {
|
|
202
|
+
if (exports.MODE === 'wasm')
|
|
203
|
+
enter();
|
|
204
|
+
const result = fn(...args);
|
|
205
|
+
if (exports.MODE === 'wasm') {
|
|
206
|
+
exports.wasmTotals.selfMs[name] = (exports.wasmTotals.selfMs[name] ?? 0) + leave();
|
|
207
|
+
exports.wasmTotals.calls[name] = (exports.wasmTotals.calls[name] ?? 0) + 1;
|
|
208
|
+
}
|
|
209
|
+
if (name === 'alloc' && !inEnv && typeof result === 'number')
|
|
210
|
+
allocsSinceCall.push({ ptr: result, len: args[0] });
|
|
211
|
+
return result;
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const bucket = bucketOf(name);
|
|
215
|
+
return (...args) => {
|
|
216
|
+
// The inputs are every allocation since the previous call but the last,
|
|
217
|
+
// which is the two out slots (loader.ts `exchange`).
|
|
218
|
+
const allocs = allocsSinceCall;
|
|
219
|
+
allocsSinceCall = [];
|
|
220
|
+
const writesOutput = args.length >= 2;
|
|
221
|
+
const inputs = writesOutput ? allocs.slice(0, -1) : [];
|
|
222
|
+
if (exports.MODE === 'json')
|
|
223
|
+
attributeInputs(bucket, inputs);
|
|
224
|
+
let call;
|
|
225
|
+
if (exports.MODE === 'capture') {
|
|
226
|
+
const exports = exportsOf();
|
|
227
|
+
// engine_answer's id comes before its buffer; every other export's
|
|
228
|
+
// scalars come after its buffers (loader.ts).
|
|
229
|
+
const scalars = name === 'engine_answer' ? args.slice(0, 1) : args.slice(2 * inputs.length, writesOutput ? -2 : undefined);
|
|
230
|
+
call = {
|
|
231
|
+
export: name,
|
|
232
|
+
scalars,
|
|
233
|
+
inputs: inputs.map((i) => text(exports, i.ptr, i.len)),
|
|
234
|
+
env: [],
|
|
235
|
+
};
|
|
236
|
+
exports.captured.push(call);
|
|
237
|
+
}
|
|
238
|
+
if (exports.MODE === 'wasm')
|
|
239
|
+
enter();
|
|
240
|
+
const result = fn(...args);
|
|
241
|
+
if (exports.MODE === 'wasm') {
|
|
242
|
+
exports.wasmTotals.selfMs[name] = (exports.wasmTotals.selfMs[name] ?? 0) + leave();
|
|
243
|
+
exports.wasmTotals.calls[name] = (exports.wasmTotals.calls[name] ?? 0) + 1;
|
|
244
|
+
}
|
|
245
|
+
if (exports.MODE === 'json' && writesOutput)
|
|
246
|
+
expectDecode = bucket;
|
|
247
|
+
if (call !== undefined && writesOutput) {
|
|
248
|
+
const exports = exportsOf();
|
|
249
|
+
const [outPtrPtr, outLenPtr] = args.slice(-2);
|
|
250
|
+
call.output = text(exports, u32At(exports, outPtrPtr), u32At(exports, outLenPtr));
|
|
251
|
+
if (typeof result === 'number')
|
|
252
|
+
call.status = result;
|
|
253
|
+
}
|
|
254
|
+
return result;
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function wrapEnvCall(real, exportsOf) {
|
|
258
|
+
return (requestPtr, requestLen, outPtrPtr, outLenPtr) => {
|
|
259
|
+
const request = exports.MODE === 'capture' ? text(exportsOf(), requestPtr, requestLen) : '';
|
|
260
|
+
if (exports.MODE === 'wasm')
|
|
261
|
+
enter();
|
|
262
|
+
inEnv = true;
|
|
263
|
+
let status;
|
|
264
|
+
try {
|
|
265
|
+
status = real(requestPtr, requestLen, outPtrPtr, outLenPtr);
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
inEnv = false;
|
|
269
|
+
if (exports.MODE === 'wasm') {
|
|
270
|
+
exports.wasmTotals.envMs += leave();
|
|
271
|
+
exports.wasmTotals.envCalls += 1;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (exports.MODE === 'capture') {
|
|
275
|
+
const exports = exportsOf();
|
|
276
|
+
const reply = text(exports, u32At(exports, outPtrPtr), u32At(exports, outLenPtr));
|
|
277
|
+
const into = exports.captured[exports.captured.length - 1];
|
|
278
|
+
if (into === undefined)
|
|
279
|
+
throw new Error('bench: an environment call outside any export call');
|
|
280
|
+
into.env.push({ request, reply, status: typeof status === 'number' ? status : -1 });
|
|
281
|
+
}
|
|
282
|
+
return status;
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function installInstanceHook() {
|
|
286
|
+
const RealInstance = WebAssembly.Instance;
|
|
287
|
+
function BenchInstance(module, imports) {
|
|
288
|
+
let raw;
|
|
289
|
+
const exportsOf = () => {
|
|
290
|
+
if (raw === undefined)
|
|
291
|
+
throw new Error('bench: the instance called out while instantiating');
|
|
292
|
+
return raw;
|
|
293
|
+
};
|
|
294
|
+
const wrappedImports = {};
|
|
295
|
+
for (const [moduleName, fields] of Object.entries(imports)) {
|
|
296
|
+
wrappedImports[moduleName] = { ...fields };
|
|
297
|
+
const call = fields.call;
|
|
298
|
+
if (moduleName === 'movement_env' && typeof call === 'function') {
|
|
299
|
+
wrappedImports[moduleName].call = wrapEnvCall((...args) => Reflect.apply(call, undefined, args), exportsOf);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
raw = new RealInstance(module, wrappedImports).exports;
|
|
303
|
+
const exports = {};
|
|
304
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
305
|
+
exports[name] =
|
|
306
|
+
typeof value === 'function' ? wrapExport(name, (...args) => Reflect.apply(value, undefined, args), exportsOf) : value;
|
|
307
|
+
}
|
|
308
|
+
return { exports };
|
|
309
|
+
}
|
|
310
|
+
Reflect.set(WebAssembly, 'Instance', BenchInstance);
|
|
311
|
+
}
|
|
312
|
+
if (exports.MODE !== 'clean')
|
|
313
|
+
installInstanceHook();
|
|
314
|
+
if (exports.MODE === 'json')
|
|
315
|
+
installJsonHooks();
|
|
316
|
+
/** One timer read's cost, ms: the floor under every timed frame or op. */
|
|
317
|
+
function timerCostMs() {
|
|
318
|
+
const samples = [];
|
|
319
|
+
for (let i = 0; i < 2000; i += 1) {
|
|
320
|
+
const a = now();
|
|
321
|
+
const b = now();
|
|
322
|
+
samples.push(b - a);
|
|
323
|
+
}
|
|
324
|
+
samples.sort((x, y) => x - y);
|
|
325
|
+
return samples[samples.length >> 1];
|
|
326
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|