benchmark: add vm.SourceTextModule benchmark

PR-URL: https://github.com/nodejs/node/pull/59396
Refs: https://chromium-review.googlesource.com/c/v8/v8/+/6804466
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Joyee Cheung 2025-10-10 17:42:05 +02:00 committed by Node.js GitHub Bot
parent 7c7f30ed17
commit 8973cc4516
3 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,37 @@
'use strict';
const common = require('../common');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
const assert = require('assert');
const { pathToFileURL } = require('url');
const bench = common.createBenchmark(main, {
count: [1, 100],
n: [1000],
});
function prepare(count) {
tmpdir.refresh();
const dir = tmpdir.resolve('modules');
fs.mkdirSync(dir, { recursive: true });
let modSource = '';
for (let i = 0; i < count; ++i) {
modSource += `export const value${i} = 1;\n`;
}
const script = tmpdir.resolve('mod.js');
fs.writeFileSync(script, modSource, 'utf8');
return script;
}
async function main({ n, count }) {
const script = prepare(count);
const url = pathToFileURL(script).href;
let result = 0;
bench.start();
for (let i = 0; i < n; i++) {
const mod = await import(`${url}?t=${i}`);
result += mod[`value${count - 1}`];
}
bench.end(n);
assert.strictEqual(result, n);
}

View File

@ -0,0 +1,48 @@
'use strict';
const vm = require('vm');
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
stage: ['all', 'instantiate', 'evaluate'],
n: [1000],
}, {
flags: ['--experimental-vm-modules'],
});
function main({ stage, n }) {
const arr = [new vm.SourceTextModule(`
export const value = 42;
`)];
if (stage === 'all') {
bench.start();
}
for (let i = 0; i < n; i++) {
const m = new vm.SourceTextModule(`
export { value } from 'mod${i}';
`);
arr.push(m);
m.linkRequests([arr[i]]);
}
if (stage === 'instantiate') {
bench.start();
}
arr[n].instantiate();
if (stage === 'instantiate') {
bench.end(n);
}
if (stage === 'evaluate') {
bench.start();
}
arr[n].evaluate();
if (stage === 'evaluate' || stage === 'all') {
bench.end(n);
}
assert.strictEqual(arr[n].namespace.value, 42);
}

View File

@ -0,0 +1,68 @@
'use strict';
const vm = require('vm');
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
stage: ['all', 'link', 'instantiate', 'evaluate'],
n: [1000],
}, {
flags: ['--experimental-vm-modules'],
});
function main({ stage, n }) {
const arr = [];
let importSource = '';
let useSource = 'export const result = 0 ';
for (let i = 0; i < n; i++) {
importSource += `import { value${i} } from 'mod${i}';\n`;
useSource += ` + value${i}\n`;
}
if (stage === 'all') {
bench.start();
}
for (let i = 0; i < n; i++) {
const m = new vm.SourceTextModule(`
export const value${i} = 1;
`);
arr.push(m);
}
const root = new vm.SourceTextModule(`
${importSource}
${useSource};
`);
if (stage === 'link') {
bench.start();
}
root.linkRequests(arr);
for (let i = 0; i < n; i++) {
arr[i].linkRequests([]);
}
if (stage === 'link') {
bench.end(n);
}
if (stage === 'instantiate') {
bench.start();
}
root.instantiate();
if (stage === 'instantiate') {
bench.end(n);
}
if (stage === 'evaluate') {
bench.start();
}
root.evaluate();
if (stage === 'evaluate' || stage === 'all') {
bench.end(n);
}
assert.strictEqual(root.namespace.result, n);
}