Skip to content

Instantly share code, notes, and snippets.

@david-martin
Last active February 8, 2017 14:18
Show Gist options
  • Save david-martin/2355ef4ecc3dc26186cb1c89b1e1bb81 to your computer and use it in GitHub Desktop.
Save david-martin/2355ef4ecc3dc26186cb1c89b1e1bb81 to your computer and use it in GitHub Desktop.
const fs = require('fs');
function readStat(cb) {
fs.readFile("/proc/" + process.pid + "/stat", function(err, data) {
if (err) {
return cb(err);
}
var elems = data.toString().split(' ');
var utime = parseInt(elems[13]);
var stime = parseInt(elems[14]);
console.log('utime', utime, 'stime', stime);
return cb(null, utime + stime);
});
}
readStat(function(err, t1) {
setTimeout(function() {
readStat(function(err, t2) {
console.log('t1', t1, 't2', t2);
console.log('cpu percentage', 100 * ((t2 - t1) / 10000));
});
}, 1000);
var j = 0;
for(var i = 0, l = 10000000000; i<l; i++) {
j++;
}
console.log('j', j);
});
const fs = require('fs');
function readStat(cb) {
fs.readFile("/proc/" + process.pid + "/stat", function(err, data) {
if (err) {
return cb(err);
}
var timeOfRead = Date.now();
var elems = data.toString().split(' ');
var utime = parseInt(elems[13]);
var stime = parseInt(elems[14]);
console.log('utime', utime, 'stime', stime);
return cb(null, utime + stime, timeOfRead);
});
}
readStat(function(err, t1, t1TimeOfRead) {
setTimeout(function() {
readStat(function(err, t2, t2TimeOfRead) {
var secondsBetweenReads = (t2TimeOfRead - t1TimeOfRead)/1000;
console.log('t1', t1, 't2', t2, 'secondsBetweenReads', secondsBetweenReads);
console.log('cpu percentage', 100 * ((t2 - t1) / (10000 * secondsBetweenReads)));
});
}, 1000);
var j = 0;
for(var i = 0, l = 10000000000; i<l; i++) {
j++;
}
console.log('j', j);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment