@@ -16,6 +16,7 @@ import { Command } from '../src/command';
1616import { defaultReporter } from '../src/default-reporter' ;
1717import { defaultStatsCapture } from '../src/default-stats-capture' ;
1818import { runBenchmark } from '../src/run-benchmark' ;
19+ import { runBenchmarkWatch } from './run-benchmark-watch' ;
1920
2021
2122export interface MainOptions {
@@ -47,6 +48,9 @@ export async function main({
4748 --output-file File to output benchmark log to.
4849 --overwrite-output-file If the output file should be overwritten rather than appended to.
4950 --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.
5054
5155 Example:
5256 benchmark --iterations=3 -- node my-script.js
@@ -63,19 +67,27 @@ export async function main({
6367 'output-file' : string | null ;
6468 cwd: string ;
6569 prefix: string ;
70+ 'watch-timeout' : number ;
71+ 'watch-matcher' ? : string ;
72+ 'watch-script' ? : string ;
6673 '--' : string [ ] | null ;
6774 }
6875
6976 // Parse the command line.
7077 const argv = minimist ( args , {
7178 boolean : [ 'help' , 'verbose' , 'overwrite-output-file' ] ,
79+ string : [
80+ 'watch-matcher' ,
81+ 'watch-script' ,
82+ ] ,
7283 default : {
7384 'exit-code' : 0 ,
7485 'iterations' : 5 ,
7586 'retries' : 5 ,
7687 'output-file' : null ,
7788 'cwd' : process . cwd ( ) ,
7889 'prefix' : '[benchmark]' ,
90+ 'watch-timeout' : 10000 ,
7991 } ,
8092 '--' : true ,
8193 } ) as { } as BenchmarkCliArgv ;
@@ -127,6 +139,29 @@ export async function main({
127139
128140 const commandArgv = argv [ '--' ] ;
129141
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+
130165 // Exit early if we can't find the command to benchmark.
131166 if ( ! commandArgv || ! Array . isArray ( argv [ '--' ] ) || ( argv [ '--' ] as Array < string > ) . length < 1 ) {
132167 logger . fatal ( `Missing command, see benchmark --help for help.` ) ;
@@ -135,32 +170,42 @@ export async function main({
135170 }
136171
137172 // Setup file logging.
138- if ( argv [ 'output-file' ] !== null ) {
173+ if ( outFile !== null ) {
139174 if ( argv [ 'overwrite-output-file' ] ) {
140- writeFileSync ( argv [ 'output-file' ] as string , '' ) ;
175+ writeFileSync ( outFile , '' ) ;
141176 }
142177 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` ) ) ;
144179 }
145180
146181 // Run benchmark on given command, capturing stats and reporting them.
147- const exitCode = argv [ 'exit-code' ] ;
148182 const cmd = commandArgv [ 0 ] ;
149183 const cmdArgs = commandArgv . slice ( 1 ) ;
150184 const command = new Command ( cmd , cmdArgs , argv [ 'cwd' ] , exitCode ) ;
151185 const captures = [ defaultStatsCapture ] ;
152186 const reporters = [ defaultReporter ( logger ) ] ;
153- const iterations = argv [ 'iterations' ] ;
154- const retries = argv [ 'retries' ] ;
155187
156188 logger . info ( `Benchmarking process over ${ iterations } iterations, with up to ${ retries } retries.` ) ;
157189 logger . info ( ` ${ command . toString ( ) } ` ) ;
158190
159- let res ;
160191 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+
164209 } catch ( error ) {
165210 if ( error . message ) {
166211 logger . fatal ( error . message ) ;
@@ -171,10 +216,6 @@ export async function main({
171216 return 1 ;
172217 }
173218
174- if ( res . length === 0 ) {
175- return 1 ;
176- }
177-
178219 return 0 ;
179220}
180221
0 commit comments