node/test/embedding/test-embedding.js
Anna Henningsen 06bb6b42b3 src: add snapshot support for embedder API
Add experimental support for loading snapshots in the embedder API
by adding a public opaque wrapper for our `SnapshotData` struct and
allowing embedders to pass it to the relevant setup functions.
Where applicable, use these helpers to deduplicate existing code
in Node.js’s startup path.

This has shown a 40 % startup performance increase for a real-world
application, even with the somewhat limited current support for
built-in modules.

The documentation includes a note about no guarantees for API or
ABI stability for this feature while it is experimental.

PR-URL: https://github.com/nodejs/node/pull/45888
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2023-02-03 20:48:22 +00:00

89 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const child_process = require('child_process');
const path = require('path');
const fs = require('fs');
tmpdir.refresh();
common.allowGlobals(global.require);
common.allowGlobals(global.embedVars);
function resolveBuiltBinary(bin) {
let binary = `out/${common.buildType}/${bin}`;
if (common.isWindows) {
binary += '.exe';
}
return path.resolve(__dirname, '..', '..', binary);
}
const binary = resolveBuiltBinary('embedtest');
const standaloneNodeBinary = resolveBuiltBinary('node');
assert.strictEqual(
child_process.spawnSync(binary, ['console.log(42)'])
.stdout.toString().trim(),
'42');
assert.strictEqual(
child_process.spawnSync(binary, ['console.log(embedVars.nön_ascıı)'])
.stdout.toString().trim(),
'🏳️‍🌈');
assert.strictEqual(
child_process.spawnSync(binary, ['console.log(42)'])
.stdout.toString().trim(),
'42');
assert.strictEqual(
child_process.spawnSync(binary, ['throw new Error()']).status,
1);
assert.strictEqual(
child_process.spawnSync(binary, ['process.exitCode = 8']).status,
8);
const fixturePath = JSON.stringify(fixtures.path('exit.js'));
assert.strictEqual(
child_process.spawnSync(binary, [`require(${fixturePath})`, 92]).status,
92);
// Basic snapshot support
{
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
const blobPath = path.join(tmpdir.path, 'embedder-snapshot.blob');
const buildSnapshotArgs = [snapshotFixture, 'arg1', 'arg2'];
const runEmbeddedArgs = ['--embedder-snapshot-blob', blobPath, 'arg3', 'arg4'];
fs.rmSync(blobPath, { force: true });
assert.strictEqual(child_process.spawnSync(standaloneNodeBinary, [
'--snapshot-blob', blobPath, '--build-snapshot', ...buildSnapshotArgs,
], {
cwd: tmpdir.path,
}).status, 0);
const spawnResult = child_process.spawnSync(binary, ['--', ...runEmbeddedArgs]);
assert.deepStrictEqual(JSON.parse(spawnResult.stdout), {
originalArgv: [standaloneNodeBinary, ...buildSnapshotArgs],
currentArgv: [binary, ...runEmbeddedArgs],
});
}
// Create workers and vm contexts after deserialization
{
const snapshotFixture = fixtures.path('snapshot', 'create-worker-and-vm.js');
const blobPath = path.join(tmpdir.path, 'embedder-snapshot.blob');
fs.rmSync(blobPath, { force: true });
assert.strictEqual(child_process.spawnSync(standaloneNodeBinary, [
'--snapshot-blob', blobPath, '--build-snapshot', snapshotFixture,
], {
cwd: tmpdir.path,
}).status, 0);
assert.strictEqual(
child_process.spawnSync(binary, ['--', '--embedder-snapshot-blob', blobPath]).status,
0);
}