11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fs = require('fs');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
81cb0ef41Sopenharmony_ci  n: [100],
91cb0ef41Sopenharmony_ci  dir: [ 'lib', 'test/parallel'],
101cb0ef41Sopenharmony_ci  mode: [ 'async', 'sync', 'callback' ],
111cb0ef41Sopenharmony_ci  bufferSize: [ 4, 32, 1024 ],
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciasync function main({ n, dir, mode, bufferSize }) {
151cb0ef41Sopenharmony_ci  const fullPath = path.resolve(__dirname, '../../', dir);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  bench.start();
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  let counter = 0;
201cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
211cb0ef41Sopenharmony_ci    if (mode === 'async') {
221cb0ef41Sopenharmony_ci      const dir = await fs.promises.opendir(fullPath, { bufferSize });
231cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-unused-vars
241cb0ef41Sopenharmony_ci      for await (const entry of dir)
251cb0ef41Sopenharmony_ci        counter++;
261cb0ef41Sopenharmony_ci    } else if (mode === 'callback') {
271cb0ef41Sopenharmony_ci      const dir = await fs.promises.opendir(fullPath, { bufferSize });
281cb0ef41Sopenharmony_ci      await new Promise((resolve, reject) => {
291cb0ef41Sopenharmony_ci        function read() {
301cb0ef41Sopenharmony_ci          dir.read((err, entry) => {
311cb0ef41Sopenharmony_ci            if (err) {
321cb0ef41Sopenharmony_ci              reject(err);
331cb0ef41Sopenharmony_ci            } else if (entry === null) {
341cb0ef41Sopenharmony_ci              resolve(dir.close());
351cb0ef41Sopenharmony_ci            } else {
361cb0ef41Sopenharmony_ci              counter++;
371cb0ef41Sopenharmony_ci              read();
381cb0ef41Sopenharmony_ci            }
391cb0ef41Sopenharmony_ci          });
401cb0ef41Sopenharmony_ci        }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci        read();
431cb0ef41Sopenharmony_ci      });
441cb0ef41Sopenharmony_ci    } else {
451cb0ef41Sopenharmony_ci      const dir = fs.opendirSync(fullPath, { bufferSize });
461cb0ef41Sopenharmony_ci      while (dir.readSync() !== null)
471cb0ef41Sopenharmony_ci        counter++;
481cb0ef41Sopenharmony_ci      dir.closeSync();
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  bench.end(counter);
531cb0ef41Sopenharmony_ci}
54