Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
benchmark: conditionally use spawn with taskset for cpu pinning
This change enhances the benchmarking tool by conditionally using the,
spawn method with taskset for CPU pinning, improving consistency of
benchmark results across different environments.

Fixes: #52233
  • Loading branch information
thisalihassan committed Mar 31, 2024
commit 28c68f063055cd73669006f326683abfe43feaa4
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];
Comment thread
thisalihassan marked this conversation as resolved.
Outdated
}

// 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) => {
Comment thread
thisalihassan marked this conversation as resolved.
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');
Comment thread
thisalihassan marked this conversation as resolved.
Outdated
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']
});

Comment thread
mcollina marked this conversation as resolved.
child.stdout.on('data', (data) => {
process.stdout.write(data);
});
} else {
child = fork(
scriptPath,
args,
);
}

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