forked from sindresorhus/del
/
benchmark.js
69 lines (55 loc) · 1.71 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'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,
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
// 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});