@@ -17,10 +17,40 @@ async function getMeasurements(instrumentFile) {
1717
1818 log ( `Getting measurements for "${ cmd } "` ) ;
1919
20+ const [ appProcess , appProcessClosed ] = await startAppProcess ( cmd ) ;
21+
22+ log ( 'Example app listening, running autocannon...' ) ;
23+
24+ try {
25+ return await startAutocannonProcess ( ) ;
26+ } catch ( error ) {
27+ log ( `Error running autocannon: ${ error } ` ) ;
28+ } finally {
29+ appProcess . kill ( 'SIGKILL' ) ;
30+ await appProcessClosed ;
31+ }
32+ }
33+
34+ async function startAppProcess ( cmd ) {
2035 const appProcess = spawn ( cmd , { shell : true } ) ;
2136
2237 log ( 'Child process started, waiting for example app...' ) ;
2338
39+ // Promise to keep track of the app process being closed
40+ let resolveAppClose , rejectAppClose ;
41+ const appClosePromise = new Promise ( ( res , rej ) => {
42+ resolveAppClose = res ;
43+ rejectAppClose = rej ;
44+ } ) ;
45+
46+ appProcess . on ( 'close' , code => {
47+ if ( code !== 0 ) {
48+ rejectAppClose ( new Error ( `App process exited with code ${ code } ` ) ) ;
49+ } else {
50+ resolveAppClose ( ) ;
51+ }
52+ } ) ;
53+
2454 await new Promise ( ( resolve , reject ) => {
2555 appProcess . stdout . on ( 'data' , data => {
2656 log ( `appProcess: ${ data } ` ) ;
@@ -31,13 +61,15 @@ async function getMeasurements(instrumentFile) {
3161
3262 appProcess . stderr . on ( 'data' , data => {
3363 log ( `appProcess stderr: ${ data } ` ) ;
34- reject ( data ) ;
3564 appProcess . kill ( 'SIGKILL' ) ;
65+ reject ( data ) ;
3666 } ) ;
3767 } ) ;
3868
39- log ( 'Example app listening, running autocannon...' ) ;
69+ return [ appProcess , appClosePromise ] ;
70+ }
4071
72+ async function startAutocannonProcess ( ) {
4173 const autocannon = spawn ( 'yarn test' , {
4274 shell : true ,
4375 cwd : packageRoot ,
@@ -62,11 +94,8 @@ async function getMeasurements(instrumentFile) {
6294
6395 autocannon . on ( 'close' , code => {
6496 log ( `autocannon closed with code ${ code } ` ) ;
65-
6697 log ( `Average requests: ${ lastJson ?. requests . average } ` ) ;
6798
68- appProcess . kill ( 'SIGKILL' ) ;
69-
7099 resolve ( lastJson ?. requests . average ) ;
71100 } ) ;
72101 } ) ;
0 commit comments