Skip to content
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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions benchmark/fs/promises-access.js
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());
Copy link
Member

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.

Copy link
Member

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

Copy link
Member

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)

}
}

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');
}
}
70 changes: 70 additions & 0 deletions benchmark/fs/promises-chmod.js
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');
}
}
77 changes: 77 additions & 0 deletions benchmark/fs/promises-chown.js
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');
}
}
76 changes: 76 additions & 0 deletions benchmark/fs/promises-copyfile.js
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');
}
}
77 changes: 77 additions & 0 deletions benchmark/fs/promises-open-close.js
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');
}
}
Loading