1
1
'use strict' ;
2
2
3
- const nodeTiming = require ( 'internal/perf/nodetiming' ) ;
4
-
5
- const { now } = require ( 'internal/perf/utils' ) ;
3
+ const {
4
+ constants : {
5
+ NODE_PERFORMANCE_MILESTONE_LOOP_START ,
6
+ } ,
7
+ loopIdleTime,
8
+ milestones,
9
+ } = internalBinding ( 'performance' ) ;
6
10
7
11
function eventLoopUtilization ( util1 , util2 ) {
8
- const ls = nodeTiming . loopStart ;
12
+ // Get the original milestone timestamps that calculated from the beginning
13
+ // of the process.
14
+ return internalEventLoopUtilization (
15
+ milestones [ NODE_PERFORMANCE_MILESTONE_LOOP_START ] / 1e6 ,
16
+ loopIdleTime ( ) ,
17
+ util1 ,
18
+ util2
19
+ ) ;
20
+ }
9
21
10
- if ( ls <= 0 ) {
22
+ function internalEventLoopUtilization ( loopStart , loopIdleTime , util1 , util2 ) {
23
+ if ( loopStart <= 0 ) {
11
24
return { idle : 0 , active : 0 , utilization : 0 } ;
12
25
}
13
26
@@ -17,17 +30,31 @@ function eventLoopUtilization(util1, util2) {
17
30
return { idle, active, utilization : active / ( idle + active ) } ;
18
31
}
19
32
20
- const idle = nodeTiming . idleTime ;
21
- const active = now ( ) - ls - idle ;
33
+ // Using process.hrtime() to get the time from the beginning of the process,
34
+ // and offset it by the loopStart time (which is also calculated from the
35
+ // beginning of the process).
36
+ const now = process . hrtime ( ) ;
37
+ const active = now [ 0 ] * 1e3 + now [ 1 ] / 1e6 - loopStart - loopIdleTime ;
22
38
23
39
if ( ! util1 ) {
24
- return { idle, active, utilization : active / ( idle + active ) } ;
40
+ return {
41
+ idle : loopIdleTime ,
42
+ active,
43
+ utilization : active / ( loopIdleTime + active ) ,
44
+ } ;
25
45
}
26
46
27
- const idle_delta = idle - util1 . idle ;
28
- const active_delta = active - util1 . active ;
29
- const utilization = active_delta / ( idle_delta + active_delta ) ;
30
- return { idle : idle_delta , active : active_delta , utilization } ;
47
+ const idleDelta = loopIdleTime - util1 . idle ;
48
+ const activeDelta = active - util1 . active ;
49
+ const utilization = activeDelta / ( idleDelta + activeDelta ) ;
50
+ return {
51
+ idle : idleDelta ,
52
+ active : activeDelta ,
53
+ utilization,
54
+ } ;
31
55
}
32
56
33
- module . exports = eventLoopUtilization ;
57
+ module . exports = {
58
+ internalEventLoopUtilization,
59
+ eventLoopUtilization,
60
+ } ;
0 commit comments