From 3941719dea0ef40ecb9adbb3ed3775525aacdf9c Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Fri, 12 Jul 2019 17:53:41 -0700 Subject: [PATCH 1/7] Add benchmarks --- benchmark.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 4 ++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 benchmark.js diff --git a/benchmark.js b/benchmark.js new file mode 100644 index 0000000..541ba82 --- /dev/null +++ b/benchmark.js @@ -0,0 +1,70 @@ +'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 concurrency = [1, 3, 5, 10, 15, 20, 50, 100, Infinity]; + +concurrency.forEach(num => { + const name = `concurrency: ${num.toString()}`; + + suite.add({ + name, + defer: true, + fn(deferred) { + // Can't use setup because it isn't called after every defer + // https://github.com/bestiejs/benchmark.js/issues/136 + createFixtures(); + + // Async await was giving too many errors. stick with standard promises + del(['**/*'], { + cwd: tempDir, + concurrency: num + // eslint-disable-next-line promise/prefer-await-to-then + }).then(removedFiles => { + 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)); + }) + .on('complete', function () { + console.log(`Fastest is ${this.filter('fastest').map('name')}`); + + del.sync(tempDir, {cwd: tempDir, force: true}); + }) + .run({async: true}); diff --git a/package.json b/package.json index 054bfb2..5047f84 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava && tsd" + "test": "xo && ava && tsd", + "bench": "node benchmark.js" }, "files": [ "index.js", @@ -52,6 +53,7 @@ }, "devDependencies": { "ava": "^2.1.0", + "benchmark": "^2.1.4", "make-dir": "^3.0.0", "tempy": "^0.3.0", "tsd": "^0.7.3", From 22dc07c4ab382fe4667e801287b7828e60a75158 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 13 Jul 2019 12:57:27 +0700 Subject: [PATCH 2/7] Update benchmark.js --- benchmark.js | 1 - 1 file changed, 1 deletion(-) diff --git a/benchmark.js b/benchmark.js index 541ba82..e490160 100644 --- a/benchmark.js +++ b/benchmark.js @@ -1,5 +1,4 @@ 'use strict'; - const path = require('path'); const Benchmark = require('benchmark'); const makeDir = require('make-dir'); From d066a9d2dccd1771029f72a201584518f583ddef Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Mon, 15 Jul 2019 10:47:01 -0700 Subject: [PATCH 3/7] use for-of and rename variable "num" --- benchmark.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/benchmark.js b/benchmark.js index e490160..5ddb814 100644 --- a/benchmark.js +++ b/benchmark.js @@ -19,10 +19,10 @@ function createFixtures() { } } -const concurrency = [1, 3, 5, 10, 15, 20, 50, 100, Infinity]; +const concurrencies = [1, 3, 5, 10, 15, 20, 50, 100, Infinity]; -concurrency.forEach(num => { - const name = `concurrency: ${num.toString()}`; +for (const concurrency of concurrencies) { + const name = `concurrency: ${concurrency.toString()}`; suite.add({ name, @@ -35,7 +35,7 @@ concurrency.forEach(num => { // Async await was giving too many errors. stick with standard promises del(['**/*'], { cwd: tempDir, - concurrency: num + concurrency // eslint-disable-next-line promise/prefer-await-to-then }).then(removedFiles => { if (removedFiles.length !== fixtures.length) { @@ -55,7 +55,7 @@ concurrency.forEach(num => { }); } }); -}); +} suite .on('cycle', event => { From 3a665d32fa8fb40f034fc01411654b1dac493163 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Wed, 21 Aug 2019 20:01:24 -0700 Subject: [PATCH 4/7] Add more concurrencies --- benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark.js b/benchmark.js index 5ddb814..fac225b 100644 --- a/benchmark.js +++ b/benchmark.js @@ -19,7 +19,7 @@ function createFixtures() { } } -const concurrencies = [1, 3, 5, 10, 15, 20, 50, 100, Infinity]; +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()}`; From 705c4e22df5432203e0d43718bb08773b2bd9831 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Thu, 22 Aug 2019 07:33:58 -0700 Subject: [PATCH 5/7] use async await --- benchmark.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/benchmark.js b/benchmark.js index fac225b..96556fb 100644 --- a/benchmark.js +++ b/benchmark.js @@ -27,32 +27,30 @@ for (const concurrency of concurrencies) { suite.add({ name, defer: true, - fn(deferred) { + async fn(deferred) { // Can't use setup because it isn't called after every defer // https://github.com/bestiejs/benchmark.js/issues/136 createFixtures(); - // Async await was giving too many errors. stick with standard promises - del(['**/*'], { + const removedFiles = await del(['**/*'], { cwd: tempDir, concurrency - // eslint-disable-next-line promise/prefer-await-to-then - }).then(removedFiles => { - if (removedFiles.length !== fixtures.length) { - const error = new Error( - `"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`, - ); + }); - console.error(error); + if (removedFiles.length !== fixtures.length) { + const error = new Error( + `"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`, + ); - del.sync(tempDir, {cwd: tempDir, force: true}); + console.error(error); - // eslint-disable-next-line unicorn/no-process-exit - process.exit(1); - } + del.sync(tempDir, {cwd: tempDir, force: true}); - deferred.resolve(); - }); + // eslint-disable-next-line unicorn/no-process-exit + process.exit(1); + } + + deferred.resolve(); } }); } From 7f1d106d57143e83a000920cd745fd8d987b4c15 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Thu, 22 Aug 2019 07:35:28 -0700 Subject: [PATCH 6/7] demonstrate broken async await --- benchmark.js | 1 + 1 file changed, 1 insertion(+) diff --git a/benchmark.js b/benchmark.js index 96556fb..48a88b3 100644 --- a/benchmark.js +++ b/benchmark.js @@ -27,6 +27,7 @@ for (const concurrency of concurrencies) { 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 From 58a46126f46ef3598a15ae97b76bc81132d4d940 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Thu, 22 Aug 2019 07:38:10 -0700 Subject: [PATCH 7/7] lint fix --- benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark.js b/benchmark.js index 48a88b3..41cb7b3 100644 --- a/benchmark.js +++ b/benchmark.js @@ -27,7 +27,7 @@ for (const concurrency of concurrencies) { suite.add({ name, defer: true, - setup(){}, // this line breaks async await + 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