diff --git a/test/cli/run/esm-fixture-leak-small.mjs b/test/cli/run/esm-fixture-leak-small.mjs index 71fb83e6fa07..6f6540985b11 100644 --- a/test/cli/run/esm-fixture-leak-small.mjs +++ b/test/cli/run/esm-fixture-leak-small.mjs @@ -9,15 +9,23 @@ const rss = ? Bun.unsafe.memoryFootprint : process.memoryUsage.rss; +// The JS heap and allocator grow by 5-10 MB over the first couple thousand +// loads and then hold steady. Taking the baseline after that leaves only +// per-load growth inside the measured window, which is what lets the window be +// 40k loads instead of the 100k this fixture used to need (20-27s on the 4 vCPU +// CI agents, against the test's 30s budget). +const warmupLoads = 2_000; +const measuredLoads = 40_000; + if (typeof Bun !== "undefined") Bun.gc(true); -for (let i = 0; i < 5; i++) { +for (let i = 0; i < warmupLoads; i++) { delete require.cache[dest]; await import(dest); } if (typeof Bun !== "undefined") Bun.gc(true); const baseline = rss(); -for (let i = 0; i < 100000; i++) { +for (let i = 0; i < measuredLoads; i++) { delete require.cache[dest]; await import(dest); } @@ -27,19 +35,10 @@ setTimeout(() => { let diff = rss() - baseline; diff = (diff / 1024 / 1024) | 0; console.log({ leaked: diff + " MB" }); - // This test seems to be more flaky on slow filesystems. - // This used to be 40 MB, but the original version of Bun which this triggered on would reach 120 MB - // so we can increase it to 100 and still catch the leak. - // - // ❯ bunx bun@1.0.0 --smol test/cli/run/esm-fixture-leak-small.mjs - // { - // leaked: "100 MB" - // } - // ❯ bunx bun@1.1.0 --smol test/cli/run/esm-fixture-leak-small.mjs - // { - // leaked: "38 MB", - // } - if (diff >= (isASAN ? 500 : 100)) { + // The leak this guards against retained about 1 KB per load (bun 1.0.0 + // measured 100-120 MB over 100k loads), so 40k loads of it are 40 MB or more. + // A non-leaking release build measures 0-7 MB here (20 runs, Linux x64). + if (diff >= (isASAN ? 500 : 20)) { console.log("\n--fail--\n"); process.exit(1); } else {