Skip to content

Commit 6497329

Browse files
AlexTugarevroboquat
authored andcommitted
[server] add cpu profiler
use: kill -s SIGUSR1 <pid>
1 parent 70e1da2 commit 6497329

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

components/server/src/init.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ process.on("SIGUSR2", () => {
5050
});
5151
//#endregion
5252

53+
//#region cpu profile
54+
/**
55+
* Make cpu profile by sending the server process a SIGINFO signal:
56+
* kill -s SIGUSR1 <pid>
57+
*
58+
* ***IMPORTANT***: making the cpu profile costs cpu and ram!
59+
*
60+
* cpu profiles are written to tmp folder and have `.cpuprofile` extension.
61+
* Check server logs for the concrete filename.
62+
*/
63+
64+
import { Session } from "inspector";
65+
import * as fs from "fs";
66+
67+
process.on("SIGUSR1", () => {
68+
const session = new Session();
69+
session.connect();
70+
71+
session.post('Profiler.enable', () => {
72+
session.post('Profiler.start', async () => {
73+
await new Promise(resolve => setTimeout(resolve, 5 * 60_000));
74+
75+
session.post('Profiler.stop', (err, { profile }) => {
76+
// Write profile to disk, upload, etc.
77+
if (!err) {
78+
const filename = path.join(os.tmpdir(), Date.now() + '.cpuprofile');
79+
console.log('preparing cpuprofile: ' + filename);
80+
fs.promises.writeFile(filename, JSON.stringify(profile));
81+
} else {
82+
console.error('failed to cpuprofile: ', err);
83+
}
84+
});
85+
});
86+
});
87+
});
88+
//#endregion
89+
5390
require('reflect-metadata');
5491
// Use asyncIterators with es2015
5592
if (typeof (Symbol as any).asyncIterator === 'undefined') {

0 commit comments

Comments
 (0)