-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathmain.ts
186 lines (158 loc) · 5.28 KB
/
main.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
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
#!/usr/bin/env node
/**
* @license
* Copyright Google Inc. 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.io/license
*/
import { logging, tags, terminal } from '@angular-devkit/core';
import { ProcessOutput } from '@angular-devkit/core/node';
import { appendFileSync, writeFileSync } from 'fs';
import * as minimist from 'minimist';
import { filter, map, toArray } from 'rxjs/operators';
import { Command } from '../src/command';
import { defaultReporter } from '../src/default-reporter';
import { defaultStatsCapture } from '../src/default-stats-capture';
import { runBenchmark } from '../src/run-benchmark';
export interface MainOptions {
args: string[];
stdout?: ProcessOutput;
stderr?: ProcessOutput;
}
export async function main({
args,
stdout = process.stdout,
stderr = process.stderr,
}: MainOptions): Promise<0 | 1> {
// Show usage of the CLI tool, and exit the process.
function usage(logger: logging.Logger) {
logger.info(tags.stripIndent`
benchmark [options] -- [command to benchmark]
Collects process stats from running the command.
Options:
--help Show this message.
--verbose Show more information while running.
--exit-code Expected exit code for the command. Default is 0.
--iterations Number of iterations to run the benchmark over. Default is 5.
--retries Number of times to retry when process fails. Default is 5.
--cwd Current working directory to run the process in.
--output-file File to output benchmark log to.
--overwrite-output-file If the output file should be overwritten rather than appended to.
--prefix Logging prefix.
Example:
benchmark --iterations=3 -- node my-script.js
`);
}
interface BenchmarkCliArgv {
help: boolean;
verbose: boolean;
'overwrite-output-file': boolean;
'exit-code': number;
iterations: number;
retries: number;
'output-file': string | null;
cwd: string;
prefix: string;
'--': string[] | null;
}
// Parse the command line.
const argv = minimist(args, {
boolean: ['help', 'verbose', 'overwrite-output-file'],
default: {
'exit-code': 0,
'iterations': 5,
'retries': 5,
'output-file': null,
'cwd': process.cwd(),
'prefix': '[benchmark]',
},
'--': true,
}) as {} as BenchmarkCliArgv;
// Create the DevKit Logger used through the CLI.
const logger = new logging.TransformLogger(
'benchmark-prefix-logger',
stream => stream.pipe(map(entry => {
if (argv['prefix']) { entry.message = `${argv['prefix']} ${entry.message}`; }
return entry;
})),
);
// Log to console.
logger
.pipe(filter(entry => (entry.level != 'debug' || argv['verbose'])))
.subscribe(entry => {
let color: (s: string) => string = x => terminal.dim(terminal.white(x));
let output = stdout;
switch (entry.level) {
case 'info':
color = s => s;
break;
case 'warn':
color = terminal.yellow;
output = stderr;
break;
case 'error':
color = terminal.red;
output = stderr;
break;
case 'fatal':
color = (x: string) => terminal.bold(terminal.red(x));
output = stderr;
break;
}
output.write(color(entry.message) + '\n');
});
// Print help.
if (argv['help']) {
usage(logger);
return 0;
}
const commandArgv = argv['--'];
// Exit early if we can't find the command to benchmark.
if (!commandArgv || !Array.isArray(argv['--']) || (argv['--'] as Array<string>).length < 1) {
logger.fatal(`Missing command, see benchmark --help for help.`);
return 1;
}
// Setup file logging.
if (argv['output-file'] !== null) {
if (argv['overwrite-output-file']) {
writeFileSync(argv['output-file'] as string, '');
}
logger.pipe(filter(entry => (entry.level != 'debug' || argv['verbose'])))
.subscribe(entry => appendFileSync(argv['output-file'] as string, `${entry.message}\n`));
}
// Run benchmark on given command, capturing stats and reporting them.
const exitCode = argv['exit-code'];
const cmd = commandArgv[0];
const cmdArgs = commandArgv.slice(1);
const command = new Command(cmd, cmdArgs, argv['cwd'], exitCode);
const captures = [defaultStatsCapture];
const reporters = [defaultReporter(logger)];
const iterations = argv['iterations'];
const retries = argv['retries'];
logger.info(`Benchmarking process over ${iterations} iterations, with up to ${retries} retries.`);
logger.info(` ${command.toString()}`);
let res;
try {
res = await runBenchmark(
{ command, captures, reporters, iterations, retries, logger },
).pipe(toArray()).toPromise();
} catch (error) {
if (error.message) {
logger.fatal(error.message);
} else {
logger.fatal(error);
}
return 1;
}
if (res.length === 0) {
return 1;
}
return 0;
}
if (require.main === module) {
const args = process.argv.slice(2);
main({ args })
.then(exitCode => process.exitCode = exitCode)
.catch(e => { throw (e); });
}