Skip to content

Commit 03b386d

Browse files
authored
Add cli flags (#101)
- Add basic command line flags parsing - Add command line flags for common benchmark settings - Add --help
1 parent 3d4ec25 commit 03b386d

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

JetStreamDriver.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,16 @@ globalThis.testWorstCaseCountMap ??= new Map();
3939
globalThis.dumpJSONResults ??= false;
4040
globalThis.testList ??= undefined;
4141
globalThis.startDelay ??= undefined;
42-
43-
let shouldReport = false;
42+
globalThis.shouldReport ??= false;
4443

4544
function getIntParam(urlParams, key) {
4645
if (!urlParams.has(key))
4746
return undefined
4847
const rawValue = urlParams.get(key);
4948
const value = parseInt(rawValue);
5049
if (value <= 0)
51-
throw new Error(`Expected positive value for ${key}, but got ${rawValue}`)
52-
return value
50+
throw new Error(`Expected positive value for ${key}, but got ${rawValue}`);
51+
return value;
5352
}
5453

5554
function getTestListParam(urlParams, key) {
@@ -60,16 +59,20 @@ function getTestListParam(urlParams, key) {
6059

6160
if (typeof(URLSearchParams) !== "undefined") {
6261
const urlParameters = new URLSearchParams(window.location.search);
63-
shouldReport = urlParameters.has('report') && urlParameters.get('report').toLowerCase() == 'true';
64-
globalThis.startDelay = getIntParam(urlParameters, "startDelay");
65-
if (shouldReport && !globalThis.startDelay)
62+
if (urlParameters.has("report"))
63+
globalThis.shouldReport = urlParameters.get("report").toLowerCase() == "true";
64+
if (urlParameters.has("startDelay"))
65+
globalThis.startDelay = getIntParam(urlParameters, "startDelay");
66+
if (globalThis.shouldReport && !globalThis.startDelay)
6667
globalThis.startDelay = 4000;
6768
if (urlParameters.has("tag"))
6869
globalThis.testList = getTestListParam(urlParameters, "tag");
6970
if (urlParameters.has("test"))
7071
globalThis.testList = getTestListParam(urlParameters, "test");
71-
globalThis.testIterationCount = getIntParam(urlParameters, "iterationCount");
72-
globalThis.testWorstCaseCount = getIntParam(urlParameters, "worstCaseCount");
72+
if (urlParameters.has("iterationCount"))
73+
globalThis.testIterationCount = getIntParam(urlParameters, "iterationCount");
74+
if (urlParameters.has("worstCaseCount"))
75+
globalThis.testWorstCaseCount = getIntParam(urlParameters, "worstCaseCount");
7376
}
7477

7578
// Used for the promise representing the current benchmark run.
@@ -549,7 +552,7 @@ class Driver {
549552
if (!isInBrowser)
550553
return;
551554

552-
if (!shouldReport)
555+
if (!globalThis.shouldReport)
553556
return;
554557

555558
const content = this.resultsJSON();

cli.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,41 @@
2424
*/
2525

2626
load("./shell-config.js")
27-
load("./JetStreamDriver.js");
27+
28+
const cliFlags = { __proto__: null };
29+
const cliArgs = [];
30+
if (globalThis.arguments?.length) {
31+
for (const argument of globalThis.arguments)
32+
if (argument.startsWith("--")) {
33+
const parts = argument.split("=");
34+
cliFlags[parts[0].toLowerCase()] = parts.slice(1).join("=");
35+
} else
36+
cliArgs.push(argument);
37+
}
38+
39+
function getIntFlag(flags, flag) {
40+
if (!(flag in flags))
41+
return undefined;
42+
const rawValue = flags[flag];
43+
const value = parseInt(rawValue);
44+
if (value <= 0)
45+
throw new Error(`Expected positive value for ${flag}, but got ${rawValue}`);
46+
return value;
47+
}
48+
49+
if ("--iteration-count" in cliFlags)
50+
globalThis.testIterationCount = getIntFlag(cliFlags, "--iteration-count");
51+
if ("--worst-case-count" in cliFlags)
52+
globalThis.testWorstCaseCount = getIntFlag(cliFlags, "--worst-case-count");
53+
if ("--dump-json-results" in cliFlags)
54+
globalThis.dumpJSONResults = true;
55+
if (typeof runMode !== "undefined" && runMode == "RAMification")
56+
globalThis.RAMification = true;
57+
if ("--ramification" in cliFlags)
58+
globalThis.RAMification = true;
59+
if (cliArgs.length)
60+
globalThis.testList = cliArgs;
61+
2862

2963
async function runJetStream() {
3064
try {
@@ -36,4 +70,29 @@ async function runJetStream() {
3670
throw e;
3771
}
3872
}
39-
runJetStream();
73+
74+
load("./JetStreamDriver.js");
75+
76+
if ("--help" in cliFlags) {
77+
console.log("JetStream Driver Help");
78+
console.log("");
79+
80+
console.log("Options:");
81+
console.log(" --iteration-count: Set the default iteration count.");
82+
console.log(" --worst-case-count: Set the default worst-case count");
83+
console.log(" --dump-json-results: Print summary json to the console.");
84+
console.log("");
85+
86+
console.log("Available tags:");
87+
const tagNames = Array.from(benchmarksByTag.keys()).sort();
88+
for (const tagName of tagNames)
89+
console.log(" ", tagName);
90+
console.log("");
91+
92+
console.log("Available tests:");
93+
const benchmarkNames = BENCHMARKS.map(b => b.name).sort();
94+
for (const benchmark of benchmarkNames)
95+
console.log(" ", benchmark);
96+
} else {
97+
runJetStream();
98+
}

shell-config.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,3 @@ if (isSpiderMonkey) {
3737
globalThis.readFile = readRelativeToScript;
3838
globalThis.arguments = scriptArgs;
3939
}
40-
if (globalThis.arguments?.length)
41-
globalThis.testList = globalThis.arguments.slice();
42-
43-
if (typeof runMode !== "undefined" && runMode == "RAMification")
44-
globalThis.RAMification = true;

0 commit comments

Comments
 (0)