Skip to content

Commit 3fb5b79

Browse files
committed
benchmark: conditionally use spawn with taskset for CPU pinning
Fixes: #52233
1 parent 27493a1 commit 3fb5b79

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

benchmark/compare.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { fork } = require('child_process');
3+
const { spawn, fork } = require('child_process');
44
const { inspect } = require('util');
55
const path = require('path');
66
const CLI = require('./_cli.js');
@@ -40,6 +40,12 @@ if (benchmarks.length === 0) {
4040
return;
4141
}
4242

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+
4349
// Create queue from the benchmarks list such both node versions are tested
4450
// `runs` amount of times each.
4551
// Note: BenchmarkProgress relies on this order to estimate
@@ -70,9 +76,23 @@ if (showProgress) {
7076
(function recursive(i) {
7177
const job = queue[i];
7278

73-
const child = fork(path.resolve(__dirname, job.filename), cli.optional.set, {
74-
execPath: cli.optional[job.binary],
75-
});
79+
const resolvedPath = path.resolve(__dirname, job.filename);
80+
let child;
81+
if (cpuCore !== null) {
82+
const spawnArgs = ['-c', cpuCore, cli.optional[job.binary], resolvedPath, ...cli.optional.set];
83+
child = spawn('taskset', spawnArgs, {
84+
env: process.env,
85+
stdio: ['inherit', 'pipe', 'ipc'],
86+
});
87+
88+
child.stdout.on('data', (data) => {
89+
process.stdout.write(data);
90+
});
91+
} else {
92+
child = fork(resolvedPath, cli.optional.set, {
93+
execPath: cli.optional[job.binary],
94+
});
95+
}
7696

7797
child.on('message', (data) => {
7898
if (data.type === 'report') {

benchmark/run.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4-
const fork = require('child_process').fork;
4+
const { spawn, fork } = require('child_process');
55
const CLI = require('./_cli.js');
66

77
const cli = new CLI(`usage: ./node run.js [options] [--] <category> ...
@@ -34,16 +34,37 @@ if (!validFormats.includes(format)) {
3434
return;
3535
}
3636

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+
3743
if (format === 'csv') {
3844
console.log('"filename", "configuration", "rate", "time"');
3945
}
4046

4147
(function recursive(i) {
4248
const filename = benchmarks[i];
43-
const child = fork(
44-
path.resolve(__dirname, filename),
45-
cli.test ? ['--test'] : cli.optional.set,
46-
);
49+
const scriptPath = path.resolve(__dirname, filename);
50+
const args = cli.test ? ['--test'] : cli.optional.set;
51+
52+
let child;
53+
54+
if (cpuCore !== null) {
55+
child = spawn('taskset', [`-c`, cpuCore, `node`, scriptPath, ...args], {
56+
stdio: ['inherit', 'pipe', 'ipc']
57+
});
58+
59+
child.stdout.on('data', (data) => {
60+
process.stdout.write(data);
61+
});
62+
} else {
63+
child = fork(
64+
scriptPath,
65+
args,
66+
);
67+
}
4768

4869
if (format !== 'csv') {
4970
console.log();

0 commit comments

Comments
 (0)