2023-07-17 19:17:59 -05:00
|
|
|
import { fileURLToPath } from 'node:url';
|
2024-08-28 09:52:49 -05:00
|
|
|
import { exec } from 'tinyexec';
|
2023-07-10 10:43:01 -05:00
|
|
|
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`...');
|
2024-08-28 09:52:49 -05:00
|
|
|
const helpStat = await benchmarkCommand('node', [astroBin, '--help'], root);
|
2023-07-10 10:43:01 -05:00
|
|
|
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);
|
2023-07-10 10:43:01 -05:00
|
|
|
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
|
2024-08-28 09:52:49 -05:00
|
|
|
* @param {string[]} args
|
2023-07-10 10:43:01 -05:00
|
|
|
* @param {string} root
|
|
|
|
* @returns {Promise<import('./_util.js').Stat>}
|
|
|
|
*/
|
2024-08-28 09:52:49 -05:00
|
|
|
async function benchmarkCommand(command, args, root) {
|
2023-07-10 10:43:01 -05:00
|
|
|
/** @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 });
|
2023-07-10 10:43:01 -05:00
|
|
|
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'],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|