-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs:improve error performance for
readSync
- Loading branch information
pluris
committed
Oct 4, 2023
1 parent
1a839f3
commit 593d4ba
Showing
4 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'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'], | ||
paramType: ['offset-and-length', 'no-offset-and-length'], | ||
n: [1e4], | ||
}); | ||
|
||
function main({ n, type, paramType }) { | ||
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(); | ||
switch (paramType) { | ||
case 'offset-and-length': | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.readSync(fd, Buffer.alloc(1), 0, 1, 0); | ||
} catch { | ||
// Continue regardless of error. | ||
} | ||
} | ||
break; | ||
case 'no-offset-and-length': | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.readSync(fd, Buffer.alloc(1)); | ||
} catch { | ||
// Continue regardless of error. | ||
} | ||
} | ||
break; | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
bench.end(n); | ||
|
||
if (fd === 'existing') fs.closeSync(fd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters