diff options
author | 2022-02-18 15:23:47 -0500 | |
---|---|---|
committer | 2022-02-18 14:23:47 -0600 | |
commit | 23783648b61b43f342c3c2c5c0d90996419c936c (patch) | |
tree | 96dc4ed62db92b79b706242676737efc5a0bb7d4 /scripts | |
parent | 9bdceeef28528f482c98ac369c27ea233051892a (diff) | |
download | astro-23783648b61b43f342c3c2c5c0d90996419c936c.tar.gz astro-23783648b61b43f342c3c2c5c0d90996419c936c.tar.zst astro-23783648b61b43f342c3c2c5c0d90996419c936c.zip |
Adds memory leak test in CI (#2616)
* Add a memory leak test
* Add memory leak smoke test to CI
* Update the latest compiler
* Update yarn.lock
* Remove entries optimization
* Calculate as 5% higher
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/memory/index.js | 81 | ||||
-rw-r--r-- | scripts/memory/mk.js | 11 | ||||
-rw-r--r-- | scripts/memory/project/.gitkeep | 0 | ||||
-rw-r--r-- | scripts/memory/project/src/pages/.gitkeep | 0 |
4 files changed, 92 insertions, 0 deletions
diff --git a/scripts/memory/index.js b/scripts/memory/index.js new file mode 100644 index 000000000..8beda6e44 --- /dev/null +++ b/scripts/memory/index.js @@ -0,0 +1,81 @@ +import { execa } from 'execa'; +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'; + +/** URL directory containing the entire project. */ +const projDir = new URL('./project/', import.meta.url); + +function mean(numbers) { + var total = 0, i; + for (i = 0; i < numbers.length; i += 1) { + total += numbers[i]; + } + return total / numbers.length; +} + +function median(numbers) { + // median of [3, 5, 4, 4, 1, 1, 2, 3] = 3 + var median = 0, numsLen = numbers.length; + numbers.sort(); + + if ( + numsLen % 2 === 0 // is even + ) { + // average of two middle numbers + median = (numbers[numsLen / 2 - 1] + numbers[numsLen / 2]) / 2; + } else { // is odd + // middle number only + median = numbers[(numsLen - 1) / 2]; + } + + return median; +} + +let config = await loadConfig({ + cwd: fileURLToPath(projDir) +}); + +config.buildOptions.experimentalStaticBuild = true; + +const server = await dev(config, { logging: 'error'}); + +// Prime the server so initial memory is created +await fetch(`http://localhost:3000/page-0`); + +const sizes = []; + +function addSize() { + sizes.push(v8.getHeapStatistics().total_heap_size); +} + +async function run() { + addSize(); + for(let i = 0; i < 100; i++) { + let path = `/page-${i}`; + await fetch(`http://localhost:3000${path}`); + } + addSize(); +} + +for(let i = 0; i < 100; i++) { + await run(); +} + +let lastThirthy = sizes.slice(sizes.length - 30); +let averageOfLastThirty = mean(lastThirthy); +let medianOfAll = median(sizes); + +// If the trailing average is higher than the median, see if it's more than 5% higher +if(averageOfLastThirty > medianOfAll) { + let percentage = Math.abs(averageOfLastThirty - medianOfAll) / medianOfAll; + if(percentage > .05) { + throw new Error(`The average towards the end (${prettyBytes(averageOfLastThirty)}) is more than 5% higher than the median of all runs (${prettyBytes(medianOfAll)}). This tells us that memory continues to grow and a leak is likely.`) + } +} + + + +await server.stop(); diff --git a/scripts/memory/mk.js b/scripts/memory/mk.js new file mode 100644 index 000000000..6e6e74637 --- /dev/null +++ b/scripts/memory/mk.js @@ -0,0 +1,11 @@ +import fs from 'fs'; + +const pages = new URL('./project/src/pages/', import.meta.url); + +for(let i = 0; i < 100; i++) { + let content = `--- +const i = ${i}; +--- +<span>{i}</span>`; + await fs.promises.writeFile(new URL(`./page-${i}.astro`, pages), content, 'utf-8'); +} diff --git a/scripts/memory/project/.gitkeep b/scripts/memory/project/.gitkeep new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/scripts/memory/project/.gitkeep diff --git a/scripts/memory/project/src/pages/.gitkeep b/scripts/memory/project/src/pages/.gitkeep new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/scripts/memory/project/src/pages/.gitkeep |