-
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 perf of sync
chmod
+fchmod
PR-URL: #49859 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
- Loading branch information
1 parent
6bd77db
commit 6bc7fa7
Showing
5 changed files
with
121 additions
and
24 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,41 @@ | ||
'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'], | ||
n: [1e3], | ||
}); | ||
|
||
function main({ n, type }) { | ||
switch (type) { | ||
case 'existing': { | ||
for (let i = 0; i < n; i++) { | ||
fs.writeFileSync(tmpdir.resolve(`chmodsync-bench-file-${i}`), 'bench'); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
fs.chmodSync(tmpdir.resolve(`chmodsync-bench-file-${i}`), 0o666); | ||
} | ||
bench.end(n); | ||
break; | ||
} | ||
case 'non-existing': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.chmodSync(tmpdir.resolve(`chmod-non-existing-file-${i}`), 0o666); | ||
} 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'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'], | ||
n: [1e3], | ||
}); | ||
|
||
function main({ n, type }) { | ||
let files; | ||
|
||
switch (type) { | ||
case 'existing': | ||
files = []; | ||
|
||
// Populate tmpdir with mock files | ||
for (let i = 0; i < n; i++) { | ||
const path = tmpdir.resolve(`fchmodsync-bench-file-${i}`); | ||
fs.writeFileSync(path, 'bench'); | ||
files.push(path); | ||
} | ||
break; | ||
case 'non-existing': | ||
files = new Array(n).fill(tmpdir.resolve(`.non-existing-file-${Date.now()}`)); | ||
break; | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
|
||
const fds = files.map((x) => { | ||
// Try to open, if not return likely invalid fd (1 << 30) | ||
try { | ||
return fs.openSync(x, 'r'); | ||
} catch { | ||
return 1 << 30; | ||
} | ||
}); | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.fchmodSync(fds[i], 0o666); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
bench.end(n); | ||
|
||
for (const x of fds) { | ||
try { | ||
fs.closeSync(x); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
} |
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