Skip to content

Commit

Permalink
[WIP]: jsdom: use parallel option value as the jobs option value.
Browse files Browse the repository at this point in the history
- add usage().
  • Loading branch information
h-sug1no committed Dec 21, 2021
1 parent b5ad91f commit f6d912a
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions tools/generate_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,24 @@ const log = (msg = 'undefined', type) => {
process.stdout.write('\n');
};

const usage = () => {
log('Usage:');
log(' node generate_images.js ver imageDir [--backend=<backend>] [--parallel=<jobs>]');
log('Options:');
log(' ver : specify the version to run.');
log(' imageDir : specify the directory to save images.');
log(
' --backend=<backend> : specify backends to run. default is all. available backends are "jsdom", "pptr", "all". default is "all"'
);
log(' --parallel=<jobs> : specify the number of parallel processes. default is the number of cpus');
process.exit(1);
};

const parseArgs = () => {
const argv = [...process.argv];
if (argv.length < 4) {
usage();
}

const argv0 = argv.shift();
argv.shift(); // skip this script name.
Expand All @@ -23,12 +39,13 @@ const parseArgs = () => {
};

let backends;
let parallel;
let parallel = require('os').cpus().length;

argv.forEach((str) => {
const prop = str.split('=');
const name = (prop[0] || '').toLowerCase();
const value = prop[1];
const intValue = parseInt(value, 10);
switch (name) {
case '--backend':
backends = backends || {};
Expand All @@ -40,17 +57,25 @@ const parseArgs = () => {
case 'pptr':
case 'jsdom':
case 'all':
case 'none':
backends[backend] = true;
break;
default:
log(`unknown backend: ${backend}`, 'error');
usage();
break;
}
});
break;
case '--parallel':
parallel = parseInt(value);
if (value && !Number.isNaN(intValue)) {
parallel = intValue;
} else {
log(`invalid value for --parallel: ${value}`, 'error');
usage();
}
break;
case '--help':
usage();
default:
childArgs.args.push(str);
break;
Expand All @@ -64,23 +89,23 @@ const parseArgs = () => {
return {
childArgs,
backends,
parallel,
parallel: parallel <= 1 ? 1 : parallel,
};
};

const resolveJobsOption = (childArgs) => {
const resolveJobsOption = (ver) => {
let numTestes = NaN;
let allJobs = 1;
let pptrJobs = 1;

try {
global.Vex = require(`../${childArgs.ver}/vexflow-debug-with-tests.js`);
global.Vex = require(`../${ver}/vexflow-debug-with-tests.js`);
if (global.Vex) {
const { Flow } = global.Vex;
if (Flow) {
const { Test } = Flow;
if (Test && Test.tests && Test.parseJobOptions) {
numTestes = Test.tests.length;
allJobs = Math.ceil(numTestes / 10);
pptrJobs = Math.ceil(numTestes / 10);
}
}
}
Expand All @@ -91,30 +116,30 @@ const resolveJobsOption = (childArgs) => {

return {
numTestes,
allJobs,
pptrJobs,
};
};

const appMain = async () => {
const options = parseArgs();
const { childArgs, backends, parallel } = options;
const { numTestes, allJobs } = resolveJobsOption(childArgs);
const { numTestes, pptrJobs } = resolveJobsOption(childArgs.ver);
const { ver, imageDir, args } = childArgs;

const jobs = parallel <= 1 ? 1 : allJobs;

const backendDefs = {
jsdom: {
path: './tools/generate_png_images.js',
getArgs: () => {
return [`../${ver}`, imageDir].concat(args);
},
jobs: parallel,
},
pptr: {
path: './tools/generate_images_pptr.js',
getArgs: () => {
return [ver, imageDir].concat(args);
},
jobs: parallel <= 1 ? 1 : pptrJobs,
},
};

Expand Down Expand Up @@ -147,12 +172,12 @@ const appMain = async () => {
let ps = [];
let keys = [];
let key = 0;
const procs = require('os').cpus().length;
log(JSON.stringify(backends));
for (const backend in backendDefs) {
if (!exitCode && !backends.none && (backends.all || backends[backend])) {
const { jobs } = backendDefs[backend];
for (let i = 0; (!exitCode && i < jobs) || ps.length; ) {
while (i < jobs && ps.length < procs) {
while (i < jobs && ps.length < parallel) {
ps.push(execChild(backend, jobs, i, key));
key += 1;
keys.push(i);
Expand Down

0 comments on commit f6d912a

Please sign in to comment.