Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored agents, added simple test
- Loading branch information
1 parent
3bcadd3
commit 4372fe7
Showing
10 changed files
with
220 additions
and
190 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
node_modules | ||
samples |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var DEFAULT_AMOUNT = Math.pow(10, 5); | ||
|
||
// define the base agent | ||
var BaseAgent = exports.BaseAgent = function() { | ||
}; | ||
|
||
BaseAgent.prototype.roundVal = function(value, decimals) { | ||
// initialise the amount, avoid math if we can | ||
var amount = decimals ? Math.pow(10, decimals) : DEFAULT_AMOUNT; | ||
|
||
return Math.round(value * amount) / amount; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,51 @@ | ||
var os = require('os'); | ||
|
||
module.exports = (function() { | ||
var cpuCores = os.cpus().length, | ||
cpuOld; | ||
/* internals */ | ||
var os = require('os'), | ||
util = require('util'), | ||
BaseAgent = require('../agent').BaseAgent, | ||
coreCount = os.cpus().length; | ||
|
||
|
||
function collect() { | ||
var raw = os.cpus(), | ||
cpu = []; | ||
|
||
for (var ii=0; ii < cpuCores; ii++) { | ||
var times = raw[ii].times, | ||
total = 0; | ||
|
||
for (var key in times) { | ||
total += times[key]; | ||
} // for | ||
function readCpu() { | ||
var raw = os.cpus(), | ||
cpu = []; | ||
|
||
for (var ii = coreCount; ii--; ) { | ||
var times = raw[ii].times, | ||
total = 0; | ||
|
||
cpu[ii] = { | ||
total: total, | ||
idle: times.idle | ||
}; | ||
for (var key in times) { | ||
total += times[key]; | ||
} // for | ||
|
||
return cpu; | ||
} // collect | ||
|
||
function calcLoad(cpuNew) { | ||
var idleDelta, | ||
tickDelta, | ||
totalTick, | ||
load = []; | ||
cpu[ii] = { | ||
total: total, | ||
idle: times.idle | ||
}; | ||
} // for | ||
|
||
return cpu; | ||
}; | ||
|
||
for (var ii=0; ii < cpuCores; ii++) { | ||
tickDelta = cpuNew[ii].total - cpuOld[ii].total; | ||
idleDelta = cpuNew[ii].idle - cpuOld[ii].idle; | ||
|
||
load[ii] = 1 - idleDelta / tickDelta; | ||
cpuOld[ii] = cpuNew[ii]; | ||
} // for | ||
var Agent = exports.Agent = function() { | ||
// set the default frequency to 100ms | ||
this.frequency = 100; | ||
this.lastReading = readCpu(); | ||
}; | ||
|
||
return load; | ||
} // calcLoad | ||
util.inherits(Agent, BaseAgent); | ||
|
||
cpuOld = collect(); | ||
Agent.prototype.run = function(callback) { | ||
var idleDelta, tickDelta, load = [], | ||
reading = readCpu(); | ||
|
||
function run(callback) { | ||
callback(calcLoad(collect())); | ||
} // callback | ||
|
||
/* exports */ | ||
for (var ii = coreCount; ii--; ) { | ||
tickDelta = reading[ii].total - this.lastReading[ii].total; | ||
idleDelta = reading[ii].idle - this.lastReading[ii].idle; | ||
|
||
// calculate the load for the core | ||
load[ii] = this.roundVal(1 - (tickDelta ? idleDelta / tickDelta : 1)); | ||
|
||
// update the last reading | ||
this.lastReading[ii] = reading[ii]; | ||
} // for | ||
|
||
return { | ||
// define the frequency for | ||
frequency: 100, // '* * * * * *', | ||
run: run | ||
}; | ||
|
||
})(); | ||
callback(load); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,19 @@ | ||
var os = require('os'); | ||
|
||
module.exports = (function() { | ||
var data = { | ||
"total" : os.totalmem() | ||
}; | ||
|
||
/* internals */ | ||
function collect() { | ||
data.free = os.freemem(); | ||
|
||
return data; | ||
} | ||
var os = require('os'), | ||
util = require('util'), | ||
BaseAgent = require('../agent').BaseAgent, | ||
totalMem = os.totalmem(); | ||
|
||
var Agent = exports.Agent = function() { | ||
}; | ||
|
||
util.inherits(Agent, BaseAgent); | ||
|
||
function run(callback) { | ||
callback(collect()); | ||
} // callback | ||
|
||
/* exports */ | ||
Agent.prototype.run = function(callback) { | ||
// read the free memory | ||
var free = os.freemem(); | ||
|
||
return { | ||
// define the frequency for | ||
frequency: '* * * * * *', | ||
run: run | ||
}; | ||
})(); | ||
callback(this.roundVal((totalMem - free) / totalMem), { | ||
free: free, | ||
total: totalMem | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.