Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions test/parallel/test-perf-hooks-histogram-external-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Loading