11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
61cb0ef41Sopenharmony_ci  dur: [1],
71cb0ef41Sopenharmony_ci  code: ['1', '"string"', 'process.versions', 'process'],
81cb0ef41Sopenharmony_ci});
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifunction spawnProcess(code) {
111cb0ef41Sopenharmony_ci  const cmd = process.execPath || process.argv[0];
121cb0ef41Sopenharmony_ci  const argv = ['-p', code];
131cb0ef41Sopenharmony_ci  return spawn(cmd, argv);
141cb0ef41Sopenharmony_ci}
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cifunction start(state, code, bench, getNode) {
171cb0ef41Sopenharmony_ci  const node = getNode(code);
181cb0ef41Sopenharmony_ci  let stdout = '';
191cb0ef41Sopenharmony_ci  let stderr = '';
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  node.stdout.on('data', (data) => {
221cb0ef41Sopenharmony_ci    stdout += data;
231cb0ef41Sopenharmony_ci  });
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  node.stderr.on('data', (data) => {
261cb0ef41Sopenharmony_ci    stderr += data;
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  node.on('exit', (code) => {
301cb0ef41Sopenharmony_ci    if (code !== 0) {
311cb0ef41Sopenharmony_ci      console.error('------ stdout ------');
321cb0ef41Sopenharmony_ci      console.error(stdout);
331cb0ef41Sopenharmony_ci      console.error('------ stderr ------');
341cb0ef41Sopenharmony_ci      console.error(stderr);
351cb0ef41Sopenharmony_ci      throw new Error(`Error during node startup, exit code ${code}`);
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci    state.throughput++;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    if (state.go) {
401cb0ef41Sopenharmony_ci      start(state, code, bench, getNode);
411cb0ef41Sopenharmony_ci    } else {
421cb0ef41Sopenharmony_ci      bench.end(state.throughput);
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cifunction main({ dur, code }) {
481cb0ef41Sopenharmony_ci  const state = {
491cb0ef41Sopenharmony_ci    go: true,
501cb0ef41Sopenharmony_ci    throughput: 0,
511cb0ef41Sopenharmony_ci  };
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  setTimeout(() => {
541cb0ef41Sopenharmony_ci    state.go = false;
551cb0ef41Sopenharmony_ci  }, dur * 1000);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  bench.start();
581cb0ef41Sopenharmony_ci  start(state, code, bench, spawnProcess);
591cb0ef41Sopenharmony_ci}
60