-
Notifications
You must be signed in to change notification settings - Fork 30k
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
process.cpuUsage() - implementation, doc, tests #6157
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fbc9666
process.cpuUsage() - implementation, doc, tests
7562e9b
use Float64 vals instead of sec/usec tuples
26e1106
updates from PR review
8470076
changes from PR review
e0ee863
updates from PR review
4b1e9fd
updates from PR review
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 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
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,63 @@ | ||
'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); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it ever possible for
thisUsage.user == lastUsage.user
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, why not? Assuming a fast enough processor that can run this code faster than a microsecond. In fact, the
cpuUsage()
call with no argument (returning cpu usage since process start) could in theory return 0's.Will ensure tests and comments match that assumption.