-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathbench-readSync.js
More file actions
57 lines (44 loc) · 1.26 KB
/
bench-readSync.js
File metadata and controls
57 lines (44 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'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);
}