11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that --cpu-prof-dir works for workers.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst fs = require('fs');
111cb0ef41Sopenharmony_ciconst path = require('path');
121cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
151cb0ef41Sopenharmony_ciconst {
161cb0ef41Sopenharmony_ci  getCpuProfiles,
171cb0ef41Sopenharmony_ci  kCpuProfInterval,
181cb0ef41Sopenharmony_ci  env,
191cb0ef41Sopenharmony_ci  getFrames
201cb0ef41Sopenharmony_ci} = require('../common/cpu-prof');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// --cpu-prof-dir with worker
231cb0ef41Sopenharmony_ci{
241cb0ef41Sopenharmony_ci  tmpdir.refresh();
251cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
261cb0ef41Sopenharmony_ci    '--cpu-prof-interval',
271cb0ef41Sopenharmony_ci    kCpuProfInterval,
281cb0ef41Sopenharmony_ci    '--cpu-prof-dir',
291cb0ef41Sopenharmony_ci    'prof',
301cb0ef41Sopenharmony_ci    '--cpu-prof',
311cb0ef41Sopenharmony_ci    fixtures.path('workload', 'fibonacci-worker.js'),
321cb0ef41Sopenharmony_ci  ], {
331cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
341cb0ef41Sopenharmony_ci    env
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci  if (output.status !== 0) {
371cb0ef41Sopenharmony_ci    console.log(output.stderr.toString());
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci  assert.strictEqual(output.status, 0);
401cb0ef41Sopenharmony_ci  const dir = path.join(tmpdir.path, 'prof');
411cb0ef41Sopenharmony_ci  assert(fs.existsSync(dir));
421cb0ef41Sopenharmony_ci  const profiles = getCpuProfiles(dir);
431cb0ef41Sopenharmony_ci  assert.strictEqual(profiles.length, 2);
441cb0ef41Sopenharmony_ci  const profile1 = getFrames(profiles[0], 'fibonacci.js');
451cb0ef41Sopenharmony_ci  const profile2 = getFrames(profiles[1], 'fibonacci.js');
461cb0ef41Sopenharmony_ci  if (profile1.frames.length === 0 && profile2.frames.length === 0) {
471cb0ef41Sopenharmony_ci    // Show native debug output and the profile for debugging.
481cb0ef41Sopenharmony_ci    console.log(output.stderr.toString());
491cb0ef41Sopenharmony_ci    console.log('CPU path: ', profiles[0]);
501cb0ef41Sopenharmony_ci    console.log(profile1.nodes);
511cb0ef41Sopenharmony_ci    console.log('CPU path: ', profiles[1]);
521cb0ef41Sopenharmony_ci    console.log(profile2.nodes);
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci  assert(profile1.frames.length > 0 || profile2.frames.length > 0);
551cb0ef41Sopenharmony_ci}
56