From 0782a5b6835ae40fa34b4e175cad91336f65b367 Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Tue, 5 Apr 2016 09:17:48 -0400 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Trevor Norris Reviewed-By: Santiago Gimeno --- doc/api/process.md | 23 +++++++++ src/node.cc | 35 ++++++++++++++ src/node.js | 53 ++++++++++++++++++++ test/parallel/test-process-cpuUsage.js | 67 ++++++++++++++++++++++++++ test/pummel/test-process-cpuUsage.js | 30 ++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 test/parallel/test-process-cpuUsage.js create mode 100644 test/pummel/test-process-cpuUsage.js diff --git a/doc/api/process.md b/doc/api/process.md index 1cb946aa2579c7..efffa85b7b7f5b 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -421,6 +421,29 @@ added: v0.7.2 If `process.connected` is false, it is no longer possible to send messages. +## process.cpuUsage([previousValue]) + +Returns the user and system CPU time usage of the current process, in an object +with properties `user` and `system`, whose values are microsecond values +(millionth of a second). 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. + +The result of a previous call to `process.cpuUsage()` can be passed as the +argument to the function, to get a diff reading. + +```js +const startUsage = process.cpuUsage(); +// { user: 38579, system: 6986 } + +// spin the CPU for 500 milliseconds +const now = Date.now(); +while (Date.now() - now < 500); + +console.log(process.cpuUsage(startUsage)); +// { user: 514883, system: 11226 } +``` + ## process.cwd()