-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: support more options in startup benchmark
1. Add options to benchmark the startup performance of a node "instance" after running a script. By default there are two options: `test/fixtures/semicolon` which is basically an empty file, and `benchmark/fixtures/require-cachable` which require all the cachable modules before exiting. This allows us to measure the overhead of bootstrap in more scenarios. 2. Add options to benchmark the overhead of spinning node through a process and through a worker. PR-URL: #24220 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
- Loading branch information
1 parent
0063448
commit ad5c9b4
Showing
3 changed files
with
79 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const list = require('internal/bootstrap/cache'); | ||
const { | ||
isMainThread | ||
} = require('worker_threads'); | ||
|
||
for (const key of list.cachableBuiltins) { | ||
if (!isMainThread && key === 'trace_events') { | ||
continue; | ||
} | ||
require(key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,76 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const spawn = require('child_process').spawn; | ||
const { spawn } = require('child_process'); | ||
const path = require('path'); | ||
const emptyJsFile = path.resolve(__dirname, '../../test/fixtures/semicolon.js'); | ||
|
||
const bench = common.createBenchmark(startNode, { | ||
dur: [1] | ||
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', '--experimental-worker'] // for workers | ||
}); | ||
|
||
function startNode({ dur }) { | ||
var go = true; | ||
var starts = 0; | ||
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() { | ||
go = false; | ||
state.go = false; | ||
}, dur * 1000); | ||
|
||
bench.start(); | ||
start(); | ||
|
||
function start() { | ||
const node = spawn(process.execPath || process.argv[0], [emptyJsFile]); | ||
node.on('exit', function(exitCode) { | ||
if (exitCode !== 0) { | ||
throw new Error('Error during node startup'); | ||
} | ||
starts++; | ||
|
||
if (go) | ||
start(); | ||
else | ||
bench.end(starts); | ||
}); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters