index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import mri from 'mri';
  2. import fs from 'node:fs/promises';
  3. import path from 'node:path';
  4. import { pathToFileURL } from 'node:url';
  5. const args = mri(process.argv.slice(2));
  6. if (args.help || args.h) {
  7. console.log(`\
  8. astro-benchmark <command> [options]
  9. Command
  10. [empty] Run all benchmarks
  11. memory Run build memory and speed test
  12. render Run rendering speed test
  13. server-stress Run server stress test
  14. cli-startup Run CLI startup speed test
  15. Options
  16. --project <project-name> Project to use for benchmark, see benchmark/make-project/ for available names
  17. --output <output-file> Output file to write results to
  18. `);
  19. process.exit(0);
  20. }
  21. const commandName = args._[0];
  22. const benchmarks = {
  23. memory: () => import('./bench/memory.js'),
  24. render: () => import('./bench/render.js'),
  25. 'server-stress': () => import('./bench/server-stress.js'),
  26. 'cli-startup': () => import('./bench/cli-startup.js'),
  27. };
  28. if (commandName && !(commandName in benchmarks)) {
  29. console.error(`Invalid benchmark name: ${commandName}`);
  30. process.exit(1);
  31. }
  32. if (commandName) {
  33. // Run single benchmark
  34. const bench = benchmarks[commandName];
  35. const benchMod = await bench();
  36. const projectDir = await makeProject(args.project || benchMod.defaultProject);
  37. const outputFile = await getOutputFile(commandName);
  38. await benchMod.run(projectDir, outputFile);
  39. } else {
  40. // Run all benchmarks
  41. for (const name in benchmarks) {
  42. const bench = benchmarks[name];
  43. const benchMod = await bench();
  44. const projectDir = await makeProject(args.project || benchMod.defaultProject);
  45. const outputFile = await getOutputFile(name);
  46. await benchMod.run(projectDir, outputFile);
  47. }
  48. }
  49. async function makeProject(name) {
  50. console.log('Making project:', name);
  51. const projectDir = new URL(`./projects/${name}/`, import.meta.url);
  52. const makeProjectMod = await import(`./make-project/${name}.js`);
  53. await makeProjectMod.run(projectDir);
  54. console.log('Finished making project:', name);
  55. return projectDir;
  56. }
  57. /**
  58. * @param {string} benchmarkName
  59. */
  60. async function getOutputFile(benchmarkName) {
  61. let file;
  62. if (args.output) {
  63. file = pathToFileURL(path.resolve(args.output));
  64. } else {
  65. file = new URL(`./results/${benchmarkName}-bench-${Date.now()}.json`, import.meta.url);
  66. }
  67. // Prepare output file directory
  68. await fs.mkdir(new URL('./', file), { recursive: true });
  69. return file;
  70. }