0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/common/measure-memory.js
Joyee Cheung 5f2c4ce74f
vm: fix vm.measureMemory() and introduce execution option
https://github.com/nodejs/node-v8/pull/147 broke the
`vm.measureMemory()` API. It only created a `MeasureMemoryDelegate` and
without actually calling `v8::Isolate::MeasureMemory()` so the returned
promise will never resolve. This was not caught by the tests because
the promise resolvers were not wrapped with `common.mustCall()`.

This patch migrates the API properly and also introduce the newly
added execution option to the API. It also removes support for
specifying contexts to measure - instead we'll just return the
measurements for all contexts in the detailed mode, which is
what the `performance.measureMemory()` prototype in V8 currently does.
We can consider implementing our own `v8::MeasureMemoryDelegate`
to select the target context in `ShouldMeasure()` in the future,
but then we'll also need to implement `MeasurementComplete()`
to assemble the result. For now it's probably too early to do that.

Since this API is still experimental (and guarded with a warning),
such breakage should be acceptable.

Refs: https://github.com/nodejs/node-v8/pull/147

PR-URL: https://github.com/nodejs/node/pull/32988
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-04-30 17:42:12 +08:00

58 lines
1.8 KiB
JavaScript

/* eslint-disable node-core/require-common-first, node-core/required-modules */
'use strict';
const assert = require('assert');
const common = require('./');
// The formats could change when V8 is updated, then the tests should be
// updated accordingly.
function assertResultShape(result) {
assert.strictEqual(typeof result.jsMemoryEstimate, 'number');
assert.strictEqual(typeof result.jsMemoryRange[0], 'number');
assert.strictEqual(typeof result.jsMemoryRange[1], 'number');
}
function assertSummaryShape(result) {
assert.strictEqual(typeof result, 'object');
assert.strictEqual(typeof result.total, 'object');
assertResultShape(result.total);
}
function assertDetailedShape(result, contexts = 0) {
assert.strictEqual(typeof result, 'object');
assert.strictEqual(typeof result.total, 'object');
assert.strictEqual(typeof result.current, 'object');
assertResultShape(result.total);
assertResultShape(result.current);
if (contexts === 0) {
assert.deepStrictEqual(result.other, []);
} else {
assert.strictEqual(result.other.length, contexts);
for (const item of result.other) {
assertResultShape(item);
}
}
}
function assertSingleDetailedShape(result) {
assert.strictEqual(typeof result, 'object');
assert.strictEqual(typeof result.total, 'object');
assert.strictEqual(typeof result.current, 'object');
assert.deepStrictEqual(result.other, []);
assertResultShape(result.total);
assertResultShape(result.current);
}
function expectExperimentalWarning() {
common.expectWarning('ExperimentalWarning',
'vm.measureMemory is an experimental feature. ' +
'This feature could change at any time');
}
module.exports = {
assertSummaryShape,
assertDetailedShape,
assertSingleDetailedShape,
expectExperimentalWarning
};