-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 of sync methods
PR-URL: #49593 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
- Loading branch information
Showing
10 changed files
with
529 additions
and
198 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,42 @@ | ||
'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', 'non-flat-existing'], | ||
n: [1e5], | ||
}); | ||
|
||
function main({ n, type }) { | ||
let path; | ||
|
||
switch (type) { | ||
case 'existing': | ||
path = __filename; | ||
break; | ||
case 'non-flat-existing': | ||
path = tmpfile; | ||
break; | ||
case 'non-existing': | ||
path = tmpdir.resolve(`.non-existing-file-${process.pid}`); | ||
break; | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.accessSync(path); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
bench.end(n); | ||
} |
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,37 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['invalid', 'valid'], | ||
n: [1e4], | ||
}); | ||
|
||
function main({ n, type }) { | ||
tmpdir.refresh(); | ||
const dest = tmpdir.resolve(`copy-file-bench-${process.pid}`); | ||
let path; | ||
|
||
switch (type) { | ||
case 'invalid': | ||
path = tmpdir.resolve(`.existing-file-${process.pid}`); | ||
break; | ||
case 'valid': | ||
path = __filename; | ||
break; | ||
default: | ||
throw new Error('Invalid type'); | ||
} | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.copyFileSync(path, dest); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
bench.end(n); | ||
} |
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,38 @@ | ||
'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', 'non-flat-existing'], | ||
n: [1e6], | ||
}); | ||
|
||
function main({ n, type }) { | ||
let path; | ||
|
||
switch (type) { | ||
case 'existing': | ||
path = __filename; | ||
break; | ||
case 'non-flat-existing': | ||
path = tmpfile; | ||
break; | ||
case 'non-existing': | ||
path = tmpdir.resolve(`.non-existing-file-${process.pid}`); | ||
break; | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
fs.existsSync(path); | ||
} | ||
bench.end(n); | ||
} |
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,37 @@ | ||
'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: [1e5], | ||
}); | ||
|
||
function main({ n, type }) { | ||
let path; | ||
|
||
switch (type) { | ||
case 'existing': | ||
path = __filename; | ||
break; | ||
case 'non-existing': | ||
path = tmpdir.resolve(`.non-existing-file-${process.pid}`); | ||
break; | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
const fd = fs.openSync(path, 'r', 0o666); | ||
fs.closeSync(fd); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
bench.end(n); | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.