11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst nodeTiming = require('internal/perf/nodetiming'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst { now } = require('internal/perf/utils'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cifunction eventLoopUtilization(util1, util2) { 81cb0ef41Sopenharmony_ci const ls = nodeTiming.loopStart; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci if (ls <= 0) { 111cb0ef41Sopenharmony_ci return { idle: 0, active: 0, utilization: 0 }; 121cb0ef41Sopenharmony_ci } 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci if (util2) { 151cb0ef41Sopenharmony_ci const idle = util1.idle - util2.idle; 161cb0ef41Sopenharmony_ci const active = util1.active - util2.active; 171cb0ef41Sopenharmony_ci return { idle, active, utilization: active / (idle + active) }; 181cb0ef41Sopenharmony_ci } 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci const idle = nodeTiming.idleTime; 211cb0ef41Sopenharmony_ci const active = now() - ls - idle; 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci if (!util1) { 241cb0ef41Sopenharmony_ci return { idle, active, utilization: active / (idle + active) }; 251cb0ef41Sopenharmony_ci } 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci const idle_delta = idle - util1.idle; 281cb0ef41Sopenharmony_ci const active_delta = active - util1.active; 291cb0ef41Sopenharmony_ci const utilization = active_delta / (idle_delta + active_delta); 301cb0ef41Sopenharmony_ci return { idle: idle_delta, active: active_delta, utilization }; 311cb0ef41Sopenharmony_ci} 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_cimodule.exports = eventLoopUtilization; 34