Repository navigation
Expand file tree
/
Copy pathbenchmark.mts
More file actions
314 lines (263 loc) · 9.97 KB
/
Copy pathbenchmark.mts
File metadata and controls
314 lines (263 loc) · 9.97 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import fs from 'node:fs';
import path from 'node:path';
import { type BenchmarkCliOptions, runI18nBenchmarks } from './benchmarks/i18n/index.mts';
import {
LIBRARY_BUILDERS,
STYLE_FLAVORS,
type LibraryBuilder,
type StyleFlavor,
} from './benchmarks/library-builder/fixtures.mts';
import {
type LibraryBuilderBenchmarkOptions,
runLibraryBuilderBenchmarks,
} from './benchmarks/library-builder/index.mts';
function checkBuildStatus(logger: Console): boolean {
const distFile = 'dist/@angular/build/src/tools/i18n/i18n-inliner.js';
const srcFile = 'packages/angular/build/src/tools/i18n/i18n-inliner.ts';
if (!fs.existsSync(distFile)) {
logger.error(
'Error: @angular/build has not been built yet.\nPlease run "pnpm build" before benchmarking.',
);
return false;
}
if (fs.existsSync(srcFile)) {
const srcMtime = fs.statSync(srcFile).mtimeMs;
const distMtime = fs.statSync(distFile).mtimeMs;
if (srcMtime > distMtime) {
logger.warn(
'Warning: Source files in packages/angular/build are newer than dist/.\n' +
'Run "pnpm build" to ensure your benchmark reflects your latest local edits.\n',
);
}
}
return true;
}
function parseIntList(raw: string | number | undefined): number[] | undefined {
if (raw === undefined) {
return undefined;
}
return String(raw)
.split(',')
.map((s) => Number(s.trim()));
}
function parseStringList(raw: string | undefined): string[] | undefined {
if (raw === undefined) {
return undefined;
}
return raw.split(',').map((s) => s.trim());
}
async function runLibraryBuilderSubsystem(
options: {
layout?: string;
builder?: string;
count?: string;
depth?: string;
'style-flavor'?: string;
iterations?: string | number;
json?: boolean;
csv?: string | boolean;
},
cwd: string,
): Promise<number> {
if (options.layout !== undefined && options.layout !== 'flat' && options.layout !== 'deep') {
// eslint-disable-next-line no-console
console.error('Error: --layout must be "flat" or "deep", got "' + options.layout + '".');
return 1;
}
const count = parseIntList(options.count);
if (count !== undefined && count.some((n) => !Number.isInteger(n) || n < 1 || n > 5000)) {
// eslint-disable-next-line no-console
console.error(
'Error: --count must be a comma-separated list of positive integers up to 5000, got "' +
options.count +
'".',
);
return 1;
}
const depth = parseIntList(options.depth);
if (depth !== undefined && depth.some((n) => !Number.isInteger(n) || n < 1 || n > 13)) {
// eslint-disable-next-line no-console
console.error(
'Error: --depth must be a comma-separated list of positive integers up to 13, got "' +
options.depth +
'".',
);
return 1;
}
const iterations = options.iterations !== undefined ? Number(options.iterations) : undefined;
if (iterations !== undefined && (!Number.isInteger(iterations) || iterations < 1)) {
// eslint-disable-next-line no-console
console.error(
'Error: --iterations must be a positive integer, got "' + options.iterations + '".',
);
return 1;
}
if (options.csv !== undefined && (typeof options.csv !== 'string' || options.csv.length === 0)) {
// eslint-disable-next-line no-console
console.error('Error: --csv requires a file path, got "' + options.csv + '".');
return 1;
}
const builder = parseStringList(options.builder);
const validBuilders: readonly string[] = LIBRARY_BUILDERS;
if (builder !== undefined && builder.some((b) => !validBuilders.includes(b))) {
// eslint-disable-next-line no-console
console.error(
'Error: --builder must be a comma-separated list of: ' +
LIBRARY_BUILDERS.join(', ') +
'. Got "' +
options.builder +
'".',
);
return 1;
}
const styleFlavor = parseStringList(options['style-flavor']);
const validStyleFlavors: readonly string[] = STYLE_FLAVORS;
if (styleFlavor !== undefined && styleFlavor.some((f) => !validStyleFlavors.includes(f))) {
// eslint-disable-next-line no-console
console.error(
'Error: --style-flavor must be a comma-separated list of: ' +
STYLE_FLAVORS.join(', ') +
'. Got "' +
options['style-flavor'] +
'".',
);
return 1;
}
const cliOptions: LibraryBuilderBenchmarkOptions = {
layout: options.layout as LibraryBuilderBenchmarkOptions['layout'],
builder: builder as LibraryBuilder[] | undefined,
count,
depth,
styleFlavor: styleFlavor as StyleFlavor[] | undefined,
iterations,
json: Boolean(options.json),
csv: options.csv !== undefined ? path.resolve(cwd, options.csv as string) : undefined,
};
const { exitCode } = await runLibraryBuilderBenchmarks(cliOptions);
return exitCode;
}
export default async function (
options: {
_?: string[];
scenario?: string;
iterations?: string | number;
warmup?: string | number;
concurrency?: string | number;
build?: boolean;
json?: boolean;
inProcess?: boolean;
'in-process'?: boolean;
saveBaseline?: string;
'save-baseline'?: string;
compareBaseline?: string;
'compare-baseline'?: string;
layout?: string;
builder?: string;
count?: string;
depth?: string;
'style-flavor'?: string;
help?: boolean;
[key: string]: unknown;
},
_cwd: string,
): Promise<number> {
const positionals = options._ ?? [];
const targetSubsystem = positionals[0] ?? 'i18n';
if (options.help || targetSubsystem === 'help') {
// eslint-disable-next-line no-console
console.log(`
Angular CLI Performance Benchmark Runner
Usage:
pnpm admin benchmark [subsystem] [options]
Subsystems:
i18n Run i18n inliner performance benchmarks (default)
library-builder Run @angular/build:library (and :ng-packagr) scaling benchmarks
Options (i18n):
--scenario=<name> Run a specific scenario (e.g. standard-app, enterprise-multilingual)
--iterations=<n> Number of measured iterations (default: 5)
--warmup=<n> Number of warmup iterations (default: 2)
--concurrency=<n> Override worker thread pool concurrency
--in-process Run all scenarios in a single process (useful for debugging)
--build Automatically build packages before benchmarking
--json Output results in machine-readable JSON
--save-baseline=<file> Save run results to a baseline JSON file
--compare-baseline=<file> Compare run results against an existing baseline JSON file
Options (library-builder):
--layout=<flat|deep> Fixture layout (default: flat)
--builder=<b,b,...> Builders to benchmark, one or more of: library, ng-packagr.
Pass both for an A/B run (default: library)
--count=<n,n,...> Entry-point count targets, drives both layouts
(default: 10,20,50,100,200,300,400,500,1000,2000)
--depth=<n,n,...> Explicit tree depths, layout=deep only. Overrides the depths
that would otherwise be derived from --count (the smallest
depth with 2^depth >= each target count).
--style-flavor=<f,f,...> Style-flavor targets, one or more of: inline, inline-css,
inline-scss, external, external-css, external-scss
(default: inline)
--iterations=<n> Measured iterations (default: 3)
--json Output raw measurements as JSON to stdout
--csv=<path> Write raw per-iteration measurements as CSV to <path>
--help Show this help message
`);
return 0;
}
if (targetSubsystem === 'library-builder') {
return runLibraryBuilderSubsystem(options, _cwd);
}
if (targetSubsystem !== 'i18n') {
// eslint-disable-next-line no-console
console.error(
`Unknown benchmark subsystem: "${targetSubsystem}". Supported subsystems: i18n, library-builder`,
);
return 1;
}
if (options.build) {
const buildModule = await import('./build.mts');
await buildModule.default({ local: true });
}
// eslint-disable-next-line no-console
if (!checkBuildStatus(console)) {
return 1;
}
const rawSaveBaseline = options.saveBaseline ?? options['save-baseline'];
const rawCompareBaseline = options.compareBaseline ?? options['compare-baseline'];
const iterations = options.iterations !== undefined ? Number(options.iterations) : undefined;
const warmup = options.warmup !== undefined ? Number(options.warmup) : undefined;
const concurrency = options.concurrency !== undefined ? Number(options.concurrency) : undefined;
if (iterations !== undefined && (!Number.isInteger(iterations) || iterations < 1)) {
// eslint-disable-next-line no-console
console.error('Error: --iterations must be a positive integer.');
return 1;
}
if (warmup !== undefined && (!Number.isInteger(warmup) || warmup < 0)) {
// eslint-disable-next-line no-console
console.error('Error: --warmup must be a non-negative integer.');
return 1;
}
if (concurrency !== undefined && (!Number.isInteger(concurrency) || concurrency < 1)) {
// eslint-disable-next-line no-console
console.error('Error: --concurrency must be a positive integer.');
return 1;
}
const cliOptions: BenchmarkCliOptions = {
scenario: options.scenario,
iterations,
warmup,
concurrency,
json: Boolean(options.json),
inProcess: Boolean(options.inProcess ?? options['in-process']),
saveBaseline: rawSaveBaseline ? path.resolve(_cwd, String(rawSaveBaseline)) : undefined,
compareBaseline: rawCompareBaseline
? path.resolve(_cwd, String(rawCompareBaseline))
: undefined,
};
const { exitCode } = await runI18nBenchmarks(cliOptions);
return exitCode;
}