• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const fs = require('fs');
3const path = require('path');
4const { builtinModules } = require('module');
5const common = require('../common.js');
6
7const tmpdir = require('../../test/common/tmpdir');
8let benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');
9
10// Filter all irregular modules.
11const otherModules = builtinModules.filter((name) => !/\/|^_|^sys/.test(name));
12
13const bench = common.createBenchmark(main, {
14  name: ['', '/', '/index.js'],
15  dir: ['rel', 'abs'],
16  files: [5e2],
17  n: [1, 1e3],
18  cache: ['true', 'false'],
19});
20
21function main({ n, name, cache, files, dir }) {
22  tmpdir.refresh();
23  fs.mkdirSync(benchmarkDirectory);
24  for (let i = 0; i <= files; i++) {
25    fs.mkdirSync(`${benchmarkDirectory}${i}`);
26    fs.writeFileSync(
27      `${benchmarkDirectory}${i}/package.json`,
28      '{"main": "index.js"}',
29    );
30    fs.writeFileSync(
31      `${benchmarkDirectory}${i}/index.js`,
32      'module.exports = "";',
33    );
34  }
35
36  if (dir === 'rel')
37    benchmarkDirectory = path.relative(__dirname, benchmarkDirectory);
38
39  measureDir(n, cache === 'true', files, name);
40
41  tmpdir.refresh();
42}
43
44function measureDir(n, cache, files, name) {
45  if (cache) {
46    for (let i = 0; i <= files; i++) {
47      require(`${benchmarkDirectory}${i}${name}`);
48    }
49  }
50  bench.start();
51  for (let i = 0; i <= files; i++) {
52    for (let j = 0; j < n; j++)
53      require(`${benchmarkDirectory}${i}${name}`);
54    // Pretend mixed input (otherwise the results are less representative due to
55    // highly specialized code).
56    require(otherModules[i % otherModules.length]);
57  }
58  bench.end(n * files);
59}
60