11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst fs = require('fs');
41cb0ef41Sopenharmony_ciconst path = require('path');
51cb0ef41Sopenharmony_ciconst common = require('../common.js');
61cb0ef41Sopenharmony_ciconst tmpdir = require('../../test/common/tmpdir');
71cb0ef41Sopenharmony_ciconst benchmarkDirectory = path.join(tmpdir.path, 'nodejs-benchmark-module');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
101cb0ef41Sopenharmony_ci  type: ['.js', '.json', 'dir'],
111cb0ef41Sopenharmony_ci  n: [1e4],
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction main({ type, n }) {
151cb0ef41Sopenharmony_ci  tmpdir.refresh();
161cb0ef41Sopenharmony_ci  createEntryPoint(n);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  switch (type) {
191cb0ef41Sopenharmony_ci    case '.js':
201cb0ef41Sopenharmony_ci      measureJSFile(n);
211cb0ef41Sopenharmony_ci      break;
221cb0ef41Sopenharmony_ci    case '.json':
231cb0ef41Sopenharmony_ci      measureJSONFile(n);
241cb0ef41Sopenharmony_ci      break;
251cb0ef41Sopenharmony_ci    case 'dir':
261cb0ef41Sopenharmony_ci      measureDir(n);
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  tmpdir.refresh();
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction measureJSFile(n) {
331cb0ef41Sopenharmony_ci  bench.start();
341cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
351cb0ef41Sopenharmony_ci    require(`${benchmarkDirectory}/${i}`);
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci  bench.end(n);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifunction measureJSONFile(n) {
411cb0ef41Sopenharmony_ci  bench.start();
421cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
431cb0ef41Sopenharmony_ci    require(`${benchmarkDirectory}/json_${i}.json`);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci  bench.end(n);
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cifunction measureDir(n) {
491cb0ef41Sopenharmony_ci  bench.start();
501cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
511cb0ef41Sopenharmony_ci    require(`${benchmarkDirectory}${i}`);
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci  bench.end(n);
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cifunction createEntryPoint(n) {
571cb0ef41Sopenharmony_ci  fs.mkdirSync(benchmarkDirectory);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  const JSFileContent = 'module.exports = [];';
601cb0ef41Sopenharmony_ci  const JSONFileContent = '[]';
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
631cb0ef41Sopenharmony_ci    // JS file.
641cb0ef41Sopenharmony_ci    fs.writeFileSync(`${benchmarkDirectory}/${i}.js`, JSFileContent);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    // JSON file.
671cb0ef41Sopenharmony_ci    fs.writeFileSync(`${benchmarkDirectory}/json_${i}.json`, JSONFileContent);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    // Dir.
701cb0ef41Sopenharmony_ci    fs.mkdirSync(`${benchmarkDirectory}${i}`);
711cb0ef41Sopenharmony_ci    fs.writeFileSync(
721cb0ef41Sopenharmony_ci      `${benchmarkDirectory}${i}/package.json`,
731cb0ef41Sopenharmony_ci      '{"main": "index.js"}',
741cb0ef41Sopenharmony_ci    );
751cb0ef41Sopenharmony_ci    fs.writeFileSync(
761cb0ef41Sopenharmony_ci      `${benchmarkDirectory}${i}/index.js`,
771cb0ef41Sopenharmony_ci      JSFileContent,
781cb0ef41Sopenharmony_ci    );
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci}
81