@@ -6,8 +6,42 @@ const PRODUCTION = 'production';
66
77const env = process . env . NODE_ENV || PRODUCTION ;
88
9+ /**
10+ * Calculates a duration between a given moment and now
11+ * @param {Number } now - The current time
12+ * @param {Number } created - The created time to calculate it's duration
13+ * @return {Number }
14+ */
15+ const getDuration = ( now , created ) => now - created
16+
917module . exports = {
1018 env,
1119 isProduction : ( environment = env ) => environment === PRODUCTION ,
12- isUndefined : ( thing ) => [ null , undefined ] . includes ( thing )
20+ isUndefined : ( thing ) => [ null , undefined ] . includes ( thing ) ,
21+ /**
22+ * Returns a monitoring report over the "executionContext" memory usage.
23+ * @param {ExecutionContextMap } executionContextMap The execution map to monitor
24+ * @return {ExecutionMapUsage }
25+ */
26+ monitorMap : ( executionContextMap ) => {
27+ const now = Date . now ( ) ;
28+ const entries = [ ...executionContextMap . values ( ) ]
29+ . filter ( ( { children } ) => ! ! children )
30+ . map ( ( { asyncId, created, children, context = { } } ) => ( {
31+ asyncId,
32+ created,
33+ contextSize : JSON . stringify ( context ) . length ,
34+ duration : getDuration ( now , created ) ,
35+ children : children . map ( ( childId ) => {
36+ const { type, created } = executionContextMap . get ( childId ) ;
37+
38+ return { asyncId : childId , type, created, duration : getDuration ( now , created ) }
39+ } )
40+ } ) ) ;
41+
42+ return {
43+ size : executionContextMap . size ,
44+ entries
45+ }
46+ }
1347} ;
0 commit comments