@@ -16,6 +16,7 @@ import { Command } from '../src/command';
16
16
import { defaultReporter } from '../src/default-reporter' ;
17
17
import { defaultStatsCapture } from '../src/default-stats-capture' ;
18
18
import { runBenchmark } from '../src/run-benchmark' ;
19
+ import { runBenchmarkWatch } from './run-benchmark-watch' ;
19
20
20
21
21
22
export interface MainOptions {
@@ -47,6 +48,9 @@ export async function main({
47
48
--output-file File to output benchmark log to.
48
49
--overwrite-output-file If the output file should be overwritten rather than appended to.
49
50
--prefix Logging prefix.
51
+ --watch-matcher Text to match in stdout to mark an iteration complete.
52
+ --watch-timeout The maximum time in 'ms' to wait for the text specified in the matcher to be matched. Default is 10000.
53
+ --watch-script Script to run before each watch iteration.
50
54
51
55
Example:
52
56
benchmark --iterations=3 -- node my-script.js
@@ -63,19 +67,27 @@ export async function main({
63
67
'output-file' : string | null ;
64
68
cwd: string ;
65
69
prefix: string ;
70
+ 'watch-timeout' : number ;
71
+ 'watch-matcher' ? : string ;
72
+ 'watch-script' ? : string ;
66
73
'--' : string [ ] | null ;
67
74
}
68
75
69
76
// Parse the command line.
70
77
const argv = minimist ( args , {
71
78
boolean : [ 'help' , 'verbose' , 'overwrite-output-file' ] ,
79
+ string : [
80
+ 'watch-matcher' ,
81
+ 'watch-script' ,
82
+ ] ,
72
83
default : {
73
84
'exit-code' : 0 ,
74
85
'iterations' : 5 ,
75
86
'retries' : 5 ,
76
87
'output-file' : null ,
77
88
'cwd' : process . cwd ( ) ,
78
89
'prefix' : '[benchmark]' ,
90
+ 'watch-timeout' : 10000 ,
79
91
} ,
80
92
'--' : true ,
81
93
} ) as { } as BenchmarkCliArgv ;
@@ -127,6 +139,29 @@ export async function main({
127
139
128
140
const commandArgv = argv [ '--' ] ;
129
141
142
+ const {
143
+ 'watch-timeout' : watchTimeout ,
144
+ 'watch-matcher' : watchMatcher ,
145
+ 'watch-script' : watchScript ,
146
+ 'exit-code' : exitCode ,
147
+ 'output-file' : outFile ,
148
+ iterations,
149
+ retries,
150
+ } = argv ;
151
+
152
+ // Exit early if we can't find the command to benchmark.
153
+ if ( watchMatcher && ! watchScript ) {
154
+ logger . fatal ( `Cannot use --watch-matcher without specifying --watch-script.` ) ;
155
+
156
+ return 1 ;
157
+ }
158
+
159
+ if ( ! watchMatcher && watchScript ) {
160
+ logger . fatal ( `Cannot use --watch-script without specifying --watch-matcher.` ) ;
161
+
162
+ return 1 ;
163
+ }
164
+
130
165
// Exit early if we can't find the command to benchmark.
131
166
if ( ! commandArgv || ! Array . isArray ( argv [ '--' ] ) || ( argv [ '--' ] as Array < string > ) . length < 1 ) {
132
167
logger . fatal ( `Missing command, see benchmark --help for help.` ) ;
@@ -135,32 +170,42 @@ export async function main({
135
170
}
136
171
137
172
// Setup file logging.
138
- if ( argv [ 'output-file' ] !== null ) {
173
+ if ( outFile !== null ) {
139
174
if ( argv [ 'overwrite-output-file' ] ) {
140
- writeFileSync ( argv [ 'output-file' ] as string , '' ) ;
175
+ writeFileSync ( outFile , '' ) ;
141
176
}
142
177
logger . pipe ( filter ( entry => ( entry . level != 'debug' || argv [ 'verbose' ] ) ) )
143
- . subscribe ( entry => appendFileSync ( argv [ 'output-file' ] as string , `${ entry . message } \n` ) ) ;
178
+ . subscribe ( entry => appendFileSync ( outFile , `${ entry . message } \n` ) ) ;
144
179
}
145
180
146
181
// Run benchmark on given command, capturing stats and reporting them.
147
- const exitCode = argv [ 'exit-code' ] ;
148
182
const cmd = commandArgv [ 0 ] ;
149
183
const cmdArgs = commandArgv . slice ( 1 ) ;
150
184
const command = new Command ( cmd , cmdArgs , argv [ 'cwd' ] , exitCode ) ;
151
185
const captures = [ defaultStatsCapture ] ;
152
186
const reporters = [ defaultReporter ( logger ) ] ;
153
- const iterations = argv [ 'iterations' ] ;
154
- const retries = argv [ 'retries' ] ;
155
187
156
188
logger . info ( `Benchmarking process over ${ iterations } iterations, with up to ${ retries } retries.` ) ;
157
189
logger . info ( ` ${ command . toString ( ) } ` ) ;
158
190
159
- let res ;
160
191
try {
161
- res = await runBenchmark (
162
- { command, captures, reporters, iterations, retries, logger } ,
163
- ) . pipe ( toArray ( ) ) . toPromise ( ) ;
192
+ let res$ ;
193
+ if ( watchMatcher && watchScript ) {
194
+ res$ = runBenchmarkWatch ( {
195
+ command, captures, reporters, iterations, retries, logger,
196
+ watchCommand : new Command ( 'node' , [ watchScript ] ) , watchMatcher, watchTimeout,
197
+ } ) ;
198
+ } else {
199
+ res$ = runBenchmark (
200
+ { command, captures, reporters, iterations, retries, logger } ,
201
+ ) ;
202
+ }
203
+
204
+ const res = await res$ . pipe ( toArray ( ) ) . toPromise ( ) ;
205
+ if ( res . length === 0 ) {
206
+ return 1 ;
207
+ }
208
+
164
209
} catch ( error ) {
165
210
if ( error . message ) {
166
211
logger . fatal ( error . message ) ;
@@ -171,10 +216,6 @@ export async function main({
171
216
return 1 ;
172
217
}
173
218
174
- if ( res . length === 0 ) {
175
- return 1 ;
176
- }
177
-
178
219
return 0 ;
179
220
}
180
221
0 commit comments