11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This benchmark uses `yes` to a create noisy child_processes with varying
51cb0ef41Sopenharmony_ci// output message lengths, and tries to read 8GB of output
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst os = require('os');
81cb0ef41Sopenharmony_ciconst child_process = require('child_process');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst messagesLength = [64, 256, 1024, 4096];
111cb0ef41Sopenharmony_ci// Windows does not support that long arguments
121cb0ef41Sopenharmony_ciif (os.platform() !== 'win32')
131cb0ef41Sopenharmony_ci  messagesLength.push(32768);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
161cb0ef41Sopenharmony_ci  len: messagesLength,
171cb0ef41Sopenharmony_ci  dur: [5],
181cb0ef41Sopenharmony_ci});
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction main({ dur, len }) {
211cb0ef41Sopenharmony_ci  bench.start();
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  const msg = `"${'.'.repeat(len)}"`;
241cb0ef41Sopenharmony_ci  const options = { 'stdio': ['ignore', 'pipe', 'ignore'] };
251cb0ef41Sopenharmony_ci  const child = child_process.spawn('yes', [msg], options);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  let bytes = 0;
281cb0ef41Sopenharmony_ci  child.stdout.on('data', (msg) => {
291cb0ef41Sopenharmony_ci    bytes += msg.length;
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  setTimeout(() => {
331cb0ef41Sopenharmony_ci    if (process.platform === 'win32') {
341cb0ef41Sopenharmony_ci      // Sometimes there's a yes.exe process left hanging around on Windows...
351cb0ef41Sopenharmony_ci      child_process.execSync(`taskkill /f /t /pid ${child.pid}`);
361cb0ef41Sopenharmony_ci    } else {
371cb0ef41Sopenharmony_ci      child.kill();
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci    const gbits = (bytes * 8) / (1024 * 1024 * 1024);
401cb0ef41Sopenharmony_ci    bench.end(gbits);
411cb0ef41Sopenharmony_ci  }, dur * 1000);
421cb0ef41Sopenharmony_ci}
43