• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const fs = require('fs');
4const cp = require('child_process');
5const path = require('path');
6
7const tmpdir = require('../common/tmpdir');
8tmpdir.refresh();
9
10const LOG_FILE = path.join(tmpdir.path, 'tick-processor.log');
11const RETRY_TIMEOUT = 150;
12
13function runTest(test) {
14  const proc = cp.spawn(process.execPath, [
15    '--no_logfile_per_isolate',
16    '--logfile=-',
17    '--prof',
18    '-pe', test.code,
19  ], {
20    stdio: [ 'ignore', 'pipe', 'inherit' ]
21  });
22
23  let ticks = '';
24  proc.stdout.on('data', (chunk) => ticks += chunk);
25
26  // Try to match after timeout
27  setTimeout(() => {
28    match(test.pattern, proc, () => ticks, test.profProcessFlags);
29  }, RETRY_TIMEOUT);
30}
31
32function match(pattern, parent, ticks, flags = []) {
33  // Store current ticks log
34  fs.writeFileSync(LOG_FILE, ticks());
35
36  const proc = cp.spawn(process.execPath, [
37    '--prof-process',
38    '--call-graph-size=10',
39    ...flags,
40    LOG_FILE,
41  ], {
42    stdio: [ 'ignore', 'pipe', 'inherit' ]
43  });
44
45  let out = '';
46
47  proc.stdout.on('data', (chunk) => out += chunk);
48  proc.stdout.once('end', () => {
49    proc.once('exit', () => {
50      fs.unlinkSync(LOG_FILE);
51
52      // Retry after timeout
53      if (!pattern.test(out))
54        return setTimeout(() => match(pattern, parent, ticks), RETRY_TIMEOUT);
55
56      parent.stdout.removeAllListeners();
57      parent.kill();
58    });
59
60    proc.stdout.removeAllListeners();
61    proc.kill();
62  });
63}
64
65exports.runTest = runTest;
66