2019-01-02 13:54:51 +01:00
|
|
|
'use strict';
|
|
|
|
const fs = require('fs');
|
|
|
|
const common = require('../common.js');
|
|
|
|
|
|
|
|
const tmpdir = require('../../test/common/tmpdir');
|
2023-08-30 18:55:20 +09:00
|
|
|
const benchmarkDirectory = tmpdir.resolve('nodejs-benchmark-module');
|
2019-01-02 13:54:51 +01:00
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
ext: ['', '.js'],
|
|
|
|
files: [1e3],
|
2023-01-30 10:05:28 +01:00
|
|
|
cache: ['true', 'false'],
|
2019-01-02 13:54:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function main({ ext, cache, files }) {
|
|
|
|
tmpdir.refresh();
|
|
|
|
fs.mkdirSync(benchmarkDirectory);
|
|
|
|
fs.writeFileSync(
|
|
|
|
`${benchmarkDirectory}/a.js`,
|
2023-01-30 10:05:28 +01:00
|
|
|
'module.exports = {};',
|
2019-01-02 13:54:51 +01:00
|
|
|
);
|
2019-07-31 08:26:38 -05:00
|
|
|
for (let i = 0; i <= files; i++) {
|
2019-01-02 13:54:51 +01:00
|
|
|
fs.mkdirSync(`${benchmarkDirectory}/${i}`);
|
|
|
|
fs.writeFileSync(
|
|
|
|
`${benchmarkDirectory}/${i}/package.json`,
|
2023-01-30 10:05:28 +01:00
|
|
|
'{"main": "index.js"}',
|
2019-01-02 13:54:51 +01:00
|
|
|
);
|
|
|
|
fs.writeFileSync(
|
|
|
|
`${benchmarkDirectory}/${i}/index.js`,
|
2023-01-30 10:05:28 +01:00
|
|
|
`require('../a${ext}');`,
|
2019-01-02 13:54:51 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
measureDir(cache === 'true', files);
|
|
|
|
|
|
|
|
tmpdir.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
function measureDir(cache, files) {
|
|
|
|
if (cache) {
|
2020-01-31 16:50:00 +01:00
|
|
|
for (let i = 0; i <= files; i++) {
|
2019-01-02 13:54:51 +01:00
|
|
|
require(`${benchmarkDirectory}/${i}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bench.start();
|
2020-01-31 16:50:00 +01:00
|
|
|
for (let i = 0; i <= files; i++) {
|
2019-01-02 13:54:51 +01:00
|
|
|
require(`${benchmarkDirectory}/${i}`);
|
|
|
|
}
|
|
|
|
bench.end(files);
|
|
|
|
}
|