2022-02-18 20:23:47 +00:00
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import v8 from 'v8';
|
|
|
|
import dev from '../../packages/astro/dist/core/dev/index.js';
|
|
|
|
import { loadConfig } from '../../packages/astro/dist/core/config.js';
|
|
|
|
import prettyBytes from 'pretty-bytes';
|
|
|
|
|
2022-02-19 01:08:28 +00:00
|
|
|
if (!global.gc) {
|
|
|
|
console.error('ERROR: Node must be run with --expose-gc');
|
|
|
|
process.exit(1);
|
2022-02-18 20:23:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 01:08:28 +00:00
|
|
|
const isCI = process.argv.includes('--ci');
|
2022-02-18 20:23:47 +00:00
|
|
|
|
2022-02-19 01:08:28 +00:00
|
|
|
/** URL directory containing the entire project. */
|
|
|
|
const projDir = new URL('./project/', import.meta.url);
|
2022-02-18 20:23:47 +00:00
|
|
|
|
|
|
|
let config = await loadConfig({
|
2022-02-18 20:24:55 +00:00
|
|
|
cwd: fileURLToPath(projDir),
|
2022-02-18 20:23:47 +00:00
|
|
|
});
|
|
|
|
|
2022-02-19 01:08:28 +00:00
|
|
|
const server = await dev(config, { logging: { level: 'error' } });
|
2022-02-18 20:23:47 +00:00
|
|
|
|
|
|
|
// Prime the server so initial memory is created
|
|
|
|
await fetch(`http://localhost:3000/page-0`);
|
|
|
|
|
|
|
|
async function run() {
|
2022-02-18 20:24:55 +00:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2022-02-18 20:23:47 +00:00
|
|
|
let path = `/page-${i}`;
|
|
|
|
await fetch(`http://localhost:3000${path}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 01:08:28 +00:00
|
|
|
global.gc();
|
|
|
|
const startSize = v8.getHeapStatistics().used_heap_size;
|
|
|
|
|
|
|
|
// HUMAN mode: Runs forever. Optimized for accurate results on each snapshot Slower than CI.
|
|
|
|
if (!isCI) {
|
2022-04-02 20:15:41 +00:00
|
|
|
console.log(
|
|
|
|
`Greetings, human. This test will run forever. Run with the "--ci" flag to finish with a result.`
|
|
|
|
);
|
2022-02-19 01:08:28 +00:00
|
|
|
let i = 1;
|
|
|
|
while (i++) {
|
|
|
|
await run();
|
|
|
|
global.gc();
|
|
|
|
const checkpoint = v8.getHeapStatistics().used_heap_size;
|
|
|
|
console.log(`Snapshot ${String(i).padStart(3, '0')}: ${(checkpoint / startSize) * 100}%`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CI mode: Runs 100 times. Optimized for speed with an accurate final result.
|
2022-02-18 20:24:55 +00:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2022-02-18 20:23:47 +00:00
|
|
|
await run();
|
2022-02-19 01:08:28 +00:00
|
|
|
const checkpoint = v8.getHeapStatistics().used_heap_size;
|
|
|
|
console.log(`Estimate ${String(i).padStart(3, '0')}/100: ${(checkpoint / startSize) * 100}%`);
|
2022-02-18 20:23:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 01:08:28 +00:00
|
|
|
console.log(`Test complete. Running final garbage collection...`);
|
|
|
|
global.gc();
|
|
|
|
const endSize = v8.getHeapStatistics().used_heap_size;
|
2022-02-18 20:23:47 +00:00
|
|
|
|
|
|
|
// If the trailing average is higher than the median, see if it's more than 5% higher
|
2022-02-19 01:08:28 +00:00
|
|
|
let percentage = endSize / startSize;
|
2022-03-18 22:35:45 +00:00
|
|
|
const TEST_THRESHOLD = 1.5;
|
2022-02-19 01:08:28 +00:00
|
|
|
const isPass = percentage < TEST_THRESHOLD;
|
|
|
|
console.log(``);
|
|
|
|
console.log(`Result: ${isPass ? 'PASS' : 'FAIL'} (${percentage * 100}%)`);
|
2022-04-02 20:15:41 +00:00
|
|
|
console.log(
|
|
|
|
`Memory usage began at ${prettyBytes(startSize)} and finished at ${prettyBytes(endSize)}.`
|
|
|
|
);
|
2022-02-19 01:08:28 +00:00
|
|
|
console.log(`The threshold for a probable memory leak is ${TEST_THRESHOLD * 100}%`);
|
|
|
|
console.log(``);
|
|
|
|
console.log(`Exiting...`);
|
2022-02-18 20:23:47 +00:00
|
|
|
await server.stop();
|
2022-02-19 01:08:28 +00:00
|
|
|
process.exit(isPass ? 0 : 1);
|