xref: /third_party/node/benchmark/scatter.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst fork = require('child_process').fork;
41cb0ef41Sopenharmony_ciconst path = require('path');
51cb0ef41Sopenharmony_ciconst CLI = require('./_cli.js');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci//
81cb0ef41Sopenharmony_ci// Parse arguments
91cb0ef41Sopenharmony_ci//
101cb0ef41Sopenharmony_ciconst cli = new CLI(`usage: ./node scatter.js [options] [--] <filename>
111cb0ef41Sopenharmony_ci  Run the benchmark script <filename> many times and output the rate (ops/s)
121cb0ef41Sopenharmony_ci  together with the benchmark variables as a csv.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  --runs 30              number of samples
151cb0ef41Sopenharmony_ci  --set  variable=value  set benchmark variable (can be repeated)
161cb0ef41Sopenharmony_ci`, { arrayArgs: ['set'] });
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciif (cli.items.length !== 1) {
191cb0ef41Sopenharmony_ci  cli.abort(cli.usage);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Create queue from the benchmarks list such both node versions are tested
231cb0ef41Sopenharmony_ci// `runs` amount of times each.
241cb0ef41Sopenharmony_ciconst filepath = path.resolve(cli.items[0]);
251cb0ef41Sopenharmony_ciconst name = filepath.slice(__dirname.length + 1);
261cb0ef41Sopenharmony_ciconst runs = cli.optional.runs ? parseInt(cli.optional.runs, 10) : 30;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cilet printHeader = true;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction csvEncodeValue(value) {
311cb0ef41Sopenharmony_ci  if (typeof value === 'number') {
321cb0ef41Sopenharmony_ci    return value.toString();
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci  return `"${value.replace(/"/g, '""')}"`;
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci(function recursive(i) {
381cb0ef41Sopenharmony_ci  const child = fork(path.resolve(__dirname, filepath), cli.optional.set);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  child.on('message', (data) => {
411cb0ef41Sopenharmony_ci    if (data.type !== 'report') {
421cb0ef41Sopenharmony_ci      return;
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    // print csv header
461cb0ef41Sopenharmony_ci    if (printHeader) {
471cb0ef41Sopenharmony_ci      const confHeader = Object.keys(data.conf)
481cb0ef41Sopenharmony_ci        .map(csvEncodeValue)
491cb0ef41Sopenharmony_ci        .join(', ');
501cb0ef41Sopenharmony_ci      console.log(`"filename", ${confHeader}, "rate", "time"`);
511cb0ef41Sopenharmony_ci      printHeader = false;
521cb0ef41Sopenharmony_ci    }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    // print data row
551cb0ef41Sopenharmony_ci    const confData = Object.keys(data.conf)
561cb0ef41Sopenharmony_ci      .map((key) => csvEncodeValue(data.conf[key]))
571cb0ef41Sopenharmony_ci      .join(', ');
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    console.log(`"${name}", ${confData}, ${data.rate}, ${data.time}`);
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  child.once('close', (code) => {
631cb0ef41Sopenharmony_ci    if (code) {
641cb0ef41Sopenharmony_ci      process.exit(code);
651cb0ef41Sopenharmony_ci      return;
661cb0ef41Sopenharmony_ci    }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    // If there are more benchmarks execute the next
691cb0ef41Sopenharmony_ci    if (i + 1 < runs) {
701cb0ef41Sopenharmony_ci      recursive(i + 1);
711cb0ef41Sopenharmony_ci    }
721cb0ef41Sopenharmony_ci  });
731cb0ef41Sopenharmony_ci})(0);
74