2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2017-09-14 03:48:53 +02:00
|
|
|
const common = require('../common.js');
|
2018-11-06 21:28:50 +01:00
|
|
|
const { spawn } = require('child_process');
|
2017-09-14 03:48:53 +02:00
|
|
|
const path = require('path');
|
2013-02-11 22:24:27 +01:00
|
|
|
|
2018-11-06 21:28:50 +01:00
|
|
|
let Worker; // Lazy loaded in main
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
dur: [1],
|
|
|
|
script: ['benchmark/fixtures/require-cachable', 'test/fixtures/semicolon'],
|
|
|
|
mode: ['process', 'worker']
|
|
|
|
}, {
|
2019-01-06 01:52:16 +01:00
|
|
|
flags: ['--expose-internals']
|
2013-02-11 22:24:27 +01:00
|
|
|
});
|
|
|
|
|
2018-11-06 21:28:50 +01:00
|
|
|
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
|
|
|
|
};
|
2013-02-11 22:24:27 +01:00
|
|
|
|
2019-02-05 07:06:08 +01:00
|
|
|
setTimeout(() => {
|
2018-11-06 21:28:50 +01:00
|
|
|
state.go = false;
|
2013-02-11 22:24:27 +01:00
|
|
|
}, dur * 1000);
|
|
|
|
|
2018-11-06 21:28:50 +01:00
|
|
|
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);
|
2013-02-11 22:24:27 +01:00
|
|
|
}
|
|
|
|
}
|