index.js
function getCpuUsage(previousValue, start) {
  if (!previousValue) {
    return null;
  }
  const usage = process.cpuUsage(previousValue);
  const delta = process.hrtime(start);
  const total = Math.ceil(((delta[0] * 1e9) + delta[1]) / 1000);
  const usedPercent = Math.round(((usage.user + usage.system) / total) * 100);
  usage.usedPercent = usedPercent;
  usage.userUsedPercent = Math.round((usage.user / total) * 100);
  usage.systemUsedPercent = Math.round((usage.system / total) * 100);
  usage.total = total;
  return usage;
}
function getCpuUsage(previousValue, start) {
  if (!previousValue) {
    return null;
  }
  const usage = process.cpuUsage(previousValue);
  const delta = process.hrtime(start);
  const total = Math.ceil(((delta[0] * 1e9) + delta[1]) / 1000);
  const usedPercent = Math.round(((usage.user + usage.system) / total) * 100);
  usage.usedPercent = usedPercent;
  usage.userUsedPercent = Math.round((usage.user / total) * 100);
  usage.systemUsedPercent = Math.round((usage.system / total) * 100);
  usage.total = total;
  return usage;
}
function getCpuUsage ( previousValue , start ) { if ( ! previousValue ) { return null ; } const usage = process . cpuUsage ( previousValue ) ; const delta = process . hrtime ( start ) ; const total = Math . ceil ( ( ( delta [ 0 ] * 1e9 ) + delta [ 1 ] ) / 1000 ) ; const usedPercent = Math . round ( ( ( usage . user + usage . system ) / total ) * 100 ) ; usage . usedPercent = usedPercent ; usage . userUsedPercent = Math . round ( ( usage . user / total ) * 100 ) ; usage . systemUsedPercent = Math . round ( ( usage . system / total ) * 100 ) ; usage . total = total ; return usage ; }
Get the cpu usage
@param {Object} previousValue The previous cpu usage
@param {Array} start The previous process.hrtime
@returns {Object} The cpu usage {
user: Number,
system: Number,
usedPercent: Number,
userUsedPercent: Number,
systemUsedPercent: Number,
total: total
}
Get the cpu usage