11'use strict' ;
22
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' ) ;
610
711function 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+ }
921
10- if ( ls <= 0 ) {
22+ function internalEventLoopUtilization ( loopStart , loopIdleTime , util1 , util2 ) {
23+ if ( loopStart <= 0 ) {
1124 return { idle : 0 , active : 0 , utilization : 0 } ;
1225 }
1326
@@ -17,17 +30,31 @@ function eventLoopUtilization(util1, util2) {
1730 return { idle, active, utilization : active / ( idle + active ) } ;
1831 }
1932
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 ;
2238
2339 if ( ! util1 ) {
24- return { idle, active, utilization : active / ( idle + active ) } ;
40+ return {
41+ idle : loopIdleTime ,
42+ active,
43+ utilization : active / ( loopIdleTime + active ) ,
44+ } ;
2545 }
2646
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+ } ;
3155}
3256
33- module . exports = eventLoopUtilization ;
57+ module . exports = {
58+ internalEventLoopUtilization,
59+ eventLoopUtilization,
60+ } ;
0 commit comments