node/test/parallel/test-vm-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

37 lines
1013 B
JavaScript

'use strict';
const common = require('../common');
const {
assertSummaryShape,
assertSingleDetailedShape,
expectExperimentalWarning
} = require('../common/measure-memory');
const assert = require('assert');
const vm = require('vm');
expectExperimentalWarning();
// Test eager memory measurement
{
vm.measureMemory({ execution: 'eager' })
.then(common.mustCall(assertSummaryShape));
vm.measureMemory({ mode: 'detailed', execution: 'eager' })
.then(common.mustCall(assertSingleDetailedShape));
vm.measureMemory({ mode: 'summary', execution: 'eager' })
.then(common.mustCall(assertSummaryShape));
assert.throws(() => vm.measureMemory(null), {
code: 'ERR_INVALID_ARG_TYPE'
});
assert.throws(() => vm.measureMemory('summary'), {
code: 'ERR_INVALID_ARG_TYPE'
});
assert.throws(() => vm.measureMemory({ mode: 'random' }), {
code: 'ERR_INVALID_ARG_VALUE'
});
assert.throws(() => vm.measureMemory({ execution: 'random' }), {
code: 'ERR_INVALID_ARG_VALUE'
});
}