mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 00:20:08 +01:00
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:
parent
7c7f30ed17
commit
8973cc4516
37
benchmark/esm/import-esm-reload.js
Normal file
37
benchmark/esm/import-esm-reload.js
Normal 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);
|
||||
}
|
||||
48
benchmark/vm/source-text-module-chained.js
Normal file
48
benchmark/vm/source-text-module-chained.js
Normal 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);
|
||||
}
|
||||
68
benchmark/vm/source-text-module-flat.js
Normal file
68
benchmark/vm/source-text-module-flat.js
Normal 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);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user