-
Notifications
You must be signed in to change notification settings - Fork 101
/
stream.ts
96 lines (74 loc) · 2.34 KB
/
stream.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as path from 'node:path';
import * as bencho from 'bencho';
import * as utils from '../../utils';
import type * as fastGlobCurrent from '../../..';
type GlobImplementation = 'current' | 'previous';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type GlobImplFunction = (...args: any[]) => ReturnType<typeof fastGlobCurrent.stream>;
type GlobOptions = fastGlobCurrent.Options;
class Glob {
readonly #pattern: string;
readonly #options: GlobOptions;
constructor(pattern: string, options: GlobOptions) {
this.#pattern = pattern;
this.#options = {
unique: false,
followSymbolicLinks: false,
...options,
};
}
public async measurePreviousVersion(): Promise<void> {
const glob = await utils.importAndMeasure(utils.importPreviousFastGlob);
// @ts-expect-error remove this line after the next major release.
await this.#measure(() => glob.globStream(this.#pattern, this.#options));
}
public async measureCurrentVersion(): Promise<void> {
const glob = await utils.importAndMeasure(utils.importCurrentFastGlob);
await this.#measure(() => glob.globStream(this.#pattern, this.#options));
}
async #measure(function_: GlobImplFunction): Promise<void> {
const entries: string[] = [];
const timeStart = utils.timeStart();
await new Promise<void>((resolve, reject) => {
const stream = function_();
stream.once('error', (error) => {
reject(error);
});
stream.on('data', (entry: string) => entries.push(entry));
stream.once('end', () => {
resolve();
});
});
const count = entries.length;
const memory = utils.getMemory();
const time = utils.timeEnd(timeStart);
bencho.time('time', time);
bencho.memory('memory', memory);
bencho.value('entries', count);
}
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
const args = process.argv.slice(2);
const cwd = path.join(process.cwd(), args[0]);
const pattern = args[1];
const impl = args[2] as GlobImplementation;
const options = JSON.parse(process.env['BENCHMARK_OPTIONS'] ?? '{}') as GlobOptions;
const glob = new Glob(pattern, {
cwd,
...options,
});
switch (impl) {
case 'current': {
await glob.measureCurrentVersion();
break;
}
case 'previous': {
await glob.measurePreviousVersion();
break;
}
default: {
throw new TypeError('Unknown glob implementation.');
}
}
})();