Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit fc7fcc7

Browse files
benchmark: add test for module loader
this adds a test for benchmarking the module loader, needed for benchmarking changes / refacortings in the module loader.
1 parent 7c04197 commit fc7fcc7

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

benchmark/misc/module-loader.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
23+
var fs = require('fs');
24+
var path = require('path');
25+
var common = require('../common.js');
26+
var packageJson = '{"main": "index.js"}';
27+
28+
var tmpDirectory = path.join(__dirname, '..', 'tmp');
29+
var benchmarkDirectory = path.join(tmpDirectory, 'nodejs-benchmark-module');
30+
31+
var bench = common.createBenchmark(main, {
32+
thousands: [50]
33+
});
34+
35+
function main(conf) {
36+
rmrf(tmpDirectory);
37+
try { fs.mkdirSync(tmpDirectory); } catch (e) {}
38+
try { fs.mkdirSync(benchmarkDirectory); } catch (e) {}
39+
40+
var n = +conf.thousands * 1e3;
41+
for (var i = 0; i <= n; i++) {
42+
fs.mkdirSync(benchmarkDirectory + i);
43+
fs.writeFileSync(benchmarkDirectory + i + '/package.json', '{"main": "index.js"}');
44+
fs.writeFileSync(benchmarkDirectory + i + '/index.js', 'module.exports = "";');
45+
}
46+
47+
measure(n);
48+
}
49+
50+
function measure(n) {
51+
bench.start();
52+
for (var i = 0; i <= n; i++) {
53+
require(benchmarkDirectory + i);
54+
}
55+
bench.end(n / 1e3);
56+
}
57+
58+
function rmrf(location) {
59+
if (fs.existsSync(location)) {
60+
var things = fs.readdirSync(location);
61+
things.forEach(function(thing) {
62+
var cur = path.join(location, thing),
63+
isDirectory = fs.statSync(cur).isDirectory();
64+
if (isDirectory) {
65+
rmrf(cur);
66+
return;
67+
}
68+
fs.unlinkSync(cur);
69+
});
70+
fs.rmdirSync(location);
71+
}
72+
}

0 commit comments

Comments
 (0)