Skip to content

initial stab at process.cpuUsage()

Pull Request check-list

Please make sure to review and check all of these items:

  • Does make -j8 test (UNIX) or vcbuild test nosign (Windows) pass with this change (including linting)?
  • Is the commit message formatted according to CONTRIBUTING.md?
  • If this change fixes a bug (or a performance problem), is a regression test (or a benchmark) included?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.

Affected core subsystem(s)

process

Description of change

Adds the function cpuUsage() to the global process object. Proposed doc is in the PR, and immediate below:

process.cpuUsage()

Returns the user and system cpu time usage of the current process, in an object with properties user and system, whose values are arrays of integers as [seconds, microseconds] tuples. These values measure time spent in user and system code respectively, and may end up being greater than actual elapsed time if multiple cpu cores are performing work for this process.

console.log(process.cpuUsage());
// { user: [ 0, 43979 ], system: [ 0, 12423 ] }

setTimeout(() => {
  var startTime = process.hrtime();

  // spin the CPU for 500 milliseconds
  while (true) {
    var time = process.hrtime(startTime);

    // calculate elapsed wall clock time in millis
    var elapsed = time[0] * 1000 + time[1] / 1000 / 1000;

    if (elapsed > 500) break;
  }

  console.log(process.cpuUsage());
  // { user: [ 0, 554966 ], system: [ 0, 14881 ] }
}, 1000);

Merge request reports

Loading