• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const fork = require('child_process').fork;
4const path = require('path');
5const CLI = require('./_cli.js');
6
7//
8// Parse arguments
9//
10const cli = new CLI(`usage: ./node scatter.js [options] [--] <filename>
11  Run the benchmark script <filename> many times and output the rate (ops/s)
12  together with the benchmark variables as a csv.
13
14  --runs 30              number of samples
15  --set  variable=value  set benchmark variable (can be repeated)
16`, { arrayArgs: ['set'] });
17
18if (cli.items.length !== 1) {
19  cli.abort(cli.usage);
20}
21
22// Create queue from the benchmarks list such both node versions are tested
23// `runs` amount of times each.
24const filepath = path.resolve(cli.items[0]);
25const name = filepath.slice(__dirname.length + 1);
26const runs = cli.optional.runs ? parseInt(cli.optional.runs, 10) : 30;
27
28let printHeader = true;
29
30function csvEncodeValue(value) {
31  if (typeof value === 'number') {
32    return value.toString();
33  }
34  return `"${value.replace(/"/g, '""')}"`;
35}
36
37(function recursive(i) {
38  const child = fork(path.resolve(__dirname, filepath), cli.optional.set);
39
40  child.on('message', (data) => {
41    if (data.type !== 'report') {
42      return;
43    }
44
45    // print csv header
46    if (printHeader) {
47      const confHeader = Object.keys(data.conf)
48        .map(csvEncodeValue)
49        .join(', ');
50      console.log(`"filename", ${confHeader}, "rate", "time"`);
51      printHeader = false;
52    }
53
54    // print data row
55    const confData = Object.keys(data.conf)
56      .map((key) => csvEncodeValue(data.conf[key]))
57      .join(', ');
58
59    console.log(`"${name}", ${confData}, ${data.rate}, ${data.time}`);
60  });
61
62  child.once('close', (code) => {
63    if (code) {
64      process.exit(code);
65      return;
66    }
67
68    // If there are more benchmarks execute the next
69    if (i + 1 < runs) {
70      recursive(i + 1);
71    }
72  });
73})(0);
74