-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
perf_hooks: add idleTime and event loop util #34938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const assert = require('assert').ok; | ||
const { performance } = require('perf_hooks'); | ||
const { nodeTiming, eventLoopUtilization } = performance; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
method: [ | ||
'idleTime', | ||
'ELU_simple', | ||
'ELU_passed', | ||
], | ||
}); | ||
|
||
function main({ method, n }) { | ||
switch (method) { | ||
case 'idleTime': | ||
benchIdleTime(n); | ||
break; | ||
case 'ELU_simple': | ||
benchELUSimple(n); | ||
break; | ||
case 'ELU_passed': | ||
benchELUPassed(n); | ||
break; | ||
default: | ||
throw new Error(`Unsupported method ${method}`); | ||
} | ||
} | ||
|
||
function benchIdleTime(n) { | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
nodeTiming.idleTime; | ||
bench.end(n); | ||
} | ||
|
||
function benchELUSimple(n) { | ||
// Need to put this in setImmediate or will always return 0. | ||
setImmediate(() => { | ||
const elu = eventLoopUtilization(); | ||
assert(elu.active + elu.idle > 0); | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
eventLoopUtilization(); | ||
bench.end(n); | ||
}); | ||
} | ||
|
||
function benchELUPassed(n) { | ||
// Need to put this in setImmediate or will always return 0. | ||
setImmediate(() => { | ||
let elu = eventLoopUtilization(); | ||
assert(elu.active + elu.idle > 0); | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
elu = eventLoopUtilization(elu); | ||
bench.end(n); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const TIMEOUT = 50; | ||
|
||
const assert = require('assert'); | ||
const { performance } = require('perf_hooks'); | ||
const { Worker, parentPort } = require('worker_threads'); | ||
|
||
const { nodeTiming, eventLoopUtilization } = performance; | ||
const elu = eventLoopUtilization(); | ||
|
||
// Take into account whether this test was started as a Worker. | ||
if (nodeTiming.loopStart === -1) { | ||
assert.strictEqual(nodeTiming.idleTime, 0); | ||
assert.deepStrictEqual(elu, { idle: 0, active: 0, utilization: 0 }); | ||
assert.deepStrictEqual(eventLoopUtilization(elu), | ||
{ idle: 0, active: 0, utilization: 0 }); | ||
assert.deepStrictEqual(eventLoopUtilization(elu, eventLoopUtilization()), | ||
{ idle: 0, active: 0, utilization: 0 }); | ||
} | ||
|
||
// Place in setTimeout() to make sure there is some idle time, but not going to | ||
// assert this since it could make the test flaky. | ||
setTimeout(() => { | ||
const t = Date.now(); | ||
const elu1 = eventLoopUtilization(); | ||
|
||
while (Date.now() - t < 50) { } | ||
|
||
const elu2 = eventLoopUtilization(); | ||
const elu3 = eventLoopUtilization(elu1); | ||
const elu4 = eventLoopUtilization(elu2, elu1); | ||
|
||
assert.strictEqual(elu3.idle, 0); | ||
assert.strictEqual(elu4.idle, 0); | ||
assert.strictEqual(elu3.utilization, 1); | ||
assert.strictEqual(elu4.utilization, 1); | ||
assert.strictEqual(elu2.active - elu1.active, elu4.active); | ||
assert.ok(elu2.active > elu3.active); | ||
assert.ok(elu2.active > elu4.active); | ||
assert.ok(elu3.active > elu4.active); | ||
|
||
setTimeout(runIdleTimeTest, TIMEOUT); | ||
}, 5); | ||
|
||
function runIdleTimeTest() { | ||
const idleTime = nodeTiming.idleTime; | ||
const elu1 = eventLoopUtilization(); | ||
const sum = elu1.idle + elu1.active; | ||
|
||
assert.ok(sum >= elu1.idle && sum >= elu1.active, | ||
`idle: ${elu1.idle} active: ${elu1.active} sum: ${sum}`); | ||
assert.strictEqual(elu1.idle, idleTime); | ||
assert.strictEqual(elu1.utilization, elu1.active / sum); | ||
|
||
setTimeout(runCalcTest, TIMEOUT, elu1); | ||
} | ||
|
||
function runCalcTest(elu1) { | ||
const now = performance.now(); | ||
const elu2 = eventLoopUtilization(); | ||
const elu3 = eventLoopUtilization(elu2, elu1); | ||
const active_delta = elu2.active - elu1.active; | ||
const idle_delta = elu2.idle - elu1.idle; | ||
|
||
assert.ok(elu2.idle >= 0); | ||
assert.ok(elu2.active >= 0); | ||
assert.ok(elu3.idle >= 0); | ||
assert.ok(elu3.active >= 0); | ||
assert.ok(elu2.idle + elu2.active > elu1.idle + elu2.active); | ||
assert.ok(elu2.idle + elu2.active >= now - nodeTiming.loopStart); | ||
assert.strictEqual(elu3.active, elu2.active - elu1.active); | ||
assert.strictEqual(elu3.idle, elu2.idle - elu1.idle); | ||
assert.strictEqual(elu3.utilization, | ||
active_delta / (idle_delta + active_delta)); | ||
|
||
setImmediate(runWorkerTest); | ||
} | ||
|
||
function runWorkerTest() { | ||
// Use argv to detect whether we're running as a Worker called by this test | ||
// vs. this test also being called as a Worker. | ||
if (process.argv[2] === 'iamalive') { | ||
parentPort.postMessage(JSON.stringify(eventLoopUtilization())); | ||
return; | ||
} | ||
|
||
const elu1 = eventLoopUtilization(); | ||
const worker = new Worker(__filename, { argv: [ 'iamalive' ] }); | ||
|
||
worker.on('message', (msg) => { | ||
const elu2 = eventLoopUtilization(elu1); | ||
const data = JSON.parse(msg); | ||
|
||
assert.ok(elu2.active + elu2.idle > data.active + data.idle); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.