MCPcopy Index your code
hub / github.com/nodejs/node / main

Function main

benchmark/fs/bench-readSync.js:16–57  ·  view source on GitHub ↗
({ n, type })

Source from the content-addressed store, hash-verified

14});
15
16function main({ n, type }) {
17 let fd;
18
19 const tmpfile = { name: tmpdir.resolve(`.existing-file-${process.pid}`),
20 len: bufferSize * n };
21
22
23 tmpfile.contents = Buffer.allocUnsafe(tmpfile.len);
24
25 for (let offset = 0; offset < tmpfile.len; offset += sectorSize) {
26 const fillByte = 256 * Math.random();
27 const nBytesToFill = Math.min(sectorSize, tmpfile.len - offset);
28 tmpfile.contents.fill(fillByte, offset, offset + nBytesToFill);
29 }
30
31 fs.writeFileSync(tmpfile.name, tmpfile.contents);
32
33 switch (type) {
34 case 'existing':
35 fd = fs.openSync(tmpfile.name, 'r', 0o666);
36 break;
37 case 'non-existing':
38 fd = 1 << 30;
39 break;
40 default:
41 new Error('Invalid type');
42 }
43
44 const buffer = Buffer.alloc(bufferSize);
45
46 bench.start();
47 for (let i = 0; i < n; i++) {
48 try {
49 fs.readSync(fd, buffer);
50 } catch {
51 // Continue regardless of error.
52 }
53 }
54 bench.end(n);
55
56 if (type === 'existing') fs.closeSync(fd);
57}

Callers

nothing calls this directly

Calls 11

randomMethod · 0.80
fillMethod · 0.80
allocMethod · 0.80
resolveMethod · 0.45
minMethod · 0.45
writeFileSyncMethod · 0.45
openSyncMethod · 0.45
startMethod · 0.45
readSyncMethod · 0.45
endMethod · 0.45
closeSyncMethod · 0.45

Tested by

no test coverage detected