Repository navigation
Expand file tree
/
Copy pathstartup-esm-graph.js
More file actions
66 lines (59 loc) Β· 2.14 KB
/
Copy pathstartup-esm-graph.js
File metadata and controls
66 lines (59 loc) Β· 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
const common = require('../common.js');
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const tmpdir = require('../../test/common/tmpdir');
const BRANCHING_FACTOR = 10;
const benchmarkDirectory = tmpdir.resolve('esm-graph');
// Each measured iteration has to load a graph the ESM cache has never seen, so the
// fixture holds one independent copy of the graph per iteration.
function graphDirectory(modules, copy) {
return path.join(benchmarkDirectory, `g${modules}-${copy}`);
}
function entryURL(modules, copy) {
return pathToFileURL(path.join(graphDirectory(modules, copy), 'mod0.mjs')).href;
}
// Build a complete BRANCHING_FACTOR-ary tree of `modules` + 1 modules rooted at mod0:
// module i imports modules BRANCHING_FACTOR*i+1 through BRANCHING_FACTOR*i+BRANCHING_FACTOR,
// capped at the total, so the shape approximates a real dependency tree rather than
// one module with hundreds of direct imports.
function createGraph(modules, copy) {
const dir = graphDirectory(modules, copy);
fs.mkdirSync(dir, { recursive: true });
const total = modules + 1;
for (let i = 0; i < total; i++) {
let source = '';
for (let c = 1; c <= BRANCHING_FACTOR; c++) {
const child = BRANCHING_FACTOR * i + c;
if (child < total) {
source += `import './mod${child}.mjs';\n`;
}
}
source += `export const value${i} = ${i};\n`;
fs.writeFileSync(path.join(dir, `mod${i}.mjs`), source);
}
}
const bench = common.createBenchmark(main, {
modules: [250, 500, 1000, 2000],
n: [10],
}, {
setup(configs) {
// Build every fixture once here rather than per configuration: writing tens of
// thousands of files is far more expensive than the work being measured.
tmpdir.refresh();
const maxN = configs.reduce((max, c) => Math.max(max, c.n), 0);
for (const modules of new Set(configs.map((c) => c.modules))) {
for (let copy = 0; copy < maxN; copy++) {
createGraph(modules, copy);
}
}
},
});
async function main({ n, modules }) {
bench.start();
for (let i = 0; i < n; i++) {
await import(entryURL(modules, i));
}
bench.end(n);
}