cli-startup.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { fileURLToPath } from 'node:url';
  2. import { execaCommand } from 'execa';
  3. import { markdownTable } from 'markdown-table';
  4. import { astroBin, calculateStat } from './_util.js';
  5. /** Default project to run for this benchmark if not specified */
  6. export const defaultProject = 'render-default';
  7. /**
  8. * @param {URL} projectDir
  9. * @param {URL} outputFile
  10. */
  11. export async function run(projectDir, outputFile) {
  12. const root = fileURLToPath(projectDir);
  13. console.log('Benchmarking `astro --help`...');
  14. const helpStat = await benchmarkCommand(`node ${astroBin} --help`, root);
  15. console.log('Done');
  16. console.log('Benchmarking `astro info`...');
  17. const infoStat = await benchmarkCommand(`node ${astroBin} info`, root);
  18. console.log('Done');
  19. console.log('Result preview:');
  20. console.log('='.repeat(10));
  21. console.log(`#### CLI Startup\n\n`);
  22. console.log(
  23. printResult({
  24. 'astro --help': helpStat,
  25. 'astro info': infoStat,
  26. })
  27. );
  28. console.log('='.repeat(10));
  29. }
  30. /**
  31. * @param {string} command
  32. * @param {string} root
  33. * @returns {Promise<import('./_util.js').Stat>}
  34. */
  35. async function benchmarkCommand(command, root) {
  36. /** @type {number[]} */
  37. const durations = [];
  38. for (let i = 0; i < 10; i++) {
  39. const start = performance.now();
  40. await execaCommand(command, { cwd: root });
  41. durations.push(performance.now() - start);
  42. }
  43. // From the 10 durations, calculate average, standard deviation, and max value
  44. return calculateStat(durations);
  45. }
  46. /**
  47. * @param {Record<string, import('./_util.js').Stat>} result
  48. */
  49. function printResult(result) {
  50. return markdownTable(
  51. [
  52. ['Command', 'Avg (ms)', 'Stdev (ms)', 'Max (ms)'],
  53. ...Object.entries(result).map(([command, { avg, stdev, max }]) => [
  54. command,
  55. avg.toFixed(2),
  56. stdev.toFixed(2),
  57. max.toFixed(2),
  58. ]),
  59. ],
  60. {
  61. align: ['l', 'r', 'r', 'r'],
  62. }
  63. );
  64. }