11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst fs = require('fs');
31cb0ef41Sopenharmony_ciconst path = require('path');
41cb0ef41Sopenharmony_ciconst { builtinModules } = require('module');
51cb0ef41Sopenharmony_ciconst common = require('../common.js');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst tmpdir = require('../../test/common/tmpdir');
81cb0ef41Sopenharmony_cilet benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Filter all irregular modules.
111cb0ef41Sopenharmony_ciconst otherModules = builtinModules.filter((name) => !/\/|^_|^sys/.test(name));
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
141cb0ef41Sopenharmony_ci  name: ['', '/', '/index.js'],
151cb0ef41Sopenharmony_ci  dir: ['rel', 'abs'],
161cb0ef41Sopenharmony_ci  files: [5e2],
171cb0ef41Sopenharmony_ci  n: [1, 1e3],
181cb0ef41Sopenharmony_ci  cache: ['true', 'false'],
191cb0ef41Sopenharmony_ci});
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cifunction main({ n, name, cache, files, dir }) {
221cb0ef41Sopenharmony_ci  tmpdir.refresh();
231cb0ef41Sopenharmony_ci  fs.mkdirSync(benchmarkDirectory);
241cb0ef41Sopenharmony_ci  for (let i = 0; i <= files; i++) {
251cb0ef41Sopenharmony_ci    fs.mkdirSync(`${benchmarkDirectory}${i}`);
261cb0ef41Sopenharmony_ci    fs.writeFileSync(
271cb0ef41Sopenharmony_ci      `${benchmarkDirectory}${i}/package.json`,
281cb0ef41Sopenharmony_ci      '{"main": "index.js"}',
291cb0ef41Sopenharmony_ci    );
301cb0ef41Sopenharmony_ci    fs.writeFileSync(
311cb0ef41Sopenharmony_ci      `${benchmarkDirectory}${i}/index.js`,
321cb0ef41Sopenharmony_ci      'module.exports = "";',
331cb0ef41Sopenharmony_ci    );
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  if (dir === 'rel')
371cb0ef41Sopenharmony_ci    benchmarkDirectory = path.relative(__dirname, benchmarkDirectory);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  measureDir(n, cache === 'true', files, name);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  tmpdir.refresh();
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cifunction measureDir(n, cache, files, name) {
451cb0ef41Sopenharmony_ci  if (cache) {
461cb0ef41Sopenharmony_ci    for (let i = 0; i <= files; i++) {
471cb0ef41Sopenharmony_ci      require(`${benchmarkDirectory}${i}${name}`);
481cb0ef41Sopenharmony_ci    }
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci  bench.start();
511cb0ef41Sopenharmony_ci  for (let i = 0; i <= files; i++) {
521cb0ef41Sopenharmony_ci    for (let j = 0; j < n; j++)
531cb0ef41Sopenharmony_ci      require(`${benchmarkDirectory}${i}${name}`);
541cb0ef41Sopenharmony_ci    // Pretend mixed input (otherwise the results are less representative due to
551cb0ef41Sopenharmony_ci    // highly specialized code).
561cb0ef41Sopenharmony_ci    require(otherModules[i % otherModules.length]);
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci  bench.end(n * files);
591cb0ef41Sopenharmony_ci}
60