@@ -50,6 +50,43 @@ process.on("SIGUSR2", () => {
50
50
} ) ;
51
51
//#endregion
52
52
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
+
53
90
require ( 'reflect-metadata' ) ;
54
91
// Use asyncIterators with es2015
55
92
if ( typeof ( Symbol as any ) . asyncIterator === 'undefined' ) {
0 commit comments