|
| 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 | + |
| 51 | +function measure(n) { |
| 52 | + bench.start(); |
| 53 | + for (var i = 0; i <= n; i++) { |
| 54 | + require(benchmarkDirectory + i); |
| 55 | + } |
| 56 | + bench.end(n / 1e3); |
| 57 | +} |
| 58 | + |
| 59 | +function rmrf(location) { |
| 60 | + if (fs.existsSync(location)) { |
| 61 | + var things = fs.readdirSync(location); |
| 62 | + things.forEach(function(thing) { |
| 63 | + var cur = path.join(location, thing), |
| 64 | + isDirectory = fs.statSync(cur).isDirectory(); |
| 65 | + if (isDirectory) { |
| 66 | + rmrf(cur); |
| 67 | + return; |
| 68 | + } |
| 69 | + fs.unlinkSync(cur); |
| 70 | + }); |
| 71 | + fs.rmdirSync(location); |
| 72 | + } |
| 73 | +} |
0 commit comments