2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2017-09-14 03:48:53 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const common = require('../common.js');
|
2014-09-13 11:06:28 +02:00
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const tmpDirectory = path.join(__dirname, '..', 'tmp');
|
|
|
|
const benchmarkDirectory = path.join(tmpDirectory, 'nodejs-benchmark-module');
|
2014-09-13 11:06:28 +02:00
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const bench = common.createBenchmark(main, {
|
2016-02-10 09:58:58 +01:00
|
|
|
thousands: [50],
|
2017-01-13 11:02:45 +01:00
|
|
|
fullPath: ['true', 'false'],
|
|
|
|
useCache: ['true', 'false']
|
2014-09-13 11:06:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function main(conf) {
|
2017-09-14 03:48:53 +02:00
|
|
|
const n = +conf.thousands * 1e3;
|
2016-02-10 09:58:58 +01:00
|
|
|
|
2014-09-13 11:06:28 +02:00
|
|
|
rmrf(tmpDirectory);
|
|
|
|
try { fs.mkdirSync(tmpDirectory); } catch (e) {}
|
|
|
|
try { fs.mkdirSync(benchmarkDirectory); } catch (e) {}
|
|
|
|
|
|
|
|
for (var i = 0; i <= n; i++) {
|
2017-04-17 03:01:12 +02:00
|
|
|
fs.mkdirSync(`${benchmarkDirectory}${i}`);
|
2016-03-02 22:38:23 +01:00
|
|
|
fs.writeFileSync(
|
2017-04-17 03:01:12 +02:00
|
|
|
`${benchmarkDirectory}${i}/package.json`,
|
2016-03-02 22:38:23 +01:00
|
|
|
'{"main": "index.js"}'
|
|
|
|
);
|
|
|
|
fs.writeFileSync(
|
2017-04-17 03:01:12 +02:00
|
|
|
`${benchmarkDirectory}${i}/index.js`,
|
2016-03-02 22:38:23 +01:00
|
|
|
'module.exports = "";'
|
|
|
|
);
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 09:58:58 +01:00
|
|
|
if (conf.fullPath === 'true')
|
2017-01-13 11:02:45 +01:00
|
|
|
measureFull(n, conf.useCache === 'true');
|
2016-02-10 09:58:58 +01:00
|
|
|
else
|
2017-01-13 11:02:45 +01:00
|
|
|
measureDir(n, conf.useCache === 'true');
|
2017-03-19 11:32:39 +01:00
|
|
|
|
|
|
|
rmrf(tmpDirectory);
|
2016-02-10 09:58:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 11:02:45 +01:00
|
|
|
function measureFull(n, useCache) {
|
|
|
|
var i;
|
|
|
|
if (useCache) {
|
|
|
|
for (i = 0; i <= n; i++) {
|
2017-04-17 03:01:12 +02:00
|
|
|
require(`${benchmarkDirectory}${i}/index.js`);
|
2017-01-13 11:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-10 09:58:58 +01:00
|
|
|
bench.start();
|
2017-01-13 11:02:45 +01:00
|
|
|
for (i = 0; i <= n; i++) {
|
2017-04-17 03:01:12 +02:00
|
|
|
require(`${benchmarkDirectory}${i}/index.js`);
|
2016-02-10 09:58:58 +01:00
|
|
|
}
|
|
|
|
bench.end(n / 1e3);
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 11:02:45 +01:00
|
|
|
function measureDir(n, useCache) {
|
|
|
|
var i;
|
|
|
|
if (useCache) {
|
|
|
|
for (i = 0; i <= n; i++) {
|
2017-04-17 03:01:12 +02:00
|
|
|
require(`${benchmarkDirectory}${i}`);
|
2017-01-13 11:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-13 11:06:28 +02:00
|
|
|
bench.start();
|
2017-01-13 11:02:45 +01:00
|
|
|
for (i = 0; i <= n; i++) {
|
2017-04-17 03:01:12 +02:00
|
|
|
require(`${benchmarkDirectory}${i}`);
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|
|
|
|
bench.end(n / 1e3);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rmrf(location) {
|
2014-12-15 19:58:37 +01:00
|
|
|
try {
|
2017-09-14 03:48:53 +02:00
|
|
|
const things = fs.readdirSync(location);
|
2014-09-13 11:06:28 +02:00
|
|
|
things.forEach(function(thing) {
|
|
|
|
var cur = path.join(location, thing),
|
2016-02-25 07:30:10 +01:00
|
|
|
isDirectory = fs.statSync(cur).isDirectory();
|
2014-09-13 11:06:28 +02:00
|
|
|
if (isDirectory) {
|
|
|
|
rmrf(cur);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fs.unlinkSync(cur);
|
|
|
|
});
|
|
|
|
fs.rmdirSync(location);
|
2014-12-15 19:58:37 +01:00
|
|
|
} catch (err) {
|
|
|
|
// Ignore error
|
2014-09-13 11:06:28 +02:00
|
|
|
}
|
|
|
|
}
|