Skip to content

Commit 018a6da

Browse files
committed
benchmark: add vfs module-graph cold-load benchmark
Measures loading a large CJS or ESM module graph from a mounted VFS, relying on the unmount cache purge so every iteration is a cold load.
1 parent bdf79b2 commit 018a6da

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

benchmark/vfs/module-graph.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
const path = require('path');
3+
const { pathToFileURL } = require('url');
4+
const common = require('../common.js');
5+
6+
const bench = common.createBenchmark(main, {
7+
type: ['cjs', 'esm'],
8+
files: [1e2, 1e3],
9+
n: [10],
10+
}, { flags: ['--experimental-vfs', '--no-warnings'] });
11+
12+
// Builds a module graph of `files` packages, each with its own package.json,
13+
// an index requiring a package-local file and a shared root module, and an
14+
// entry point that pulls in every package.
15+
function buildGraph(layer, files, type) {
16+
const entryRequires = [];
17+
if (type === 'esm') {
18+
layer.writeFileSync('/package.json', '{"type":"module"}');
19+
}
20+
layer.writeFileSync('/shared.js',
21+
type === 'cjs' ? 'module.exports = 0;' : 'export default 0;');
22+
for (let i = 0; i < files; i++) {
23+
layer.mkdirSync(`/${i}`, { recursive: true });
24+
if (type === 'cjs') {
25+
layer.writeFileSync(`/${i}/package.json`, '{"main":"index.js"}');
26+
layer.writeFileSync(`/${i}/lib.js`, 'module.exports = 1;');
27+
layer.writeFileSync(
28+
`/${i}/index.js`,
29+
'require("./lib.js"); require("../shared.js"); module.exports = __filename;');
30+
entryRequires.push(`require('./${i}/');`);
31+
} else {
32+
layer.writeFileSync(`/${i}/package.json`, '{"type":"module"}');
33+
layer.writeFileSync(`/${i}/lib.js`, 'export default 1;');
34+
layer.writeFileSync(
35+
`/${i}/index.js`,
36+
'import "./lib.js"; import "../shared.js"; export default import.meta.url;');
37+
entryRequires.push(`import './${i}/index.js';`);
38+
}
39+
}
40+
layer.writeFileSync('/entry.js', entryRequires.join('\n'));
41+
}
42+
43+
async function main({ n, type, files }) {
44+
const vfs = require('node:vfs');
45+
const layer = vfs.create();
46+
buildGraph(layer, files, type);
47+
48+
bench.start();
49+
for (let i = 0; i < n; i++) {
50+
const mountPoint = layer.mount();
51+
const entry = path.join(mountPoint, 'entry.js');
52+
if (type === 'cjs') {
53+
require(entry);
54+
} else {
55+
await import(pathToFileURL(entry).href);
56+
}
57+
// Unmounting purges the module caches for the mount prefix, so every
58+
// iteration is a cold load of the full graph.
59+
layer.unmount();
60+
}
61+
bench.end(n * files);
62+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('vfs');

0 commit comments

Comments
 (0)