1'use strict'; 2 3const common = require('../common.js'); 4const assert = require('assert').ok; 5const { performance } = require('perf_hooks'); 6const { nodeTiming, eventLoopUtilization } = performance; 7 8const bench = common.createBenchmark(main, { 9 n: [1e6], 10 method: [ 11 'idleTime', 12 'ELU_simple', 13 'ELU_passed', 14 ], 15}); 16 17function main({ method, n }) { 18 switch (method) { 19 case 'idleTime': 20 benchIdleTime(n); 21 break; 22 case 'ELU_simple': 23 benchELUSimple(n); 24 break; 25 case 'ELU_passed': 26 benchELUPassed(n); 27 break; 28 default: 29 throw new Error(`Unsupported method ${method}`); 30 } 31} 32 33function benchIdleTime(n) { 34 bench.start(); 35 for (let i = 0; i < n; i++) 36 nodeTiming.idleTime; // eslint-disable-line no-unused-expressions 37 bench.end(n); 38} 39 40function benchELUSimple(n) { 41 // Need to put this in setImmediate or will always return 0. 42 setImmediate(() => { 43 const elu = eventLoopUtilization(); 44 assert(elu.active + elu.idle > 0); 45 46 bench.start(); 47 for (let i = 0; i < n; i++) 48 eventLoopUtilization(); 49 bench.end(n); 50 }); 51} 52 53function benchELUPassed(n) { 54 // Need to put this in setImmediate or will always return 0. 55 setImmediate(() => { 56 let elu = eventLoopUtilization(); 57 assert(elu.active + elu.idle > 0); 58 59 bench.start(); 60 for (let i = 0; i < n; i++) 61 elu = eventLoopUtilization(elu); 62 bench.end(n); 63 }); 64} 65