• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const fs = require('fs');
3const path = require('path');
4const common = require('../common.js');
5
6const tmpdir = require('../../test/common/tmpdir');
7const benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');
8
9const bench = common.createBenchmark(main, {
10  ext: ['', '.js'],
11  files: [1e3],
12  cache: ['true', 'false']
13});
14
15function main({ ext, cache, files }) {
16  tmpdir.refresh();
17  fs.mkdirSync(benchmarkDirectory);
18  fs.writeFileSync(
19    `${benchmarkDirectory}/a.js`,
20    'module.exports = {};'
21  );
22  for (let 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
39function measureDir(cache, files) {
40  if (cache) {
41    for (let i = 0; i <= files; i++) {
42      require(`${benchmarkDirectory}/${i}`);
43    }
44  }
45  bench.start();
46  for (let i = 0; i <= files; i++) {
47    require(`${benchmarkDirectory}/${i}`);
48  }
49  bench.end(files);
50}
51