-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Changes from 5 commits
fbc9666
7562e9b
26e1106
8470076
e0ee863
4b1e9fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it ever possible for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Will ensure tests and comments match that assumption. |
||
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(result.user != null); | ||
assert(result.system != null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use |
||
|
||
assert(Number.isFinite(result.user)); | ||
assert(Number.isFinite(result.system)); | ||
|
||
assert(result.user >= 0); | ||
assert(result.system >= 0); | ||
} |
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); |
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.
Extra space before
=