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

Add benchmarks #101

Merged
merged 8 commits into from Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions benchmark.js
@@ -0,0 +1,68 @@
'use strict';
const path = require('path');
const Benchmark = require('benchmark');
const makeDir = require('make-dir');
const tempy = require('tempy');
const del = require('.');

const suite = new Benchmark.Suite('concurrency');

const tempDir = tempy.directory();

const fixtures = Array.from({length: 2000}, (x, index) => {
return path.resolve(tempDir, (index + 1).toString());
});

function createFixtures() {
for (const fixture of fixtures) {
makeDir.sync(path.resolve(tempDir, fixture));
}
}

const concurrencies = [1, 3, 5, 10, 15, 20, 50, 100, 200, 300, 400, 500, 1000, Infinity];

for (const concurrency of concurrencies) {
const name = `concurrency: ${concurrency.toString()}`;

suite.add({
name,
defer: true,
setup() {}, // This line breaks async await
async fn(deferred) {
// Can't use setup because it isn't called after every defer
// https://github.com/bestiejs/benchmark.js/issues/136
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
createFixtures();

const removedFiles = await del(['**/*'], {
cwd: tempDir,
concurrency
});

if (removedFiles.length !== fixtures.length) {
const error = new Error(
`"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`,
);

console.error(error);

del.sync(tempDir, {cwd: tempDir, force: true});

// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}

deferred.resolve();
}
});
}

suite
.on('cycle', event => {
console.log(String(event.target));
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
})
.on('complete', function () {
console.log(`Fastest is ${this.filter('fastest').map('name')}`);

del.sync(tempDir, {cwd: tempDir, force: true});
})
.run({async: true});
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -13,7 +13,8 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
"test": "xo && ava && tsd",
"bench": "node benchmark.js"
},
"files": [
"index.js",
Expand Down Expand Up @@ -53,6 +54,7 @@
},
"devDependencies": {
"ava": "^2.3.0",
"benchmark": "^2.1.4",
"make-dir": "^3.0.0",
"tempy": "^0.3.0",
"tsd": "^0.7.3",
Expand Down