Skip to content

Commit 4517bb8

Browse files
committed
feat(special-command): Print Performance & Memory Info
1 parent 2bc9025 commit 4517bb8

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
{
6767
"command": "getArgumentReferencesFromCurrentParameter",
6868
"title": "Get Argument References from Current Parameter"
69+
},
70+
{
71+
"command": "printPerformanceMemoryInfo",
72+
"title": "Print Performance & Memory Info"
6973
}
7074
],
7175
"keybindings": [

src/onCompletionAccepted.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ export default (tsApi: { onCompletionAccepted }) => {
116116
resolve(true)
117117
}
118118
})
119-
if (accepted) void vscode.window.showInformationMessage('Completion accepted, see console for details')
119+
if (accepted) {
120+
void vscode.window.showInformationMessage('Completion accepted, see console for details')
121+
console.show(true)
122+
}
120123
},
121124
)
122125
})

src/specialCommands.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ export default () => {
302302
)
303303
})
304304

305+
registerExtensionCommand('printPerformanceMemoryInfo', async () => {
306+
const info = await sendCommand('performanceInfo', {})
307+
if (!info) return
308+
console.dir(info, { depth: 10 })
309+
console.show(true)
310+
})
311+
305312
// registerExtensionCommand('insertImportFlatten', () => {
306313
// // got -> default, got
307314
// type A = ts.Type

typescript/src/ipcTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const triggerCharacterCommands = [
1616
'getExtendedCodeActionEdits',
1717
'getLastResolvedCompletion',
1818
'getArgumentReferencesFromCurrentParameter',
19+
'performanceInfo',
1920
] as const
2021

2122
export type TriggerCharacterCommand = (typeof triggerCharacterCommands)[number]

typescript/src/specialCommands/handle.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,34 @@ export default (
277277
}),
278278
)
279279
}
280+
if (specialCommand === 'performanceInfo') {
281+
const toMb = (bytes: number) => Math.floor(bytes / 1024 / 1024)
282+
283+
const sourceFilesContents = Object.fromEntries(
284+
languageService
285+
.getProgram()!
286+
.getSourceFiles()
287+
.map(x => [x.fileName, x.getFullText().length]),
288+
)
289+
return {
290+
sourceFiles: {
291+
totalFilesNumber: Object.keys(sourceFilesContents).length,
292+
total: toMb(Object.entries(sourceFilesContents).reduce((a, [, length]) => a + length, 0)),
293+
json: toMb(
294+
Object.entries(sourceFilesContents)
295+
.filter(([fileName]) => fileName.endsWith('.json'))
296+
.map(x => x[1])
297+
.reduce((a, b) => a + b, 0),
298+
),
299+
top10: Object.entries(sourceFilesContents)
300+
.sort(([, a], [, b]) => b - a)
301+
.slice(0, 10)
302+
.map(([fileName, size]) => ({ fileName, size: toMb(size), raw: size })),
303+
},
304+
// approx
305+
memoryUsedMb: toMb(process.memoryUsage().heapUsed),
306+
}
307+
}
280308

281309
return null
282310
}

0 commit comments

Comments
 (0)