Skip to content

Commit 1bbcdf9

Browse files
joyeecheungaduh95
authored andcommitted
benchmark: add vm.SourceTextModule benchmark
PR-URL: #59396 Refs: https://chromium-review.googlesource.com/c/v8/v8/+/6804466 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 21bcd0e commit 1bbcdf9

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

benchmark/esm/import-esm-reload.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const tmpdir = require('../../test/common/tmpdir');
6+
const assert = require('assert');
7+
const { pathToFileURL } = require('url');
8+
const bench = common.createBenchmark(main, {
9+
count: [1, 100],
10+
n: [1000],
11+
});
12+
13+
function prepare(count) {
14+
tmpdir.refresh();
15+
const dir = tmpdir.resolve('modules');
16+
fs.mkdirSync(dir, { recursive: true });
17+
let modSource = '';
18+
for (let i = 0; i < count; ++i) {
19+
modSource += `export const value${i} = 1;\n`;
20+
}
21+
const script = tmpdir.resolve('mod.js');
22+
fs.writeFileSync(script, modSource, 'utf8');
23+
return script;
24+
}
25+
26+
async function main({ n, count }) {
27+
const script = prepare(count);
28+
const url = pathToFileURL(script).href;
29+
let result = 0;
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
const mod = await import(`${url}?t=${i}`);
33+
result += mod[`value${count - 1}`];
34+
}
35+
bench.end(n);
36+
assert.strictEqual(result, n);
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const vm = require('vm');
4+
const common = require('../common.js');
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
stage: ['all', 'instantiate', 'evaluate'],
9+
n: [1000],
10+
}, {
11+
flags: ['--experimental-vm-modules'],
12+
});
13+
14+
function main({ stage, n }) {
15+
const arr = [new vm.SourceTextModule(`
16+
export const value = 42;
17+
`)];
18+
19+
if (stage === 'all') {
20+
bench.start();
21+
}
22+
23+
for (let i = 0; i < n; i++) {
24+
const m = new vm.SourceTextModule(`
25+
export { value } from 'mod${i}';
26+
`);
27+
arr.push(m);
28+
m.linkRequests([arr[i]]);
29+
}
30+
31+
if (stage === 'instantiate') {
32+
bench.start();
33+
}
34+
arr[n].instantiate();
35+
if (stage === 'instantiate') {
36+
bench.end(n);
37+
}
38+
39+
if (stage === 'evaluate') {
40+
bench.start();
41+
}
42+
arr[n].evaluate();
43+
if (stage === 'evaluate' || stage === 'all') {
44+
bench.end(n);
45+
}
46+
47+
assert.strictEqual(arr[n].namespace.value, 42);
48+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
const vm = require('vm');
4+
const common = require('../common.js');
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
stage: ['all', 'link', 'instantiate', 'evaluate'],
9+
n: [1000],
10+
}, {
11+
flags: ['--experimental-vm-modules'],
12+
});
13+
14+
function main({ stage, n }) {
15+
const arr = [];
16+
let importSource = '';
17+
let useSource = 'export const result = 0 ';
18+
for (let i = 0; i < n; i++) {
19+
importSource += `import { value${i} } from 'mod${i}';\n`;
20+
useSource += ` + value${i}\n`;
21+
}
22+
23+
if (stage === 'all') {
24+
bench.start();
25+
}
26+
for (let i = 0; i < n; i++) {
27+
const m = new vm.SourceTextModule(`
28+
export const value${i} = 1;
29+
`);
30+
arr.push(m);
31+
}
32+
33+
const root = new vm.SourceTextModule(`
34+
${importSource}
35+
${useSource};
36+
`);
37+
38+
if (stage === 'link') {
39+
bench.start();
40+
}
41+
42+
root.linkRequests(arr);
43+
for (let i = 0; i < n; i++) {
44+
arr[i].linkRequests([]);
45+
}
46+
47+
if (stage === 'link') {
48+
bench.end(n);
49+
}
50+
51+
if (stage === 'instantiate') {
52+
bench.start();
53+
}
54+
root.instantiate();
55+
if (stage === 'instantiate') {
56+
bench.end(n);
57+
}
58+
59+
if (stage === 'evaluate') {
60+
bench.start();
61+
}
62+
root.evaluate();
63+
if (stage === 'evaluate' || stage === 'all') {
64+
bench.end(n);
65+
}
66+
67+
assert.strictEqual(root.namespace.result, n);
68+
}

0 commit comments

Comments
 (0)