-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
writeSync
- Loading branch information
pluris
committed
Dec 27, 2023
1 parent
6a1abd2
commit d68e8ef
Showing
5 changed files
with
79 additions
and
29 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,54 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const path = tmpdir.resolve(`new-file-${process.pid}`); | ||
const parameters = [Buffer.from('Benchmark data'), | ||
0, | ||
Buffer.byteLength('Benchmark data')]; | ||
const bench = common.createBenchmark(main, { | ||
type: ['valid', 'invalid'], | ||
n: [1e5], | ||
}); | ||
|
||
function main({ n, type }) { | ||
let fd; | ||
let result; | ||
|
||
switch (type) { | ||
case 'valid': | ||
fd = fs.openSync(path, 'w'); | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
result = fs.writeSync(fd, ...parameters); | ||
} | ||
|
||
bench.end(n); | ||
assert(result); | ||
fs.closeSync(fd); | ||
break; | ||
case 'invalid': { | ||
fd = 1 << 30; | ||
let hasError = false; | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
result = fs.writeSync(fd, ...parameters); | ||
} catch { | ||
hasError = true; | ||
} | ||
} | ||
|
||
bench.end(n); | ||
assert(hasError); | ||
break; | ||
} | ||
default: | ||
throw new Error('Invalid type'); | ||
} | ||
} |
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
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