mirror of
https://github.com/nodejs/node.git
synced 2024-11-28 14:33:11 +01:00
506858bf13
PR-URL: https://github.com/nodejs/node/pull/50033 Refs: https://github.com/nodejs/performance/issues/106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const fs = require('fs');
|
|
const tmpdir = require('../../test/common/tmpdir');
|
|
tmpdir.refresh();
|
|
|
|
const bufferSize = 1024;
|
|
const sectorSize = 512;
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
type: ['existing', 'non-existing'],
|
|
n: [1e4],
|
|
});
|
|
|
|
function main({ n, type }) {
|
|
let fd;
|
|
|
|
const tmpfile = { name: tmpdir.resolve(`.existing-file-${process.pid}`),
|
|
len: bufferSize * n };
|
|
|
|
|
|
tmpfile.contents = Buffer.allocUnsafe(tmpfile.len);
|
|
|
|
for (let offset = 0; offset < tmpfile.len; offset += sectorSize) {
|
|
const fillByte = 256 * Math.random();
|
|
const nBytesToFill = Math.min(sectorSize, tmpfile.len - offset);
|
|
tmpfile.contents.fill(fillByte, offset, offset + nBytesToFill);
|
|
}
|
|
|
|
fs.writeFileSync(tmpfile.name, tmpfile.contents);
|
|
|
|
switch (type) {
|
|
case 'existing':
|
|
fd = fs.openSync(tmpfile.name, 'r', 0o666);
|
|
break;
|
|
case 'non-existing':
|
|
fd = 1 << 30;
|
|
break;
|
|
default:
|
|
new Error('Invalid type');
|
|
}
|
|
|
|
const buffer = Buffer.alloc(bufferSize);
|
|
|
|
bench.start();
|
|
for (let i = 0; i < n; i++) {
|
|
try {
|
|
fs.readSync(fd, buffer);
|
|
} catch {
|
|
// Continue regardless of error.
|
|
}
|
|
}
|
|
bench.end(n);
|
|
|
|
if (type === 'existing') fs.closeSync(fd);
|
|
}
|