usage
Advanced tools
Comparing version 0.2.4 to 0.2.5
var usage = require('../'); | ||
usage.setKeepHistory(true); | ||
setInterval(function() { | ||
usage.lookup(parseInt(process.argv[2]), function(err, stat) { | ||
var options = { keepHistory: true }; | ||
usage.lookup(parseInt(process.argv[2]), options, function(err, stat) { | ||
@@ -8,0 +8,0 @@ console.log(err, stat); |
@@ -13,8 +13,11 @@ var sysinfo = require('bindings')('sysinfo'); | ||
var keepHistory = false; | ||
var historyCpuUsage = {}; | ||
function usageLookup(pid, callback, useHistory) { | ||
function usageLookup(pid, options, callback) { | ||
useHistory = useHistory || true; | ||
if(typeof options == 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
@@ -45,3 +48,3 @@ if(!sysinfo.IS_SUPPORTED) { | ||
if(historyCpuUsage[pid] && useHistory) { | ||
if(historyCpuUsage[pid] && options.keepHistory) { | ||
usageData['cpu'] = calculateCpuUsageFromHistory(uptime, stat, historyCpuUsage[pid]); | ||
@@ -52,3 +55,3 @@ } else { | ||
if(keepHistory) { | ||
if(options.keepHistory) { | ||
//save totalTime in history | ||
@@ -55,0 +58,0 @@ historyCpuUsage[pid] = { |
{ | ||
"name": "usage", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "simple way to lookup linux process usage", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -38,6 +38,10 @@ [![Build Status](https://travis-ci.org/arunoda/node-usage.png?branch=master)](https://travis-ci.org/arunoda/node-usage) | ||
~~~js | ||
usage.setKeepHistory(true); | ||
var pid = process.pid; | ||
var options = { keepHistory: true } | ||
usage.lookup(pid, options, function(err, result) { | ||
}); | ||
~~~ | ||
you can clear history cached too | ||
you can clear history cache too | ||
~~~js | ||
@@ -44,0 +48,0 @@ usage.clearHistory(pid); //clear history for the given pid |
@@ -25,2 +25,14 @@ var usage = require('../'); | ||
}); | ||
test('valid pid - with keepHistory', function(done) { | ||
var options = { keepHistory: true }; | ||
usage.lookup(process.pid, options, _usageCallback(done, function(err, result) { | ||
assert.equal(err, null); | ||
assert.ok(result.cpu >= 0); | ||
assert.ok(result.memory > 0); | ||
done(); | ||
})); | ||
}); | ||
}); | ||
@@ -27,0 +39,0 @@ |
8721
164
56