memory.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { execaCommand } from 'execa';
  2. import { markdownTable } from 'markdown-table';
  3. import fs from 'node:fs/promises';
  4. import { fileURLToPath } from 'node:url';
  5. import { astroBin } from './_util.js';
  6. /** @typedef {Record<string, import('../../packages/astro/src/core/config/timer').Stat>} AstroTimerStat */
  7. /** Default project to run for this benchmark if not specified */
  8. export const defaultProject = 'memory-default';
  9. /**
  10. * @param {URL} projectDir
  11. * @param {URL} outputFile
  12. */
  13. export async function run(projectDir, outputFile) {
  14. const root = fileURLToPath(projectDir);
  15. const outputFilePath = fileURLToPath(outputFile);
  16. console.log('Building and benchmarking...');
  17. await execaCommand(`node --expose-gc --max_old_space_size=256 ${astroBin} build`, {
  18. cwd: root,
  19. stdio: 'inherit',
  20. env: {
  21. ASTRO_TIMER_PATH: outputFilePath,
  22. },
  23. });
  24. console.log('Raw results written to', outputFilePath);
  25. console.log('Result preview:');
  26. console.log('='.repeat(10));
  27. console.log(`#### Memory\n\n`);
  28. console.log(printResult(JSON.parse(await fs.readFile(outputFilePath, 'utf-8'))));
  29. console.log('='.repeat(10));
  30. console.log('Done!');
  31. }
  32. /**
  33. * @param {AstroTimerStat} output
  34. */
  35. function printResult(output) {
  36. return markdownTable(
  37. [
  38. ['', 'Elapsed time (s)', 'Memory used (MB)', 'Final memory (MB)'],
  39. ...Object.entries(output).map(([name, stat]) => [
  40. name,
  41. (stat.elapsedTime / 1000).toFixed(2),
  42. (stat.heapUsedChange / 1024 / 1024).toFixed(2),
  43. (stat.heapUsedTotal / 1024 / 1024).toFixed(2),
  44. ]),
  45. ],
  46. {
  47. align: ['l', 'r', 'r', 'r'],
  48. }
  49. );
  50. }