test: strengthen test-worker-prof · nodejs/node@352c31c (original) (raw)

1

1

`'use strict';

`

2

``

`-

require('../common');

`

``

2

`+

const common = require('../common');

`

3

3

`const tmpdir = require('../common/tmpdir');

`

4

4

`const fs = require('fs');

`

5

5

`const assert = require('assert');

`

``

6

`+

const util = require('util');

`

6

7

`const { join } = require('path');

`

7

8

`const { spawnSync } = require('child_process');

`

8

``

`-

const { Worker } = require('worker_threads');

`

9

9

``

10

10

`// Test that --prof also tracks Worker threads.

`

11

11

`// Refs: https://github.com/nodejs/node/issues/24016

`

12

12

``

13

13

`if (process.argv[2] === 'child') {

`

14

``

`` -

const spin = `

``

15

``

`-

const start = Date.now();

`

16

``

`-

while (Date.now() - start < 1000);

`

``

14

`+

let files = fs.readdirSync(tmpdir.path);

`

``

15

`+

const plog = files.filter((name) => /.log$/.test(name))[0];

`

``

16

`+

if (plog === undefined) {

`

``

17

`` +

console.error('--prof did not produce a profile log for parent thread!');

``

``

18

`+

process.exit(1);

`

``

19

`+

}

`

``

20

`` +

const pingpong = `

``

``

21

`+

let counter = 0;

`

``

22

`+

const { Worker, parentPort } = require('worker_threads');

`

``

23

`+

parentPort.on('message', (m) => {

`

``

24

`+

if (counter++ === 1024)

`

``

25

`+

process.exit(0);

`

``

26

`+

parentPort.postMessage(

`

``

27

`+

m.toString().split('').reverse().toString().replace(/,/g, ''));

`

``

28

`+

});

`

17

29

`` `;

``

18

``

`-

new Worker(spin, { eval: true });

`

19

``

`-

eval(spin);

`

20

``

`-

return;

`

21

``

`-

}

`

22

30

``

23

``

`-

tmpdir.refresh();

`

24

``

`-

spawnSync(process.execPath, ['--prof', __filename, 'child'],

`

25

``

`-

{ cwd: tmpdir.path });

`

26

``

`-

const files = fs.readdirSync(tmpdir.path);

`

27

``

`-

const logfiles = files.filter((name) => /.log$/.test(name));

`

28

``

`-

assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.

`

``

31

`+

const { Worker } = require('worker_threads');

`

``

32

`+

const data = 'x'.repeat(1024);

`

``

33

`+

const w = new Worker(pingpong, { eval: true });

`

``

34

`+

w.on('message', (m) => {

`

``

35

`+

w.postMessage(m.toString().split('').reverse().toString().replace(/,/g, ''));

`

``

36

`+

});

`

``

37

+

``

38

`+

w.on('exit', common.mustCall(() => {

`

``

39

`+

files = fs.readdirSync(tmpdir.path);

`

``

40

`+

const wlog = files.filter((name) => /.log$/.test(name) && name !== plog)[0];

`

``

41

`+

if (wlog === undefined) {

`

``

42

`` +

console.error('--prof did not produce a profile log' +

``

``

43

`+

' for worker thread!');

`

``

44

`+

process.exit(1);

`

``

45

`+

}

`

``

46

`+

process.exit(0);

`

``

47

`+

}));

`

``

48

`+

w.postMessage(data);

`

``

49

`+

} else {

`

``

50

`+

tmpdir.refresh();

`

``

51

`+

const spawnResult = spawnSync(

`

``

52

`+

process.execPath, ['--prof', __filename, 'child'],

`

``

53

`+

{ cwd: tmpdir.path, encoding: 'utf8' });

`

``

54

`+

assert.strictEqual(spawnResult.stderr.toString(), '',

`

``

55

`` +

`child exited with an error: \

``

``

56

`` +

${util.inspect(spawnResult)}`);

``

``

57

`+

assert.strictEqual(spawnResult.signal, null,

`

``

58

`` +

child exited with signal: ${util.inspect(spawnResult)});

``

``

59

`+

assert.strictEqual(spawnResult.status, 0,

`

``

60

`` +

`child exited with non-zero status: \

``

``

61

`` +

${util.inspect(spawnResult)}`);

``

``

62

`+

const files = fs.readdirSync(tmpdir.path);

`

``

63

`+

const logfiles = files.filter((name) => /.log$/.test(name));

`

``

64

`+

assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.

`

29

65

``

30

``

`-

for (const logfile of logfiles) {

`

31

``

`-

const lines = fs.readFileSync(join(tmpdir.path, logfile), 'utf8').split('\n');

`

32

``

`-

const ticks = lines.filter((line) => /^tick,/.test(line)).length;

`

``

66

`+

for (const logfile of logfiles) {

`

``

67

`+

const lines = fs.readFileSync(

`

``

68

`+

join(tmpdir.path, logfile), 'utf8').split('\n');

`

``

69

`+

const ticks = lines.filter((line) => /^tick,/.test(line)).length;

`

33

70

``

34

``

`-

// Test that at least 15 ticks have been recorded for both parent and child

`

35

``

`-

// threads. When not tracking Worker threads, only 1 or 2 ticks would

`

36

``

`-

// have been recorded.

`

37

``

`-

// When running locally on x64 Linux, this number is usually at least 700

`

38

``

`-

// for both threads, so 15 seems like a very safe threshold.

`

39

``

`` -

assert(ticks >= 15, ${ticks} >= 15);

``

``

71

`+

// Test that at least 15 ticks have been recorded for both parent and child

`

``

72

`+

// threads. When not tracking Worker threads, only 1 or 2 ticks would

`

``

73

`+

// have been recorded.

`

``

74

`+

// When running locally on x64 Linux, this number is usually at least 200

`

``

75

`+

// for both threads, so 15 seems like a very safe threshold.

`

``

76

`` +

assert(ticks >= 15, ${ticks} >= 15);

``

``

77

`+

}

`

40

78

`}

`