Skip to content

Commit

Permalink
benchmark: refactor code for benchmark cpu settings
Browse files Browse the repository at this point in the history
  • Loading branch information
thisalihassan committed Mar 30, 2024
1 parent 1b5b0a6 commit 9f7132d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
21 changes: 21 additions & 0 deletions benchmark/_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,24 @@ CLI.prototype.shouldSkip = function(scripts) {

return skip;
};

/**
* Extracts the CPU core setting from the CLI arguments.
* @returns {string|null} The CPU core setting if found, otherwise null.
*/
CLI.prototype.getCpuCoreSetting = function() {
const cpuCoreSetting = this.optional.set.find((s) => s.startsWith('CPUSET='));
if (!cpuCoreSetting) return null;

const value = cpuCoreSetting.split('=')[1];
// Validate the CPUSET value to match patterns like "0", "0-2", "0,1,2", "0,2-4,6" or "0,0,1-2"
const isValid = /^(\d+(-\d+)?)(,\d+(-\d+)?)*$/.test(value);
if (!isValid) {
throw new Error(`
Invalid CPUSET format: "${value}". Please use a single core number (e.g., "0"),
a range of cores (e.g., "0-3"), or a list of cores/ranges
(e.g., "0,2,4" or "0-2,4").\n\n${this.usage}
`);
}
return value;
};
15 changes: 8 additions & 7 deletions benchmark/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const cli = new CLI(`usage: ./node compare.js [options] [--] <category> ...
repeated)
--set variable=value set benchmark variable (can be repeated)
--no-progress don't show benchmark progress indicator
Examples:
--set CPUSET=0 Runs benchmarks on CPU core 0.
--set CPUSET=0-2 Specifies that benchmarks should run on CPU cores 0 to 2.
Note: The CPUSET format should match the specifications of the 'taskset' command
`, { arrayArgs: ['set', 'filter', 'exclude'], boolArgs: ['no-progress'] });

if (!cli.optional.new || !cli.optional.old) {
Expand All @@ -40,12 +46,6 @@ 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 @@ -75,8 +75,9 @@ if (showProgress) {

(function recursive(i) {
const job = queue[i];

const resolvedPath = path.resolve(__dirname, job.filename);

const cpuCore = cli.getCpuCoreSetting();
let child;
if (cpuCore !== null) {
const spawnArgs = ['-c', cpuCore, cli.optional[job.binary], resolvedPath, ...cli.optional.set];
Expand Down
17 changes: 9 additions & 8 deletions benchmark/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ const cli = new CLI(`usage: ./node run.js [options] [--] <category> ...
test only run a single configuration from the options
matrix
all each benchmark category is run one after the other
Examples:
--set CPUSET=0 Runs benchmarks on CPU core 0.
--set CPUSET=0-2 Specifies that benchmarks should run on CPU cores 0 to 2.
Note: The CPUSET format should match the specifications of the 'taskset' command on your system.
`, { arrayArgs: ['set', 'filter', 'exclude'] });

const benchmarks = cli.benchmarks();

if (benchmarks.length === 0) {
Expand All @@ -34,23 +41,17 @@ 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 scriptPath = path.resolve(__dirname, filename);
const args = cli.test ? ['--test'] : cli.optional.set;

const args = cli.test ? ['--test'] : cli.optional.set;
const cpuCore = cli.getCpuCoreSetting();
let child;

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

0 comments on commit 9f7132d

Please sign in to comment.