Last active
January 7, 2016 12:24
-
-
Save jamesseanwright/886aaaedc5ff7c61447d to your computer and use it in GitHub Desktop.
Rudimentary capturing of CPU busy time in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var os = require('os'); | |
var hasWrittenHeader = false; | |
var fileStream = require('fs').createWriteStream('cpuProfile.out'); | |
function buildResults() { | |
var output = ''; | |
var cpus = os.cpus(); | |
var cpu; | |
for (var cpuIndex in cpus) { | |
cpu = cpus[cpuIndex]; | |
output += 'CPU ' + cpuIndex + ': ' + calculateBusyTime(cpu) + '% busy\n'; | |
} | |
return output; | |
} | |
function calculateBusyTime(cpu) { | |
var total = cpu.times.user + cpu.times.sys + cpu.times.idle; | |
var busy = cpu.times.user + cpu.times.sys; | |
return Math.round(busy / total * 100); | |
} | |
module.exports = { | |
captureNext: function captureNext() { | |
var date = new Date().toString(); | |
var results = buildResults(); | |
var output = date + ':\n' + results + '\n\n'; | |
fileStream.write(output); | |
}, | |
writeResults: function writeResults() { | |
fileStream.end(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment