Skip to content

Commit bf8e66e

Browse files
committed
benchmark: refactor code for benchmark cpu settings
1 parent 74f415e commit bf8e66e

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

benchmark/_cli.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,24 @@ CLI.prototype.shouldSkip = function(scripts) {
125125

126126
return skip;
127127
};
128+
129+
/**
130+
* Extracts the CPU core setting from the CLI arguments.
131+
* @returns {string|null} The CPU core setting if found, otherwise null.
132+
*/
133+
CLI.prototype.getCpuCoreSetting = function() {
134+
const cpuCoreSetting = this.optional.set.find((s) => s.startsWith('CPUSET='));
135+
if (!cpuCoreSetting) return null;
136+
137+
const value = cpuCoreSetting.split('=')[1];
138+
// Validate the CPUSET value to match patterns like "0", "0-2", "0,1,2", "0,2-4,6" or "0,0,1-2"
139+
const isValid = /^(\d+(-\d+)?)(,\d+(-\d+)?)*$/.test(value);
140+
if (!isValid) {
141+
throw new Error(`
142+
Invalid CPUSET format: "${value}". Please use a single core number (e.g., "0"),
143+
a range of cores (e.g., "0-3"), or a list of cores/ranges
144+
(e.g., "0,2,4" or "0-2,4").\n\n${this.usage}
145+
`);
146+
}
147+
return value;
148+
};

benchmark/compare.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const cli = new CLI(`usage: ./node compare.js [options] [--] <category> ...
2424
repeated)
2525
--set variable=value set benchmark variable (can be repeated)
2626
--no-progress don't show benchmark progress indicator
27+
28+
Examples:
29+
--set CPUSET=0 Runs benchmarks on CPU core 0.
30+
--set CPUSET=0-2 Specifies that benchmarks should run on CPU cores 0 to 2.
31+
32+
Note: The CPUSET format should match the specifications of the 'taskset' command
2733
`, { arrayArgs: ['set', 'filter', 'exclude'], boolArgs: ['no-progress'] });
2834

2935
if (!cli.optional.new || !cli.optional.old) {
@@ -40,12 +46,6 @@ if (benchmarks.length === 0) {
4046
return;
4147
}
4248

43-
const cpuCoreSetting = cli.optional.set.find((s) => s.startsWith('CPUCORE='));
44-
let cpuCore = null;
45-
if (cpuCoreSetting) {
46-
cpuCore = cpuCoreSetting.split('=')[1];
47-
}
48-
4949
// Create queue from the benchmarks list such both node versions are tested
5050
// `runs` amount of times each.
5151
// Note: BenchmarkProgress relies on this order to estimate
@@ -75,8 +75,9 @@ if (showProgress) {
7575

7676
(function recursive(i) {
7777
const job = queue[i];
78-
7978
const resolvedPath = path.resolve(__dirname, job.filename);
79+
80+
const cpuCore = cli.getCpuCoreSetting();
8081
let child;
8182
if (cpuCore !== null) {
8283
const spawnArgs = ['-c', cpuCore, cli.optional[job.binary], resolvedPath, ...cli.optional.set];

benchmark/run.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ const cli = new CLI(`usage: ./node run.js [options] [--] <category> ...
1717
test only run a single configuration from the options
1818
matrix
1919
all each benchmark category is run one after the other
20+
21+
Examples:
22+
--set CPUSET=0 Runs benchmarks on CPU core 0.
23+
--set CPUSET=0-2 Specifies that benchmarks should run on CPU cores 0 to 2.
24+
25+
Note: The CPUSET format should match the specifications of the 'taskset' command on your system.
2026
`, { arrayArgs: ['set', 'filter', 'exclude'] });
27+
2128
const benchmarks = cli.benchmarks();
2229

2330
if (benchmarks.length === 0) {
@@ -34,23 +41,17 @@ if (!validFormats.includes(format)) {
3441
return;
3542
}
3643

37-
const cpuCoreSetting = cli.optional.set.find((s) => s.startsWith('CPUCORE='));
38-
let cpuCore = null;
39-
if (cpuCoreSetting) {
40-
cpuCore = cpuCoreSetting.split('=')[1];
41-
}
42-
4344
if (format === 'csv') {
4445
console.log('"filename", "configuration", "rate", "time"');
4546
}
4647

4748
(function recursive(i) {
4849
const filename = benchmarks[i];
4950
const scriptPath = path.resolve(__dirname, filename);
50-
const args = cli.test ? ['--test'] : cli.optional.set;
5151

52+
const args = cli.test ? ['--test'] : cli.optional.set;
53+
const cpuCore = cli.getCpuCoreSetting();
5254
let child;
53-
5455
if (cpuCore !== null) {
5556
child = spawn('taskset', ['-c', cpuCore, 'node', scriptPath, ...args], {
5657
stdio: ['inherit', 'pipe', 'ipc'],

0 commit comments

Comments
 (0)