2016-02-19 17:03:16 -08:00
|
|
|
'use strict';
|
2017-09-13 22:48:53 -03:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2019-03-28 22:31:43 +01:00
|
|
|
const { builtinModules } = require('module');
|
2017-09-13 22:48:53 -03:00
|
|
|
const common = require('../common.js');
|
2014-09-13 11:06:28 +02:00
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../../test/common/tmpdir');
|
2023-08-30 18:55:20 +09:00
|
|
|
let benchmarkDirectory = tmpdir.resolve('nodejs-benchmark-module');
|
2019-03-28 22:31:43 +01:00
|
|
|
|
|
|
|
// Filter all irregular modules.
|
|
|
|
const otherModules = builtinModules.filter((name) => !/\/|^_|^sys/.test(name));
|
2014-09-13 11:06:28 +02:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const bench = common.createBenchmark(main, {
|
2019-03-28 22:31:43 +01:00
|
|
|
name: ['', '/', '/index.js'],
|
|
|
|
dir: ['rel', 'abs'],
|
|
|
|
files: [5e2],
|
|
|
|
n: [1, 1e3],
|
2023-01-30 10:05:28 +01:00
|
|
|
cache: ['true', 'false'],
|
2024-09-15 13:57:10 -03:00
|
|
|
}, { flags: '--no-warnings' });
|
2014-09-13 11:06:28 +02:00
|
|
|
|
2019-03-28 22:31:43 +01:00
|
|
|
function main({ n, name, cache, files, dir }) {
|
2017-12-24 22:38:11 -08:00
|
|
|
tmpdir.refresh();
|
2019-03-28 22:31:43 +01:00
|
|
|
fs.mkdirSync(benchmarkDirectory);
|
2019-07-31 08:26:38 -05:00
|
|
|
for (let i = 0; i <= files; i++) {
|
2017-04-17 04:01:12 +03:00
|
|
|
fs.mkdirSync(`${benchmarkDirectory}${i}`);
|
2016-03-02 13:38:23 -08:00
|
|
|
fs.writeFileSync(
|
2017-04-17 04:01:12 +03:00
|
|
|
`${benchmarkDirectory}${i}/package.json`,
|
2023-01-30 10:05:28 +01:00
|
|
|
'{"main": "index.js"}',
|
2016-03-02 13:38:23 -08:00
|
|
|
);
|
|
|
|
fs.writeFileSync(
|
2017-04-17 04:01:12 +03:00
|
|
|
`${benchmarkDirectory}${i}/index.js`,
|
2023-01-30 10:05:28 +01:00
|
|
|
'module.exports = "";',
|
2016-03-02 13:38:23 -08:00
|
|
|
);
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:31:43 +01:00
|
|
|
if (dir === 'rel')
|
|
|
|
benchmarkDirectory = path.relative(__dirname, benchmarkDirectory);
|
2017-03-19 12:32:39 +02:00
|
|
|
|
2019-03-28 22:31:43 +01:00
|
|
|
measureDir(n, cache === 'true', files, name);
|
2016-02-10 03:58:58 -05:00
|
|
|
|
2019-03-28 22:31:43 +01:00
|
|
|
tmpdir.refresh();
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:31:43 +01:00
|
|
|
function measureDir(n, cache, files, name) {
|
|
|
|
if (cache) {
|
2020-01-31 16:50:00 +01:00
|
|
|
for (let i = 0; i <= files; i++) {
|
2019-03-28 22:31:43 +01:00
|
|
|
require(`${benchmarkDirectory}${i}${name}`);
|
2017-01-13 05:02:45 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 11:06:28 +02:00
|
|
|
bench.start();
|
2020-01-31 16:50:00 +01:00
|
|
|
for (let i = 0; i <= files; i++) {
|
|
|
|
for (let j = 0; j < n; j++)
|
2019-03-28 22:31:43 +01:00
|
|
|
require(`${benchmarkDirectory}${i}${name}`);
|
|
|
|
// Pretend mixed input (otherwise the results are less representative due to
|
|
|
|
// highly specialized code).
|
|
|
|
require(otherModules[i % otherModules.length]);
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|
2019-03-28 22:31:43 +01:00
|
|
|
bench.end(n * files);
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|