node/test/parallel/test-v8-query-objects.js
Joyee Cheung 7f2d61f82a
v8: implement v8.queryObjects() for memory leak regression testing
This is similar to the `queryObjects()` console API provided by the
Chromium DevTools console. It can be used to search for objects that
have the matching constructor on its prototype chain in the entire
heap, which can be useful for memory leak regression tests. To avoid
surprising results, users should avoid using this API on constructors
whose implementation they don't control, or on constructors that can
be invoked by other parties in the application.

To avoid accidental leaks, this API does not return raw references to
the objects found. By default, it returns the count of the objects
found. If `options.format` is `'summary'`, it returns an array
containing brief string representations for each object. The visibility
provided in this API is similar to what the heap snapshot provides,
while users can save the cost of serialization and parsing and directly
filer the target objects during the search.

We have been using this API internally for the test suite, which
has been more stable than any other leak regression testing
strategies in the CI. With a public implementation we can now
use the public API instead.

PR-URL: https://github.com/nodejs/node/pull/51927
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2024-03-02 22:11:30 +00:00

104 lines
3.7 KiB
JavaScript

'use strict';
// This tests the v8.queryObjects() API.
const common = require('../common');
const v8 = require('v8');
const assert = require('assert');
const { inspect } = require('util');
function format(obj) {
return inspect(obj, { depth: 0 });
}
common.expectWarning(
'ExperimentalWarning',
'v8.queryObjects() is an experimental feature and might change at any time',
);
{
for (const invalid of [undefined, 1, null, false, {}, 'foo']) {
assert.throws(() => v8.queryObjects(invalid), { code: 'ERR_INVALID_ARG_TYPE' });
}
for (const invalid of [1, null, false, 'foo']) {
assert.throws(() => v8.queryObjects(() => {}, invalid), { code: 'ERR_INVALID_ARG_TYPE' });
}
assert.throws(() => v8.queryObjects(() => {}, { format: 'invalid' }), { code: 'ERR_INVALID_ARG_VALUE' });
}
{
class TestV8QueryObjectsClass {}
// By default, returns count of objects with the constructor on the prototype.
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsClass), 0);
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsClass, { format: 'count' }), 0);
// 'summary' format returns an array.
assert.deepStrictEqual(v8.queryObjects(TestV8QueryObjectsClass, { format: 'summary' }), []);
// Create an instance and check that it shows up in the results.
const obj = new TestV8QueryObjectsClass();
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsClass), 1);
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsClass, { format: 'count' }), 1);
assert.deepStrictEqual(
v8.queryObjects(TestV8QueryObjectsClass, { format: 'summary' }),
[ format(obj)]
);
}
{
// ES6 class inheritance.
class TestV8QueryObjectsBaseClass {}
class TestV8QueryObjectsChildClass extends TestV8QueryObjectsBaseClass {}
const summary = v8.queryObjects(TestV8QueryObjectsBaseClass, { format: 'summary' });
// TestV8QueryObjectsChildClass's prototype's [[Prototype]] slot is
// TestV8QueryObjectsBaseClass's prototoype so it shows up in the query.
assert.deepStrictEqual(summary, [
format(TestV8QueryObjectsChildClass.prototype),
]);
const obj = new TestV8QueryObjectsChildClass();
assert.deepStrictEqual(
v8.queryObjects(TestV8QueryObjectsBaseClass, { format: 'summary' }).sort(),
[
format(TestV8QueryObjectsChildClass.prototype),
format(obj),
].sort()
);
assert.deepStrictEqual(
v8.queryObjects(TestV8QueryObjectsChildClass, { format: 'summary' }),
[ format(obj) ],
);
}
{
function TestV8QueryObjectsCtor() {}
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsCtor), 0);
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsCtor, { format: 'count' }), 0);
assert.deepStrictEqual(v8.queryObjects(TestV8QueryObjectsCtor, { format: 'summary' }), []);
// Create an instance and check that it shows up in the results.
const obj = new TestV8QueryObjectsCtor();
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsCtor), 1);
assert.strictEqual(v8.queryObjects(TestV8QueryObjectsCtor, { format: 'count' }), 1);
assert.deepStrictEqual(
v8.queryObjects(TestV8QueryObjectsCtor, { format: 'summary' }),
[ format(obj)]
);
}
{
// Classic inheritance.
function TestV8QueryObjectsBaseCtor() {}
function TestV8QueryObjectsChildCtor() {}
Object.setPrototypeOf(TestV8QueryObjectsChildCtor.prototype, TestV8QueryObjectsBaseCtor.prototype);
Object.setPrototypeOf(TestV8QueryObjectsChildCtor, TestV8QueryObjectsBaseCtor);
const summary = v8.queryObjects(TestV8QueryObjectsBaseCtor, { format: 'summary' });
assert.deepStrictEqual(summary, [
format(TestV8QueryObjectsChildCtor.prototype),
]);
const obj = new TestV8QueryObjectsChildCtor();
assert.deepStrictEqual(
v8.queryObjects(TestV8QueryObjectsChildCtor, { format: 'summary' }),
[ format(obj) ],
);
}