forked from WebKit/JetStream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-cli.js
More file actions
249 lines (223 loc) · 9.05 KB
/
node-cli.js
File metadata and controls
249 lines (223 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (C) 2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
// Node.js entry point for running JetStream benchmarks.
//
// Usage: node node-cli.js [options] [test-names...]
//
// The regular cli.js entry point is designed for JSC / V8 / SpiderMonkey shells
// and relies on shell builtins (load, readFile, read, runString …). This file
// serves the same purpose for Node.js:
//
// 1. It installs Node.js shims for all required shell builtins.
// 2. It parses CLI arguments (same flags as cli.js).
// 3. It loads the JetStream harness (shell-config.js + params.js +
// JetStreamDriver.js) as a single vm.runInThisContext call so that
// top-level const/let/class declarations across the three files are in
// the same script scope and mutually visible.
// 4. It runs the benchmark suite.
//
// Node 22+ is required (matches the project's engines field in package.json).
"use strict";
const fs = require("fs");
const path = require("path");
const vm = require("vm");
// ── Working directory ────────────────────────────────────────────────────────
// All relative paths in the harness assume the JetStream root as CWD.
const ROOT = __dirname;
process.chdir(ROOT);
// ── Install Node.js shims for shell builtins ─────────────────────────────────
require("./utils/node-shims.js");
// ── CLI argument definitions (mirrors cli.js) ────────────────────────────────
const CLI_PARAMS = {
__proto__: null,
help: {
help: "Print this help message.",
param: "help",
},
"iteration-count": {
help: "Set the default iteration count.",
param: "testIterationCount",
},
"worst-case-count": {
help: "Set the default worst-case count.",
param: "testWorstCaseCount",
},
"dump-json-results": {
help: "Print summary json to the console.",
param: "dumpJSONResults",
},
"dump-test-list": {
help: "Print the selected test list instead of running.",
param: "dumpTestList",
},
ramification: {
help: "Enable ramification support.",
param: "RAMification",
},
"no-prefetch": {
help: "Do not prefetch resources before running.",
param: "prefetchResources",
},
"group-details": {
help: "Display detailed group items.",
param: "groupDetails",
},
test: {
help: "Run a specific test or comma-separated list of tests.",
param: "test",
},
tag: {
help: "Run tests with a specific tag or comma-separated list of tags.",
param: "tag",
},
"start-automatically": {
help: "Start the benchmark automatically (browser mode).",
param: "startAutomatically",
},
report: {
help: "Report results to a server URL.",
param: "report",
},
"start-delay": {
help: "Delay in milliseconds before starting the benchmark.",
param: "startDelay",
},
"custom-pre-iteration-code": {
help: "JavaScript code to run before each iteration.",
param: "customPreIterationCode",
},
"custom-post-iteration-code": {
help: "JavaScript code to run after each iteration.",
param: "customPostIterationCode",
},
"force-gc": {
help: "Force garbage collection before each benchmark. Requires --expose-gc.",
param: "forceGC",
},
};
function help(message) {
if (message) {
console.log(message);
console.log();
}
console.log("Usage: node node-cli.js [options] [test-names...]");
console.log();
console.log("Options:");
for (const [flag, { help: desc }] of Object.entries(CLI_PARAMS))
console.log(` --${flag.padEnd(24)} ${desc}`);
console.log();
console.log(
"test-names can be benchmark names or tag names (see --dump-test-list)."
);
}
// ── Parse process.argv ───────────────────────────────────────────────────────
const cliParams = new Map();
const cliArgs = []; // positional test/tag names
function parseCliFlag(argument) {
const eqIndex = argument.indexOf("=", 2);
const flagName = eqIndex >= 0 ? argument.slice(2, eqIndex) : argument.slice(2);
if (!(flagName in CLI_PARAMS)) {
help(`Unknown flag: '--${flagName}'`);
process.exit(1);
}
let value;
if (flagName.startsWith("no-")) {
// e.g. --no-prefetch → prefetchResources = "false"
value = "false";
} else if (eqIndex >= 0) {
value = argument.slice(eqIndex + 1);
} else {
value = "true";
}
cliParams.set(CLI_PARAMS[flagName].param, value);
}
for (const arg of process.argv.slice(2)) {
if (arg.startsWith("--")) {
parseCliFlag(arg);
} else {
cliArgs.push(arg);
}
}
// Collect positional test names into the "test" param
if (cliArgs.length) {
const existing = cliParams.has("test") ? cliParams.get("test").split(",") : [];
cliParams.set("test", existing.concat(cliArgs).join(","));
}
const printHelp = cliParams.delete("help");
const dumpTestList = cliParams.delete("dumpTestList");
// Expose parsed params to params.js via the same global that cli.js uses
if (cliParams.size) {
globalThis.JetStreamParamsSource = cliParams;
}
// ── Load the JetStream harness ───────────────────────────────────────────────
//
// WHY concatenate instead of separate load() calls?
//
// Node.js treats each vm.runInThisContext() call as a separate script compilation
// unit. V8 keeps const/let/class declarations in "script scope" — they are
// accessible within one script but NOT from a different script, even when both
// run in the same context. The three harness files cross-reference each other's
// top-level bindings (e.g. JetStreamDriver.js reads isInBrowser/isD8/isSpiderMonkey
// from shell-config.js and JetStreamParams from params.js), so they must share
// a single script scope. Concatenating them achieves this without touching the
// original source files.
//
// var declarations and globalThis assignments ARE shared across separate scripts,
// so any load() calls that happen at runtime (e.g. wasm/zlib/shell.js) continue
// to work correctly through the globalThis.load shim.
const harnessFiles = [
path.join(ROOT, "utils", "shell-config.js"),
path.join(ROOT, "utils", "params.js"),
path.join(ROOT, "JetStreamDriver.js"),
];
const harnessCode = harnessFiles
.map((f) => fs.readFileSync(f, "utf8"))
.join("\n;\n");
vm.runInThisContext(harnessCode, { filename: "jetstream-harness.js" });
// JetStreamDriver.js ends with: this.JetStream = new Driver(benchmarks);
// At the top level of a vm.runInThisContext script, `this` is the Node.js
// global object, so JetStream is now available as globalThis.JetStream.
// ── Help / dump-test-list / run ──────────────────────────────────────────────
if (printHelp) {
help();
process.exit(0);
}
if (dumpTestList) {
globalThis.JetStream.dumpTestList();
process.exit(0);
}
async function runJetStream() {
try {
await globalThis.JetStream.initialize();
await globalThis.JetStream.start();
} catch (e) {
let eStr;
try { eStr = String(e); } catch (_) { eStr = Object.prototype.toString.call(e); }
console.error("JetStream3 failed: " + eStr);
console.error(e?.stack ?? "(no stack)");
process.exit(1);
}
}
runJetStream().catch(() => process.exit(1));