-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: initial experimental promisified API #15485
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,56 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { access, async } = require('fs'); | ||
const { promisify } = require('util'); | ||
const Countdown = require('../../test/common/countdown'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [20e4], | ||
method: ['legacy', 'promisify', 'promise'] | ||
}); | ||
|
||
function useLegacy(n) { | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
access(__filename, () => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromisify(n) { | ||
const _access = promisify(access); | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_access(__filename).then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromise(n) { | ||
const _access = async.access; | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_access(__filename).then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function main(conf) { | ||
const n = conf.n >>> 0; | ||
const method = conf.method; | ||
|
||
switch (method) { | ||
case 'legacy': return useLegacy(n); | ||
case 'promisify': return usePromisify(n); | ||
case 'promise': return usePromise(n); | ||
default: | ||
throw new Error('invalid method'); | ||
} | ||
} |
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,70 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { | ||
chmod, | ||
async, | ||
unlinkSync, | ||
openSync, | ||
closeSync | ||
} = require('fs'); | ||
const path = require('path'); | ||
const { promisify } = require('util'); | ||
const Countdown = require('../../test/common/countdown'); | ||
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage'); | ||
const mode = process.platform === 'win32' ? 0o600 : 0o644; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [20e4], | ||
method: ['legacy', 'promisify', 'promise'] | ||
}); | ||
|
||
function useLegacy(n) { | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
chmod(filename, mode, () => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromisify(n) { | ||
const _chmod = promisify(chmod); | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_chmod(filename, mode).then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromise(n) { | ||
const _chmod = async.chmod; | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_chmod(filename, mode).then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function main(conf) { | ||
const n = conf.n >>> 0; | ||
const method = conf.method; | ||
|
||
closeSync(openSync(filename, 'w')); | ||
|
||
switch (method) { | ||
case 'legacy': return useLegacy(n); | ||
case 'promisify': return usePromisify(n); | ||
case 'promise': return usePromise(n); | ||
default: | ||
throw new Error('invalid method'); | ||
} | ||
} |
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,77 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
if (process.platform === 'win32') { | ||
console.error('Benchmark unsupported on Windows'); | ||
process.exit(1); | ||
} | ||
|
||
const { | ||
chown, | ||
async, | ||
unlinkSync, | ||
openSync, | ||
closeSync | ||
} = require('fs'); | ||
const path = require('path'); | ||
const { promisify } = require('util'); | ||
const Countdown = require('../../test/common/countdown'); | ||
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage'); | ||
const uid = process.getuid(); | ||
const gid = process.getgid(); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [20e4], | ||
method: ['legacy', 'promisify', 'promise'] | ||
}); | ||
|
||
function useLegacy(n) { | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
chown(filename, uid, gid, () => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromisify(n) { | ||
const _chown = promisify(chown); | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_chown(filename, uid, gid).then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromise(n) { | ||
const _chown = async.chown; | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_chown(filename, uid, gid).then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function main(conf) { | ||
const n = conf.n >>> 0; | ||
const method = conf.method; | ||
|
||
closeSync(openSync(filename, 'w')); | ||
|
||
switch (method) { | ||
case 'legacy': return useLegacy(n); | ||
case 'promisify': return usePromisify(n); | ||
case 'promise': return usePromise(n); | ||
default: | ||
throw new Error('invalid method'); | ||
} | ||
} |
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,76 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const { | ||
async, | ||
unlinkSync, | ||
openSync, | ||
closeSync, | ||
copyFile | ||
} = require('fs'); | ||
const path = require('path'); | ||
const { promisify } = require('util'); | ||
const Countdown = require('../../test/common/countdown'); | ||
const src = path.resolve(__dirname, '.removeme-benchmark-garbage'); | ||
const dest = path.resolve(__dirname, '.removeme-benchmark-garbage2'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [20000], | ||
method: ['legacy', 'promisify', 'promise'] | ||
}); | ||
|
||
function useLegacy(n) { | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(src); | ||
unlinkSync(dest); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
copyFile(src, dest, (err) => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromisify(n) { | ||
const _copyFile = promisify(copyFile); | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(src); | ||
unlinkSync(dest); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_copyFile(src, dest) | ||
.then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromise(n) { | ||
const _copyFile = async.copyFile; | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(src); | ||
unlinkSync(dest); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_copyFile(src, dest) | ||
.then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function main(conf) { | ||
const n = conf.n >>> 0; | ||
const method = conf.method; | ||
|
||
closeSync(openSync(src, 'w')); | ||
|
||
switch (method) { | ||
case 'legacy': return useLegacy(n); | ||
case 'promisify': return usePromisify(n); | ||
case 'promise': return usePromise(n); | ||
default: | ||
throw new Error('invalid method'); | ||
} | ||
} |
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,77 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const { | ||
async, | ||
unlinkSync, | ||
open, | ||
close | ||
} = require('fs'); | ||
const path = require('path'); | ||
const { promisify } = require('util'); | ||
const Countdown = require('../../test/common/countdown'); | ||
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [20000], | ||
method: ['legacy', 'promisify', 'promise'] | ||
}); | ||
|
||
function useLegacy(n) { | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
open(filename, 'w', (err, fd) => { | ||
close(fd, () => { | ||
countdown.dec(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function usePromisify(n) { | ||
const _open = promisify(open); | ||
const _close = promisify(close); | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_open(filename, 'w') | ||
.then(_close) | ||
.then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function usePromise(n) { | ||
const _open = async.open; | ||
const _close = async.close; | ||
const countdown = new Countdown(n, () => { | ||
bench.end(n); | ||
unlinkSync(filename); | ||
}); | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
_open(filename, 'w') | ||
.then(_close) | ||
.then(() => countdown.dec()); | ||
} | ||
} | ||
|
||
function main(conf) { | ||
const n = conf.n >>> 0; | ||
const method = conf.method; | ||
|
||
switch (method) { | ||
case 'legacy': return useLegacy(n); | ||
case 'promisify': return usePromisify(n); | ||
case 'promise': return usePromise(n); | ||
default: | ||
throw new Error('invalid method'); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not convinced that running 2e5 calls in the same tick is actually a good way of benchmarking this. I think it would be better to either use a recursive strategy to see the individual call length or a batched recursive call that batches up to lets say 500 calls together.
This applies to all these benchmarks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, ideally if we can get a more applicative benchmark that'd be the best
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In particular, the promises version always queue the callback to run later every time here which would be disproportionately represented in the above benchmark (I think)