Skip to content

Commit e9bffa8

Browse files
BridgeARBethGriggs
authored andcommitted
benchmark: improve module-loader benchmark
Add more benchmark options to properly verify the gains. This makes sure the benchmark also tests requiring the same module again instead of only loading each module only once. 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 09d6dfb commit e9bffa8

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

benchmark/module/module-loader.js

+28-32
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
'use strict';
22
const fs = require('fs');
33
const path = require('path');
4+
const { builtinModules } = require('module');
45
const common = require('../common.js');
56

67
const tmpdir = require('../../test/common/tmpdir');
7-
const benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');
8+
let benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');
9+
10+
// Filter all irregular modules.
11+
const otherModules = builtinModules.filter((name) => !/\/|^_|^sys/.test(name));
812

913
const bench = common.createBenchmark(main, {
10-
n: [5e4],
11-
fullPath: ['true', 'false'],
12-
useCache: ['true', 'false']
14+
name: ['', '/', '/index.js'],
15+
dir: ['rel', 'abs'],
16+
files: [5e2],
17+
n: [1, 1e3],
18+
cache: ['true', 'false']
1319
});
1420

15-
function main({ n, fullPath, useCache }) {
21+
function main({ n, name, cache, files, dir }) {
1622
tmpdir.refresh();
17-
try { fs.mkdirSync(benchmarkDirectory); } catch {}
18-
for (var i = 0; i <= n; i++) {
23+
fs.mkdirSync(benchmarkDirectory);
24+
for (var i = 0; i <= files; i++) {
1925
fs.mkdirSync(`${benchmarkDirectory}${i}`);
2026
fs.writeFileSync(
2127
`${benchmarkDirectory}${i}/package.json`,
@@ -27,38 +33,28 @@ function main({ n, fullPath, useCache }) {
2733
);
2834
}
2935

30-
if (fullPath === 'true')
31-
measureFull(n, useCache === 'true');
32-
else
33-
measureDir(n, useCache === 'true');
36+
if (dir === 'rel')
37+
benchmarkDirectory = path.relative(__dirname, benchmarkDirectory);
3438

35-
tmpdir.refresh();
36-
}
39+
measureDir(n, cache === 'true', files, name);
3740

38-
function measureFull(n, useCache) {
39-
var i;
40-
if (useCache) {
41-
for (i = 0; i <= n; i++) {
42-
require(`${benchmarkDirectory}${i}/index.js`);
43-
}
44-
}
45-
bench.start();
46-
for (i = 0; i <= n; i++) {
47-
require(`${benchmarkDirectory}${i}/index.js`);
48-
}
49-
bench.end(n);
41+
tmpdir.refresh();
5042
}
5143

52-
function measureDir(n, useCache) {
44+
function measureDir(n, cache, files, name) {
5345
var i;
54-
if (useCache) {
55-
for (i = 0; i <= n; i++) {
56-
require(`${benchmarkDirectory}${i}`);
46+
if (cache) {
47+
for (i = 0; i <= files; i++) {
48+
require(`${benchmarkDirectory}${i}${name}`);
5749
}
5850
}
5951
bench.start();
60-
for (i = 0; i <= n; i++) {
61-
require(`${benchmarkDirectory}${i}`);
52+
for (i = 0; i <= files; i++) {
53+
for (var j = 0; j < n; j++)
54+
require(`${benchmarkDirectory}${i}${name}`);
55+
// Pretend mixed input (otherwise the results are less representative due to
56+
// highly specialized code).
57+
require(otherModules[i % otherModules.length]);
6258
}
63-
bench.end(n);
59+
bench.end(n * files);
6460
}

0 commit comments

Comments
 (0)