forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-module-print-timing.mjs
More file actions
155 lines (128 loc) · 4.33 KB
/
test-module-print-timing.mjs
File metadata and controls
155 lines (128 loc) · 4.33 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import '../common/index.mjs';
import assert from 'node:assert';
import { readFile } from 'node:fs/promises';
import { it } from 'node:test';
import tmpdir from '../common/tmpdir.js';
import { spawnSyncAndAssert } from '../common/child_process.js';
tmpdir.refresh();
it('should print the timing information for cjs', () => {
const result = spawnSyncAndAssert(process.execPath, ['--eval', 'require("url");'], {
cwd: tmpdir.path,
env: {
...process.env,
NODE_DEBUG: 'module_timer',
},
}, {
stdout: '',
stderr: /MODULE_TIMER/g,
});
const firstLine = result.stderr.split('\n').find((line) => line.includes('[url]'));
assert.notStrictEqual(firstLine, undefined);
assert.ok(firstLine.includes('MODULE_TIMER'), `Not found MODULE_TIMER on ${firstLine}`);
assert.ok(firstLine.includes('[url]:'), `Not found [url]: on ${firstLine}`);
assert.ok(firstLine.endsWith('ms'), `Not found ms on ${firstLine}`);
});
it('should write tracing information for cjs', async () => {
const outputFile = tmpdir.resolve('output-trace.log');
spawnSyncAndAssert(process.execPath, [
'--trace-event-categories',
'node.module_timer',
'--trace-event-file-pattern',
outputFile,
'--eval',
'require("url");',
], {
cwd: tmpdir.path,
}, {
stdout: '',
stderr: '',
});
const expectedMimeTypes = ['b', 'e'];
const outputFileContent = await readFile(outputFile, 'utf-8');
const outputFileJson = JSON.parse(outputFileContent).traceEvents;
const urlTraces = outputFileJson.filter((trace) => trace.name === "require('url')");
assert.strictEqual(urlTraces.length, 2);
for (const trace of urlTraces) {
assert.strictEqual(trace.ph, expectedMimeTypes.shift());
}
});
it('should write tracing & print logs for cjs', async () => {
const outputFile = tmpdir.resolve('output-trace-and-log.log');
const result = spawnSyncAndAssert(process.execPath, [
'--trace-event-categories',
'node.module_timer',
'--trace-event-file-pattern',
outputFile,
'--eval',
'require("url");',
], {
cwd: tmpdir.path,
env: {
...process.env,
NODE_DEBUG: 'module_timer',
},
}, {
stdout: '',
stderr: /MODULE_TIMER/g,
});
const firstLine = result.stderr.split('\n').find((line) => line.includes('[url]'));
assert.notStrictEqual(firstLine, undefined);
assert.ok(firstLine.includes('MODULE_TIMER'), `Not found MODULE_TIMER on ${firstLine}`);
assert.ok(firstLine.includes('[url]:'), `Not found [url]: on ${firstLine}`);
assert.ok(firstLine.endsWith('ms'), `Not found ms on ${firstLine}`);
const expectedMimeTypes = ['b', 'e'];
const outputFileContent = await readFile(outputFile, 'utf-8');
const outputFileJson = JSON.parse(outputFileContent).traceEvents;
const urlTraces = outputFileJson.filter((trace) => trace.name === "require('url')");
assert.ok(urlTraces.length > 0, 'Not found url traces');
for (const trace of urlTraces) {
assert.strictEqual(trace.ph, expectedMimeTypes.shift());
}
});
it('should support enable tracing dynamically', async () => {
try {
spawnSyncAndAssert(process.execPath, [
'--eval',
'require("trace_events")',
], {
stdout: '',
stderr: '',
});
} catch {
// Skip this test if the trace_events module is not available
return;
}
const outputFile = tmpdir.resolve('output-dynamic-trace.log');
const jsScript = `
const traceEvents = require("trace_events");
const tracing = traceEvents.createTracing({ categories: ["node.module_timer"] });
tracing.enable();
require("http");
tracing.disable();
require("vm");
`;
spawnSyncAndAssert(process.execPath, [
'--trace-event-file-pattern',
outputFile,
'--eval',
jsScript,
], {
cwd: tmpdir.path,
env: {
...process.env,
},
}, {
stdout: '',
stderr: '',
});
const expectedMimeTypes = ['b', 'e'];
const outputFileContent = await readFile(outputFile, 'utf-8');
const outputFileJson = JSON.parse(outputFileContent).traceEvents;
const httpTraces = outputFileJson.filter((trace) => trace.name === "require('http')");
assert.ok(httpTraces.length > 0, 'Not found http traces');
for (const trace of httpTraces) {
assert.strictEqual(trace.ph, expectedMimeTypes.shift());
}
const vmTraces = outputFileJson.filter((trace) => trace.name === "require('vm')");
assert.strictEqual(vmTraces.length, 0);
});