1'use strict'; 2 3const common = require('../common'); 4const fs = require('fs'); 5const path = require('path'); 6 7const bench = common.createBenchmark(main, { 8 n: [100], 9 dir: [ 'lib', 'test/parallel'], 10 mode: [ 'async', 'sync', 'callback' ], 11 bufferSize: [ 4, 32, 1024 ] 12}); 13 14async function main({ n, dir, mode, bufferSize }) { 15 const fullPath = path.resolve(__dirname, '../../', dir); 16 17 bench.start(); 18 19 let counter = 0; 20 for (let i = 0; i < n; i++) { 21 if (mode === 'async') { 22 const dir = await fs.promises.opendir(fullPath, { bufferSize }); 23 // eslint-disable-next-line no-unused-vars 24 for await (const entry of dir) 25 counter++; 26 } else if (mode === 'callback') { 27 const dir = await fs.promises.opendir(fullPath, { bufferSize }); 28 await new Promise((resolve, reject) => { 29 function read() { 30 dir.read((err, entry) => { 31 if (err) { 32 reject(err); 33 } else if (entry === null) { 34 resolve(dir.close()); 35 } else { 36 counter++; 37 read(); 38 } 39 }); 40 } 41 42 read(); 43 }); 44 } else { 45 const dir = fs.opendirSync(fullPath, { bufferSize }); 46 while (dir.readSync() !== null) 47 counter++; 48 dir.closeSync(); 49 } 50 } 51 52 bench.end(counter); 53} 54