-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
process: add process.cpuUsage() - implementation, doc, tests
Backport to v4.x Original commit message: Add process.cpuUsage() method that returns the user and system CPU time usage of the current process PR-URL: #6157 Reviewed-By: Robert Lindstaedt <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> PR-URL: #10796 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
- Loading branch information
1 parent
278d718
commit 7a9c8d8
Showing
5 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,67 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const result = process.cpuUsage(); | ||
|
||
// Validate the result of calling with no previous value argument. | ||
validateResult(result); | ||
|
||
// Validate the result of calling with a previous value argument. | ||
validateResult(process.cpuUsage(result)); | ||
|
||
// Ensure the results are >= the previous. | ||
let thisUsage; | ||
let lastUsage = process.cpuUsage(); | ||
for (let i = 0; i < 10; i++) { | ||
thisUsage = process.cpuUsage(); | ||
validateResult(thisUsage); | ||
assert(thisUsage.user >= lastUsage.user); | ||
assert(thisUsage.system >= lastUsage.system); | ||
lastUsage = thisUsage; | ||
} | ||
|
||
// Ensure that the diffs are >= 0. | ||
let startUsage; | ||
let diffUsage; | ||
for (let i = 0; i < 10; i++) { | ||
startUsage = process.cpuUsage(); | ||
diffUsage = process.cpuUsage(startUsage); | ||
validateResult(startUsage); | ||
validateResult(diffUsage); | ||
assert(diffUsage.user >= 0); | ||
assert(diffUsage.system >= 0); | ||
} | ||
|
||
// Ensure that an invalid shape for the previous value argument throws an error. | ||
assert.throws(function() { process.cpuUsage(1); }); | ||
assert.throws(function() { process.cpuUsage({}); }); | ||
assert.throws(function() { process.cpuUsage({ user: 'a' }); }); | ||
assert.throws(function() { process.cpuUsage({ system: 'b' }); }); | ||
assert.throws(function() { process.cpuUsage({ user: null, system: 'c' }); }); | ||
assert.throws(function() { process.cpuUsage({ user: 'd', system: null }); }); | ||
assert.throws(function() { process.cpuUsage({ user: -1, system: 2 }); }); | ||
assert.throws(function() { process.cpuUsage({ user: 3, system: -2 }); }); | ||
assert.throws(function() { | ||
process.cpuUsage({ | ||
user: Number.POSITIVE_INFINITY, | ||
system: 4 | ||
}); | ||
}); | ||
assert.throws(function() { | ||
process.cpuUsage({ | ||
user: 5, | ||
system: Number.NEGATIVE_INFINITY | ||
}); | ||
}); | ||
|
||
// Ensure that the return value is the expected shape. | ||
function validateResult(result) { | ||
assert.notEqual(result, null); | ||
|
||
assert(Number.isFinite(result.user)); | ||
assert(Number.isFinite(result.system)); | ||
|
||
assert(result.user >= 0); | ||
assert(result.system >= 0); | ||
} |
This file contains 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,30 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const start = process.cpuUsage(); | ||
|
||
// Run a busy-loop for specified # of milliseconds. | ||
const RUN_FOR_MS = 500; | ||
|
||
// Define slop factor for checking maximum expected diff values. | ||
const SLOP_FACTOR = 2; | ||
|
||
// Run a busy loop. | ||
const now = Date.now(); | ||
while (Date.now() - now < RUN_FOR_MS); | ||
|
||
// Get a diff reading from when we started. | ||
const diff = process.cpuUsage(start); | ||
|
||
const MICROSECONDS_PER_SECOND = 1000 * 1000; | ||
|
||
// Diff usages should be >= 0, <= ~RUN_FOR_MS millis. | ||
// Let's be generous with the slop factor, defined above, in case other things | ||
// are happening on this CPU. The <= check may be invalid if the node process | ||
// is making use of multiple CPUs, in which case, just remove it. | ||
assert(diff.user >= 0); | ||
assert(diff.user <= SLOP_FACTOR * RUN_FOR_MS * MICROSECONDS_PER_SECOND); | ||
|
||
assert(diff.system >= 0); | ||
assert(diff.system <= SLOP_FACTOR * RUN_FOR_MS * MICROSECONDS_PER_SECOND); |