Skip to content

Commit 09d6dfb

Browse files
BridgeARBethGriggs
authored andcommitted
benchmark: add new module loading benchmarks
PR-URL: #26970 Refs: #25362 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent 32ec034 commit 09d6dfb

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
const fs = require('fs');
3+
const path = require('path');
4+
const common = require('../common.js');
5+
6+
const tmpdir = require('../../test/common/tmpdir');
7+
const benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');
8+
9+
const bench = common.createBenchmark(main, {
10+
ext: ['', '.js'],
11+
files: [1e3],
12+
cache: ['true', 'false']
13+
});
14+
15+
function main({ ext, cache, files }) {
16+
tmpdir.refresh();
17+
fs.mkdirSync(benchmarkDirectory);
18+
fs.writeFileSync(
19+
`${benchmarkDirectory}/a.js`,
20+
'module.exports = {};'
21+
);
22+
for (var i = 0; i <= files; i++) {
23+
fs.mkdirSync(`${benchmarkDirectory}/${i}`);
24+
fs.writeFileSync(
25+
`${benchmarkDirectory}/${i}/package.json`,
26+
'{"main": "index.js"}'
27+
);
28+
fs.writeFileSync(
29+
`${benchmarkDirectory}/${i}/index.js`,
30+
`require('../a${ext}');`
31+
);
32+
}
33+
34+
measureDir(cache === 'true', files);
35+
36+
tmpdir.refresh();
37+
}
38+
39+
function measureDir(cache, files) {
40+
var i;
41+
if (cache) {
42+
for (i = 0; i <= files; i++) {
43+
require(`${benchmarkDirectory}/${i}`);
44+
}
45+
}
46+
bench.start();
47+
for (i = 0; i <= files; i++) {
48+
require(`${benchmarkDirectory}/${i}`);
49+
}
50+
bench.end(files);
51+
}

0 commit comments

Comments
 (0)