|
| 1 | +'use strict'; |
| 2 | +const common = require('../common.js'); |
| 3 | +const { spawnSync } = require('child_process'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// This benchmarks the startup of various CLI tools that are already |
| 7 | +// checked into the source code. We use --version because the output |
| 8 | +// tends to be minimal and fewer operations are done to generate |
| 9 | +// these so that the startup cost is still dominated by a more |
| 10 | +// indispensible part of the CLI. |
| 11 | +const bench = common.createBenchmark(main, { |
| 12 | + cli: [ |
| 13 | + 'tools/node_modules/eslint/bin/eslint.js', |
| 14 | + 'deps/npm/bin/npm-cli.js', |
| 15 | + 'deps/corepack/dist/corepack.js', |
| 16 | + ], |
| 17 | + count: [30], |
| 18 | +}); |
| 19 | + |
| 20 | +function spawnProcess(cli, bench, state) { |
| 21 | + const cmd = process.execPath || process.argv[0]; |
| 22 | + while (state.finished < state.count) { |
| 23 | + const child = spawnSync(cmd, [cli, '--version'], { |
| 24 | + env: { npm_config_loglevel: 'silent', ...process.env }, |
| 25 | + }); |
| 26 | + // Log some information for debugging if it fails, which it shouldn't. |
| 27 | + if (child.status !== 0) { |
| 28 | + console.log('---- STDOUT ----'); |
| 29 | + console.log(child.stdout.toString()); |
| 30 | + console.log('---- STDERR ----'); |
| 31 | + console.log(child.stderr.toString()); |
| 32 | + throw new Error(`Child process stopped with exit code ${child.status}`); |
| 33 | + } |
| 34 | + state.finished++; |
| 35 | + if (state.finished === 0) { |
| 36 | + // Finished warmup. |
| 37 | + bench.start(); |
| 38 | + } |
| 39 | + |
| 40 | + if (state.finished === state.count) { |
| 41 | + bench.end(state.count); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function main({ count, cli }) { |
| 47 | + cli = path.resolve(__dirname, '../../', cli); |
| 48 | + const warmup = 3; |
| 49 | + const state = { count, finished: -warmup }; |
| 50 | + spawnProcess(cli, bench, state); |
| 51 | +} |
0 commit comments