-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
197 lines (171 loc) · 6.31 KB
/
index.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// @ts-check
const serverHarness = require("@typescript/server-harness");
const fs = require("fs");
const path = require("path");
const process = require("process");
const { performance } = require("perf_hooks");
const testDir = path.join(__dirname, "reproProject");
// Needed for excludedDirectories
process.chdir(testDir);
main().catch(e => console.error(e));
async function main() {
const server = serverHarness.launchServer(
path.join(__dirname, "node_modules", "typescript", "lib", "tsserver.js"),
// Arguments to tsserver.js
[
// ATA generates some extra network traffic and isn't usually relevant when profiling
"--disableAutomaticTypingAcquisition",
// Enable this if you're emulating VS
// "--suppressDiagnosticEvents",
// Produce a performance trace
"--traceDirectory", path.join(__dirname, "traces"),
// Produce a server log
"--logVerbosity", "verbose",
"--logFile", path.join(__dirname, "logs", "tsserver.PID.log"),
],
// Arguments to node
[
// Enable this to debug the server process (not the driver process)
// "--inspect-brk=9230",
// Generate time and heap profiles (see https://github.com/jakebailey/pprof-it for config options)
// Disable logging if profiling - their cleanup handlers conflict
// Disable tracing if profiling - it causes unrealistic slowdowns
// `--require=${path.join(__dirname, "node_modules", "pprof-it", "dist", "index.js")}`,
// Increasing the heap size is just generally a good idea
"--max-old-space-size=4096",
// This will enable some GC output in the server log
"--expose-gc"
],
// Environment variables for server process (mostly useful for pprof-it)
{
"PPROF_OUT": path.join(__dirname, "profiles")
});
server.on("exit", code => console.log(code ? `Exited with code ${code}` : `Terminated`));
server.on("event", e => console.log(e));
let seq = 1;
// Always start with a `configure` message (possibly preceded by `status` if emulating VS)
await server.message({
"seq": seq++,
"type": "request",
"command": "configure",
"arguments": {
"preferences": {
"includePackageJsonAutoImports": "auto"
},
"watchOptions": {
"excludeDirectories": ["**/node_modules"]
}
}
});
// Open a file
let start = performance.now();
const openFilePath = path.join(testDir, "src", "analyze-trace-utilities.ts");
const updateOpenResponse = await server.message({
"seq": seq++,
"type": "request",
"command": "updateOpen",
"arguments": {
"changedFiles": [],
"closedFiles": [],
"openFiles": [
{
"file": openFilePath,
"fileContent": await fs.promises.readFile(openFilePath, { encoding: "utf-8" }),
"projectRootPath": testDir,
"scriptKindName": "TS", // It's easy to get this wrong when copy-pasting
}
]
}
});
let end = performance.now();
// Note: reporting the time like this is redundant if you're also collecting a trace
console.log(`Initial file open took ${Math.round(end - start)} ms including ${Math.round(updateOpenResponse.performanceData.updateGraphDurationMs)} ms of update graph time`);
// Request squiggles
// Note: the response is uninteresting - the actual diagnostics are in events
start = performance.now();
await server.message({
"seq": seq++,
"type": "request",
"command": "geterr",
"arguments": {
"delay": 0,
"files": [openFilePath],
}
});
end = performance.now();
console.log(`Computing squiggles took ${Math.round(end - start)} ms`);
// Edit the file, introducing an error
// - import fs = require("fs");
// + import fs = require("fs1");
await server.message({
"seq": seq++,
"type": "request",
"command": "updateOpen",
"arguments": {
"closedFiles": [],
"openFiles": [],
"changedFiles": [
{
"fileName": openFilePath,
"textChanges": [{ "newText": "1", "start": { "line": 4, "offset": 24 }, "end": { "line": 4, "offset": 24 } }],
}
],
}
});
start = performance.now();
await server.message({
"seq": seq++,
"type": "request",
"command": "geterr",
"arguments": {
"delay": 0,
"files": [openFilePath],
}
});
end = performance.now();
console.log(`Computing squiggles (with error) took ${Math.round(end - start)} ms`);
// Revert edit
await server.message({
"seq": seq++,
"type": "request",
"command": "updateOpen",
"arguments": {
"closedFiles": [],
"openFiles": [],
"changedFiles": [
{
"fileName": openFilePath,
"textChanges": [{ "newText": "", "start": { "line": 4, "offset": 24 }, "end": { "line": 4, "offset": 25 } }],
}
],
}
});
try {
// Find all references to `normalizePositions`
start = performance.now();
const references = await server.message({
"seq": seq++,
"type": "request",
"command": "references",
"arguments": {
"file": openFilePath,
"line": 7,
"offset": 8
}
});
end = performance.now();
console.log(`Found ${references.body.refs.length} references in ${Math.round(end - start)} ms`);
}
catch (err) {
console.error("Caught " + err);
}
// Tell the server to shut down
// await server.message({ "seq": seq++, "command": "exit" });
// console.log("Killing");
// server.kill();
// console.log("Waiting");
// const sss = performance.now();
// while (performance.now() - sss < 2000) { }
console.log("Exiting");
console.log(await server.exitOrKill(1));
}