-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 perf of sync
*times
PR-URL: #49864 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
- Loading branch information
1 parent
a85e418
commit 6bd77db
Showing
4 changed files
with
97 additions
and
41 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,61 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['existing', 'non-existing'], | ||
func: [ 'utimes', 'futimes', 'lutimes' ], | ||
n: [1e3], | ||
}); | ||
|
||
function main({ n, type, func }) { | ||
const useFds = func === 'futimes'; | ||
const fsFunc = fs[func + 'Sync']; | ||
|
||
switch (type) { | ||
case 'existing': { | ||
const files = []; | ||
|
||
// Populate tmpdir with mock files | ||
for (let i = 0; i < n; i++) { | ||
const path = tmpdir.resolve(`timessync-bench-file-${i}`); | ||
fs.writeFileSync(path, 'bench'); | ||
files.push(useFds ? fs.openSync(path, 'r+') : path); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
fsFunc(files[i], i, i); | ||
} | ||
bench.end(n); | ||
|
||
if (useFds) files.forEach((x) => { | ||
try { | ||
fs.closeSync(x); | ||
} catch { | ||
// do nothing | ||
} | ||
}); | ||
|
||
break; | ||
} | ||
case 'non-existing': { | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fsFunc(useFds ? (1 << 30) : tmpdir.resolve(`.non-existing-file-${Date.now()}`), i, i); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
bench.end(n); | ||
|
||
break; | ||
} | ||
default: | ||
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