mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
f7a160d5b4
PR-URL: https://github.com/nodejs/node/pull/49898 Refs: https://github.com/nodejs/performance/issues/106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
43 lines
819 B
JavaScript
43 lines
819 B
JavaScript
'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'],
|
|
n: [1e4],
|
|
});
|
|
|
|
function main({ n, type }) {
|
|
let fd;
|
|
|
|
switch (type) {
|
|
case 'existing':
|
|
fd = fs.openSync(tmpfile, 'r', 0o666);
|
|
break;
|
|
case 'non-existing':
|
|
fd = 1 << 30;
|
|
break;
|
|
default:
|
|
new Error('Invalid type');
|
|
}
|
|
|
|
bench.start();
|
|
for (let i = 0; i < n; i++) {
|
|
try {
|
|
fs.fdatasyncSync(fd);
|
|
} catch {
|
|
// do nothing
|
|
}
|
|
}
|
|
|
|
bench.end(n);
|
|
|
|
if (type === 'existing') fs.closeSync(fd);
|
|
}
|