1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const assert = require('assert');
const { Session } = require('inspector');
const session = new Session();
function compareIgnoringOrder(array1, array2) {
const set = new Set(array1);
const test = set.size === array2.length && array2.every((el) => set.has(el));
assert.ok(test, `[${array1}] differs from [${array2}]`);
}
function post(message, data) {
return new Promise((resolve, reject) => {
session.post(message, data, (err, result) => {
if (err)
reject(new Error(JSON.stringify(err)));
else
resolve(result);
});
});
}
function generateTrace() {
return new Promise((resolve) => setTimeout(() => {
for (let i = 0; i < 1000000; i++) {
'test' + i;
}
resolve();
}, 1));
}
async function test() {
// This interval ensures Node does not terminate till the test is finished.
// Inspector session does not keep the node process running (e.g. it does not
// have async handles on the main event loop). It is debatable whether this
// should be considered a bug, and there are no plans to fix it atm.
const interval = setInterval(() => {}, 5000);
session.connect();
let traceNotification = null;
let tracingComplete = false;
session.on('NodeTracing.dataCollected', (n) => traceNotification = n);
session.on('NodeTracing.tracingComplete', () => tracingComplete = true);
const { categories } = await post('NodeTracing.getCategories');
compareIgnoringOrder(['node', 'node.async', 'node.bootstrap', 'node.fs.sync',
'node.perf', 'node.perf.usertiming',
'node.perf.timerify', 'v8'],
categories);
const traceConfig = { includedCategories: ['v8'] };
await post('NodeTracing.start', { traceConfig });
for (let i = 0; i < 5; i++)
await generateTrace();
JSON.stringify(await post('NodeTracing.stop', { traceConfig }));
session.disconnect();
assert(traceNotification.data.value.length > 0);
assert(tracingComplete);
clearInterval(interval);
console.log('Success');
}
common.crashOnUnhandledRejection();
test();
|