0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/benchmark/bench/cli-startup.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
1.9 KiB
JavaScript
Raw Normal View History

import { fileURLToPath } from 'node:url';
import { markdownTable } from 'markdown-table';
2024-11-06 07:33:14 -05:00
import { exec } from 'tinyexec';
import { astroBin, calculateStat } from './_util.js';
/** Default project to run for this benchmark if not specified */
export const defaultProject = 'render-default';
/**
* @param {URL} projectDir
*/
2024-11-06 07:33:14 -05:00
export async function run(projectDir) {
const root = fileURLToPath(projectDir);
console.log('Benchmarking `astro --help`...');
2024-08-28 09:52:49 -05:00
const helpStat = await benchmarkCommand('node', [astroBin, '--help'], root);
console.log('Done');
2024-08-28 09:52:49 -05:00
console.log('Benchmarking `astro preferences list`...');
const infoStat = await benchmarkCommand('node', [astroBin, 'preferences', 'list'], 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,
2024-11-06 07:33:14 -05:00
}),
);
console.log('='.repeat(10));
}
/**
* @param {string} command
2024-08-28 09:52:49 -05:00
* @param {string[]} args
* @param {string} root
* @returns {Promise<import('./_util.js').Stat>}
*/
2024-08-28 09:52:49 -05:00
async function benchmarkCommand(command, args, root) {
/** @type {number[]} */
const durations = [];
for (let i = 0; i < 10; i++) {
const start = performance.now();
2024-11-04 09:49:33 -05:00
await exec(command, args, { nodeOptions: { cwd: root }, throwOnError: true });
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'],
2024-11-06 07:33:14 -05:00
},
);
}