0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/test/pummel/test-process-cpuUsage.js
Patrick Mueller 52cb410402 process: add process.cpuUsage() - implementation, doc, tests
Add process.cpuUsage() method that returns the user and system
CPU time usage of the current process

PR-URL: https://github.com/nodejs/node/pull/6157
Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
2016-04-29 16:07:06 -07:00

31 lines
961 B
JavaScript

'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);