test: make histogram external memory test less overfitting

The test assumes too much about V8 GC's internal mechanisms and
thresholds that is no longer true on newer V8 versions and break
future V8 updates. Update it to only detect the occurence of
major GC to make it more future-proof and unblock V8 updates.

Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66416
Refs: https://github.com/nodejs/node/pull/65161
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Richard Lau <richard.lau@ibm.com>
This commit is contained in:
Joyee Cheung authored and Antoine du Hamel committed 2026-10-03 17:30:24 +02:00
1 parent 2c06236669
commit 7b25cb811a
1 file changed
+9 -9
@@ -9,30 +9,30 @@ const assert = require('assert');
const { setImmediate: setImmediatePromise } = require('timers/promises');
const {
PerformanceObserver,
constants: { NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY },
constants: { NODE_PERFORMANCE_GC_MAJOR },
createHistogram,
} = require('perf_hooks');
let externalMemoryGCs = 0;
let majorGCs = 0;
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.detail.flags & NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY)
externalMemoryGCs++;
if (entry.detail.kind === NODE_PERFORMANCE_GC_MAJOR) majorGCs++;
}
});
observer.observe({ entryTypes: ['gc'] });
// With the default options, a histogram holds 45,056 64-bit counts, about
// 352 KiB. V8 starts a garbage collection once external memory has grown by
// 64 MiB, so 400 unreferenced snapshots (about 137 MiB) must trigger one.
// 352 KiB, while its JavaScript wrapper is small. It should only
// triggers a major GC if that memory is reported. The GC trigger and finalization
// can vary depending on V8 internals, so only check the occurrence of a major GC here.
const histogram = createHistogram();
for (let i = 0; i < 400; i++) histogram.snapshot();
(async () => {
// Performance entries for garbage collections are delivered asynchronously.
for (let i = 0; i < 10 && externalMemoryGCs === 0; i++)
for (let i = 0; i < 10 && majorGCs === 0; i++)
await setImmediatePromise();
observer.disconnect();
assert.ok(externalMemoryGCs > 0,
'Expected a garbage collection caused by external memory');
assert.ok(majorGCs > 0,
'Expected a major garbage collection caused by histogram memory');
})().then(common.mustCall());