• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const { spawn } = require('child_process');
4
5const bench = common.createBenchmark(main, {
6  dur: [1],
7  code: ['1', '"string"', 'process.versions', 'process']
8});
9
10function spawnProcess(code) {
11  const cmd = process.execPath || process.argv[0];
12  const argv = ['-p', code];
13  return spawn(cmd, argv);
14}
15
16function start(state, code, bench, getNode) {
17  const node = getNode(code);
18  let stdout = '';
19  let stderr = '';
20
21  node.stdout.on('data', (data) => {
22    stdout += data;
23  });
24
25  node.stderr.on('data', (data) => {
26    stderr += data;
27  });
28
29  node.on('exit', (code) => {
30    if (code !== 0) {
31      console.error('------ stdout ------');
32      console.error(stdout);
33      console.error('------ stderr ------');
34      console.error(stderr);
35      throw new Error(`Error during node startup, exit code ${code}`);
36    }
37    state.throughput++;
38
39    if (state.go) {
40      start(state, code, bench, getNode);
41    } else {
42      bench.end(state.throughput);
43    }
44  });
45}
46
47function main({ dur, code }) {
48  const state = {
49    go: true,
50    throughput: 0
51  };
52
53  setTimeout(() => {
54    state.go = false;
55  }, dur * 1000);
56
57  bench.start();
58  start(state, code, bench, spawnProcess);
59}
60