-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
benchmark: rewrite the startup performance benchmark #45746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
996719f
benchmark: make benchmarks runnable in older versions of Node.js
joyeecheung bc05a5f
benchmark: rewrite the startup performance benchmark
joyeecheung 4b1b762
fixup! benchmark: rewrite the startup performance benchmark
joyeecheung 9f82938
fixup! fixup! benchmark: rewrite the startup performance benchmark
joyeecheung ee2cbe3
fixup! fixup! fixup! benchmark: rewrite the startup performance bench…
joyeecheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,80 +1,68 @@ | ||
| 'use strict'; | ||
| const common = require('../common.js'); | ||
| const { spawn } = require('child_process'); | ||
| const { spawn, spawnSync } = require('child_process'); | ||
| const path = require('path'); | ||
|
|
||
| let Worker; // Lazy loaded in main | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| dur: [1], | ||
| script: [ | ||
| 'benchmark/fixtures/require-builtins', | ||
| 'benchmark/fixtures/require-cachable', | ||
| 'test/fixtures/semicolon', | ||
| ], | ||
| mode: ['process', 'worker'] | ||
| }, { | ||
| flags: ['--expose-internals'] | ||
| mode: ['process', 'worker'], | ||
| count: [30], | ||
| }); | ||
|
|
||
| function spawnProcess(script) { | ||
| function spawnProcess(script, bench, state) { | ||
| const cmd = process.execPath || process.argv[0]; | ||
| const argv = ['--expose-internals', script]; | ||
| return spawn(cmd, argv); | ||
| } | ||
| while (state.finished < state.count) { | ||
| const child = spawnSync(cmd, [script]); | ||
| if (child.status !== 0) { | ||
| console.log('---- STDOUT ----'); | ||
| console.log(child.stdout.toString()); | ||
| console.log('---- STDERR ----'); | ||
| console.log(child.stderr.toString()); | ||
| throw new Error(`Child process stopped with exit code ${child.status}`); | ||
| } | ||
| state.finished++; | ||
| if (state.finished === 0) { | ||
| // Finished warmup. | ||
| bench.start(); | ||
| } | ||
|
|
||
| function spawnWorker(script) { | ||
| return new Worker(script, { stderr: true, stdout: true }); | ||
| if (state.finished == state.count) { | ||
| bench.end(state.count); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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) => { | ||
| function spawnWorker(script, bench, state) { | ||
| const child = new Worker(script); | ||
| child.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}`); | ||
| throw new Error(`Worker stopped with exit code ${code}`); | ||
| } | ||
| state.throughput++; | ||
|
|
||
| if (state.go) { | ||
| start(state, script, bench, getNode); | ||
| state.finished++; | ||
| if (state.finished === 0) { | ||
| // Finished warmup. | ||
| bench.start(); | ||
| } | ||
| if (state.finished < state.count) { | ||
| spawnProcess(script, bench, state); | ||
| } else { | ||
| bench.end(state.throughput); | ||
| bench.end(state.count); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function main({ dur, script, mode }) { | ||
| const state = { | ||
| go: true, | ||
| throughput: 0 | ||
| }; | ||
|
|
||
| setTimeout(() => { | ||
| state.go = false; | ||
| }, dur * 1000); | ||
|
|
||
| function main({ count, script, mode }) { | ||
| script = path.resolve(__dirname, '../../', `${script}.js`); | ||
| const state = { count, finished: -3 }; | ||
| if (mode === 'worker') { | ||
| Worker = require('worker_threads').Worker; | ||
| bench.start(); | ||
| start(state, script, bench, spawnWorker); | ||
| Worker = require('worker_threads').Worker; // Warm up. | ||
| spawnWorker(script, bench, state); | ||
| } else { | ||
| bench.start(); | ||
| start(state, script, bench, spawnProcess); | ||
| spawnProcess(script, bench, state); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.