11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ciconst { exec, execSync } = require('child_process');
41cb0ef41Sopenharmony_ciconst isWindows = process.platform === 'win32';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst messagesLength = [64, 256, 1024, 4096];
71cb0ef41Sopenharmony_ci// Windows does not support command lines longer than 8191 characters
81cb0ef41Sopenharmony_ciif (!isWindows) messagesLength.push(32768);
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(childProcessExecStdout, {
111cb0ef41Sopenharmony_ci  len: messagesLength,
121cb0ef41Sopenharmony_ci  dur: [5],
131cb0ef41Sopenharmony_ci});
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction childProcessExecStdout({ dur, len }) {
161cb0ef41Sopenharmony_ci  bench.start();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  const maxDuration = dur * 1000;
191cb0ef41Sopenharmony_ci  const cmd = `yes "${'.'.repeat(len)}"`;
201cb0ef41Sopenharmony_ci  const child = exec(cmd, { 'stdio': ['ignore', 'pipe', 'ignore'] });
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  let bytes = 0;
231cb0ef41Sopenharmony_ci  child.stdout.on('data', (msg) => {
241cb0ef41Sopenharmony_ci    bytes += msg.length;
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  setTimeout(() => {
281cb0ef41Sopenharmony_ci    bench.end(bytes);
291cb0ef41Sopenharmony_ci    if (isWindows) {
301cb0ef41Sopenharmony_ci      // Sometimes there's a yes.exe process left hanging around on Windows.
311cb0ef41Sopenharmony_ci      try {
321cb0ef41Sopenharmony_ci        execSync(`taskkill /f /t /pid ${child.pid}`);
331cb0ef41Sopenharmony_ci      } catch {
341cb0ef41Sopenharmony_ci        // This is a best effort kill. stderr is piped to parent for tracing.
351cb0ef41Sopenharmony_ci      }
361cb0ef41Sopenharmony_ci    } else {
371cb0ef41Sopenharmony_ci      child.kill();
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci  }, maxDuration);
401cb0ef41Sopenharmony_ci}
41