summaryrefslogtreecommitdiff
path: root/benchmark/misc/startup.js
blob: c1c893b0011abdcfb48211fbe21428f87192636e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const common = require('../common.js');
const { spawn } = require('child_process');
const path = require('path');

let Worker;  // Lazy loaded in main

const bench = common.createBenchmark(main, {
  dur: [1],
  script: ['benchmark/fixtures/require-cachable', 'test/fixtures/semicolon'],
  mode: ['process', 'worker']
}, {
  flags: ['--expose-internals']
});

function spawnProcess(script) {
  const cmd = process.execPath || process.argv[0];
  const argv = ['--expose-internals', script];
  return spawn(cmd, argv);
}

function spawnWorker(script) {
  return new Worker(script, { stderr: true, stdout: true });
}

function start(state, script, bench, getNode) {
  const node = getNode(script);
  let stdout = '';
  let stderr = '';

  node.stdout.on('data', (data) => {
    stdout += data;
  });

  node.stderr.on('data', (data) => {
    stderr += data;
  });

  node.on('exit', (code) => {
    if (code !== 0) {
      console.error('------ stdout ------');
      console.error(stdout);
      console.error('------ stderr ------');
      console.error(stderr);
      throw new Error(`Error during node startup, exit code ${code}`);
    }
    state.throughput++;

    if (state.go) {
      start(state, script, bench, getNode);
    } else {
      bench.end(state.throughput);
    }
  });
}

function main({ dur, script, mode }) {
  const state = {
    go: true,
    throughput: 0
  };

  setTimeout(function() {
    state.go = false;
  }, dur * 1000);

  script = path.resolve(__dirname, '../../', `${script}.js`);
  if (mode === 'worker') {
    Worker = require('worker_threads').Worker;
    bench.start();
    start(state, script, bench, spawnWorker);
  } else {
    bench.start();
    start(state, script, bench, spawnProcess);
  }
}