Skip to content

Instantly share code, notes, and snippets.

@jamesseanwright
Last active January 7, 2016 12:24
Show Gist options
  • Save jamesseanwright/886aaaedc5ff7c61447d to your computer and use it in GitHub Desktop.
Save jamesseanwright/886aaaedc5ff7c61447d to your computer and use it in GitHub Desktop.
Rudimentary capturing of CPU busy time in Node.js
'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