-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
benchmark.js
81 lines (66 loc) · 1.83 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
70
71
72
73
74
75
76
77
78
79
80
81
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import Benchmark from 'benchmark';
import {temporaryDirectory} from 'tempy';
import {deleteAsync, deleteSync} from './index.js';
const suite = new Benchmark.Suite('concurrency');
const temporaryDirectoryPath = temporaryDirectory();
const fixtures = Array.from({length: 2000}, (_, index) => path.resolve(temporaryDirectoryPath, (index + 1).toString()));
function createFixtures() {
for (const fixture of fixtures) {
fs.mkdirSync(path.resolve(temporaryDirectoryPath, fixture), {recursive: true});
}
}
const concurrencies = [
1,
3,
5,
10,
15,
20,
50,
100,
200,
300,
400,
500,
1000,
Number.POSITIVE_INFINITY,
];
for (const concurrency of concurrencies) {
const name = `concurrency: ${concurrency.toString()}`;
suite.add({
name,
defer: true,
async fn(deferred) {
// Can't use `setup()` because it isn't called after every
// defer and it breaks using `async` keyword here.
// https://github.com/bestiejs/benchmark.js/issues/136
createFixtures();
const removedFiles = await deleteAsync(['**/*'], {
cwd: temporaryDirectoryPath,
concurrency,
});
if (removedFiles.length !== fixtures.length) {
const error = new Error(
`"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`,
);
console.error(error);
deleteSync(temporaryDirectoryPath, {cwd: temporaryDirectoryPath, 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')}`);
deleteSync(temporaryDirectoryPath, {cwd: temporaryDirectoryPath, force: true});
})
.run({async: true});