2023-09-17 20:42:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const fs = require('fs');
|
|
|
|
const tmpdir = require('../../test/common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
|
|
|
const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
|
|
|
|
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
type: ['existing', 'non-existing', 'non-flat-existing'],
|
2024-09-05 20:56:54 +00:00
|
|
|
method: ['access', 'accessSync'],
|
2023-09-17 20:42:46 +00:00
|
|
|
n: [1e5],
|
|
|
|
});
|
|
|
|
|
2023-10-14 19:51:30 +00:00
|
|
|
function runBench(n, path) {
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
try {
|
|
|
|
fs.accessSync(path);
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 20:56:54 +00:00
|
|
|
function runAsyncBench(n, path) {
|
|
|
|
(function r(cntr, path) {
|
|
|
|
if (cntr-- <= 0)
|
|
|
|
return bench.end(n);
|
|
|
|
fs.access(path, () => {
|
|
|
|
r(cntr, path);
|
|
|
|
});
|
|
|
|
})(n, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
function main({ n, type, method }) {
|
2023-09-17 20:42:46 +00:00
|
|
|
let path;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'existing':
|
|
|
|
path = __filename;
|
|
|
|
break;
|
|
|
|
case 'non-flat-existing':
|
|
|
|
path = tmpfile;
|
|
|
|
break;
|
|
|
|
case 'non-existing':
|
|
|
|
path = tmpdir.resolve(`.non-existing-file-${process.pid}`);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
new Error('Invalid type');
|
|
|
|
}
|
|
|
|
|
2024-09-05 20:56:54 +00:00
|
|
|
if (method === 'access') {
|
|
|
|
// Warmup the filesystem - it doesn't need to use the async method
|
|
|
|
runBench(n, path);
|
|
|
|
|
|
|
|
bench.start();
|
|
|
|
runAsyncBench(n, path);
|
|
|
|
} else {
|
|
|
|
// warmup
|
|
|
|
runBench(n, path);
|
|
|
|
|
|
|
|
bench.start();
|
|
|
|
runBench(n, path);
|
|
|
|
bench.end(n);
|
|
|
|
}
|
2023-09-17 20:42:46 +00:00
|
|
|
}
|