11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common.js'); 31cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process'); 41cb0ef41Sopenharmony_ciconst path = require('path'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cilet Worker; // Lazy loaded in main 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 91cb0ef41Sopenharmony_ci script: [ 101cb0ef41Sopenharmony_ci 'benchmark/fixtures/require-builtins', 111cb0ef41Sopenharmony_ci 'test/fixtures/semicolon', 121cb0ef41Sopenharmony_ci ], 131cb0ef41Sopenharmony_ci mode: ['process', 'worker'], 141cb0ef41Sopenharmony_ci count: [30], 151cb0ef41Sopenharmony_ci}); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cifunction spawnProcess(script, bench, state) { 181cb0ef41Sopenharmony_ci const cmd = process.execPath || process.argv[0]; 191cb0ef41Sopenharmony_ci while (state.finished < state.count) { 201cb0ef41Sopenharmony_ci const child = spawnSync(cmd, [script]); 211cb0ef41Sopenharmony_ci if (child.status !== 0) { 221cb0ef41Sopenharmony_ci console.log('---- STDOUT ----'); 231cb0ef41Sopenharmony_ci console.log(child.stdout.toString()); 241cb0ef41Sopenharmony_ci console.log('---- STDERR ----'); 251cb0ef41Sopenharmony_ci console.log(child.stderr.toString()); 261cb0ef41Sopenharmony_ci throw new Error(`Child process stopped with exit code ${child.status}`); 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci state.finished++; 291cb0ef41Sopenharmony_ci if (state.finished === 0) { 301cb0ef41Sopenharmony_ci // Finished warmup. 311cb0ef41Sopenharmony_ci bench.start(); 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci if (state.finished === state.count) { 351cb0ef41Sopenharmony_ci bench.end(state.count); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_cifunction spawnWorker(script, bench, state) { 411cb0ef41Sopenharmony_ci const child = new Worker(script); 421cb0ef41Sopenharmony_ci child.on('exit', (code) => { 431cb0ef41Sopenharmony_ci if (code !== 0) { 441cb0ef41Sopenharmony_ci throw new Error(`Worker stopped with exit code ${code}`); 451cb0ef41Sopenharmony_ci } 461cb0ef41Sopenharmony_ci state.finished++; 471cb0ef41Sopenharmony_ci if (state.finished === 0) { 481cb0ef41Sopenharmony_ci // Finished warmup. 491cb0ef41Sopenharmony_ci bench.start(); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci if (state.finished < state.count) { 521cb0ef41Sopenharmony_ci spawnWorker(script, bench, state); 531cb0ef41Sopenharmony_ci } else { 541cb0ef41Sopenharmony_ci bench.end(state.count); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci }); 571cb0ef41Sopenharmony_ci} 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_cifunction main({ count, script, mode }) { 601cb0ef41Sopenharmony_ci script = path.resolve(__dirname, '../../', `${script}.js`); 611cb0ef41Sopenharmony_ci const warmup = 3; 621cb0ef41Sopenharmony_ci const state = { count, finished: -warmup }; 631cb0ef41Sopenharmony_ci if (mode === 'worker') { 641cb0ef41Sopenharmony_ci Worker = require('worker_threads').Worker; 651cb0ef41Sopenharmony_ci spawnWorker(script, bench, state); 661cb0ef41Sopenharmony_ci } else { 671cb0ef41Sopenharmony_ci spawnProcess(script, bench, state); 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci} 70