diff options
author | 2023-07-10 23:43:01 +0800 | |
---|---|---|
committer | 2023-07-10 23:43:01 +0800 | |
commit | 255dead86fdcc9f744f58a69c0f32ee70dd3ab90 (patch) | |
tree | c60e492733f47ac74184e371a84079063005329e /benchmark/bench/cli-startup.js | |
parent | 65ecbcb2867030a7e0a9bb65e808fdcb7e430f5a (diff) | |
download | astro-255dead86fdcc9f744f58a69c0f32ee70dd3ab90.tar.gz astro-255dead86fdcc9f744f58a69c0f32ee70dd3ab90.tar.zst astro-255dead86fdcc9f744f58a69c0f32ee70dd3ab90.zip |
Add CLI startup speed benchmark (#7617)
Diffstat (limited to 'benchmark/bench/cli-startup.js')
-rw-r--r-- | benchmark/bench/cli-startup.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/benchmark/bench/cli-startup.js b/benchmark/bench/cli-startup.js new file mode 100644 index 000000000..42d14cd71 --- /dev/null +++ b/benchmark/bench/cli-startup.js @@ -0,0 +1,73 @@ +import { fileURLToPath } from 'url'; +import { execaCommand } from 'execa'; +import { markdownTable } from 'markdown-table'; +import { astroBin, calculateStat } from './_util.js'; + +/** Default project to run for this benchmark if not specified */ +export const defaultProject = 'render-default'; + +/** + * @param {URL} projectDir + * @param {URL} outputFile + */ +export async function run(projectDir, outputFile) { + const root = fileURLToPath(projectDir); + + console.log('Benchmarking `astro --help`...'); + const helpStat = await benchmarkCommand(`node ${astroBin} --help`, root); + console.log('Done'); + + console.log('Benchmarking `astro info`...'); + const infoStat = await benchmarkCommand(`node ${astroBin} info`, root); + console.log('Done'); + + console.log('Result preview:'); + console.log('='.repeat(10)); + console.log(`#### CLI Startup\n\n`); + console.log( + printResult({ + 'astro --help': helpStat, + 'astro info': infoStat, + }) + ); + console.log('='.repeat(10)); +} + +/** + * @param {string} command + * @param {string} root + * @returns {Promise<import('./_util.js').Stat>} + */ +async function benchmarkCommand(command, root) { + /** @type {number[]} */ + const durations = []; + + for (let i = 0; i < 10; i++) { + const start = performance.now(); + await execaCommand(command, { cwd: root }); + durations.push(performance.now() - start); + } + + // From the 10 durations, calculate average, standard deviation, and max value + return calculateStat(durations); +} + +/** + * @param {Record<string, import('./_util.js').Stat>} result + */ +function printResult(result) { + return markdownTable( + [ + ['Command', 'Avg (ms)', 'Stdev (ms)', 'Max (ms)'], + ...Object.entries(result).map(([command, { avg, stdev, max }]) => [ + command, + avg.toFixed(2), + stdev.toFixed(2), + max.toFixed(2), + ]), + ], + { + align: ['l', 'r', 'r', 'r'], + } + ); +} |