Skip to content

Commit

Permalink
benchmark: conditionally use spawn with taskset for CPU pinning
Browse files Browse the repository at this point in the history
Fixes: #52233
  • Loading branch information
thisalihassan committed Mar 28, 2024
1 parent 27493a1 commit 3fb5b79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
28 changes: 24 additions & 4 deletions benchmark/compare.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { fork } = require('child_process');
const { spawn, fork } = require('child_process');
const { inspect } = require('util');
const path = require('path');
const CLI = require('./_cli.js');
Expand Down Expand Up @@ -40,6 +40,12 @@ if (benchmarks.length === 0) {
return;
}

const cpuCoreSetting = cli.optional.set.find(s => s.startsWith('CPUCORE='));
let cpuCore = null;
if (cpuCoreSetting) {
cpuCore = cpuCoreSetting.split('=')[1];
}

// Create queue from the benchmarks list such both node versions are tested
// `runs` amount of times each.
// Note: BenchmarkProgress relies on this order to estimate
Expand Down Expand Up @@ -70,9 +76,23 @@ if (showProgress) {
(function recursive(i) {
const job = queue[i];

const child = fork(path.resolve(__dirname, job.filename), cli.optional.set, {
execPath: cli.optional[job.binary],
});
const resolvedPath = path.resolve(__dirname, job.filename);
let child;
if (cpuCore !== null) {
const spawnArgs = ['-c', cpuCore, cli.optional[job.binary], resolvedPath, ...cli.optional.set];
child = spawn('taskset', spawnArgs, {
env: process.env,
stdio: ['inherit', 'pipe', 'ipc'],
});

child.stdout.on('data', (data) => {
process.stdout.write(data);
});
} else {
child = fork(resolvedPath, cli.optional.set, {
execPath: cli.optional[job.binary],
});
}

child.on('message', (data) => {
if (data.type === 'report') {
Expand Down
31 changes: 26 additions & 5 deletions benchmark/run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const fork = require('child_process').fork;
const { spawn, fork } = require('child_process');
const CLI = require('./_cli.js');

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

const cpuCoreSetting = cli.optional.set.find(s => s.startsWith('CPUCORE='));
let cpuCore = null;
if (cpuCoreSetting) {
cpuCore = cpuCoreSetting.split('=')[1];
}

if (format === 'csv') {
console.log('"filename", "configuration", "rate", "time"');
}

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

let child;

if (cpuCore !== null) {
child = spawn('taskset', [`-c`, cpuCore, `node`, scriptPath, ...args], {
stdio: ['inherit', 'pipe', 'ipc']
});

child.stdout.on('data', (data) => {
process.stdout.write(data);
});
} else {
child = fork(
scriptPath,
args,
);
}

if (format !== 'csv') {
console.log();
Expand Down

0 comments on commit 3fb5b79

Please sign in to comment.