2016-02-01 10:30:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
// Create an object of all benchmark scripts
|
|
|
|
const benchmarks = {};
|
|
|
|
fs.readdirSync(__dirname)
|
2020-02-12 18:33:33 +00:00
|
|
|
.filter((name) => {
|
|
|
|
return name !== 'fixtures' &&
|
|
|
|
fs.statSync(path.resolve(__dirname, name)).isDirectory();
|
|
|
|
})
|
2019-02-05 06:06:08 +00:00
|
|
|
.forEach((category) => {
|
2016-02-01 10:30:00 +00:00
|
|
|
benchmarks[category] = fs.readdirSync(path.resolve(__dirname, category))
|
|
|
|
.filter((filename) => filename[0] !== '.' && filename[0] !== '_');
|
|
|
|
});
|
|
|
|
|
|
|
|
function CLI(usage, settings) {
|
|
|
|
if (process.argv.length < 3) {
|
2019-03-22 02:44:26 +00:00
|
|
|
this.abort(usage); // Abort will exit the process
|
2016-02-01 10:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.usage = usage;
|
|
|
|
this.optional = {};
|
|
|
|
this.items = [];
|
2020-02-12 18:33:33 +00:00
|
|
|
this.test = false;
|
2016-02-01 10:30:00 +00:00
|
|
|
|
|
|
|
for (const argName of settings.arrayArgs) {
|
|
|
|
this.optional[argName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let currentOptional = null;
|
2019-01-21 00:22:27 +00:00
|
|
|
let mode = 'both'; // Possible states are: [both, option, item]
|
2016-02-01 10:30:00 +00:00
|
|
|
|
|
|
|
for (const arg of process.argv.slice(2)) {
|
2024-08-06 18:21:27 +00:00
|
|
|
if (arg === '--help') this.abort(usage);
|
2016-02-01 10:30:00 +00:00
|
|
|
if (arg === '--') {
|
|
|
|
// Only items can follow --
|
|
|
|
mode = 'item';
|
2020-02-12 18:33:33 +00:00
|
|
|
} else if (mode === 'both' && arg[0] === '-') {
|
2016-02-01 10:30:00 +00:00
|
|
|
// Optional arguments declaration
|
|
|
|
|
|
|
|
if (arg[1] === '-') {
|
|
|
|
currentOptional = arg.slice(2);
|
|
|
|
} else {
|
|
|
|
currentOptional = arg.slice(1);
|
|
|
|
}
|
|
|
|
|
2017-01-15 18:59:13 +00:00
|
|
|
if (settings.boolArgs && settings.boolArgs.includes(currentOptional)) {
|
2016-02-01 10:30:00 +00:00
|
|
|
this.optional[currentOptional] = true;
|
2017-01-15 18:59:13 +00:00
|
|
|
mode = 'both';
|
|
|
|
} else {
|
2018-12-03 16:15:45 +00:00
|
|
|
// Expect the next value to be option related (either -- or the value)
|
2017-01-15 18:59:13 +00:00
|
|
|
mode = 'option';
|
2016-02-01 10:30:00 +00:00
|
|
|
}
|
|
|
|
} else if (mode === 'option') {
|
|
|
|
// Optional arguments value
|
|
|
|
|
|
|
|
if (settings.arrayArgs.includes(currentOptional)) {
|
|
|
|
this.optional[currentOptional].push(arg);
|
|
|
|
} else {
|
|
|
|
this.optional[currentOptional] = arg;
|
|
|
|
}
|
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// The next value can be either an option or an item
|
2016-02-01 10:30:00 +00:00
|
|
|
mode = 'both';
|
2020-02-12 18:33:33 +00:00
|
|
|
} else if (arg === 'test') {
|
|
|
|
this.test = true;
|
2016-02-01 10:30:00 +00:00
|
|
|
} else if (['both', 'item'].includes(mode)) {
|
|
|
|
// item arguments
|
|
|
|
this.items.push(arg);
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// The next value must be an item
|
2016-02-01 10:30:00 +00:00
|
|
|
mode = 'item';
|
|
|
|
} else {
|
|
|
|
// Bad case, abort
|
|
|
|
this.abort(usage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = CLI;
|
|
|
|
|
|
|
|
CLI.prototype.abort = function(msg) {
|
|
|
|
console.error(msg);
|
|
|
|
process.exit(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
CLI.prototype.benchmarks = function() {
|
|
|
|
const paths = [];
|
|
|
|
|
2020-02-12 18:33:33 +00:00
|
|
|
if (this.items.includes('all')) {
|
|
|
|
this.items = Object.keys(benchmarks);
|
|
|
|
}
|
|
|
|
|
2016-02-01 10:30:00 +00:00
|
|
|
for (const category of this.items) {
|
2020-02-12 18:33:33 +00:00
|
|
|
if (benchmarks[category] === undefined) {
|
|
|
|
console.error(`The "${category}" category does not exist.`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2016-02-01 10:30:00 +00:00
|
|
|
for (const scripts of benchmarks[category]) {
|
2019-10-15 22:14:20 +00:00
|
|
|
if (this.shouldSkip(scripts)) continue;
|
2016-02-01 10:30:00 +00:00
|
|
|
|
|
|
|
paths.push(path.join(category, scripts));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
};
|
2019-10-15 22:14:20 +00:00
|
|
|
|
|
|
|
CLI.prototype.shouldSkip = function(scripts) {
|
|
|
|
const filters = this.optional.filter || [];
|
|
|
|
const excludes = this.optional.exclude || [];
|
|
|
|
let skip = filters.length > 0;
|
|
|
|
|
|
|
|
for (const filter of filters) {
|
|
|
|
if (scripts.lastIndexOf(filter) !== -1) {
|
|
|
|
skip = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const exclude of excludes) {
|
|
|
|
if (scripts.lastIndexOf(exclude) !== -1) {
|
|
|
|
skip = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return skip;
|
|
|
|
};
|
2024-04-06 22:43:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|