Comparing version 1.0.0 to 1.0.1
37
index.js
@@ -11,6 +11,7 @@ // | ||
logparser = require('./lib/logparser.js'), | ||
// We always load this proxy, though you can set it to not autostart | ||
requireproxy = require('./lib/proxies/require.proxy.js'), | ||
asyncproxy = require('./lib/proxies/async.proxy.js'), | ||
options = { | ||
appName: null, | ||
proxies: ['async'], | ||
log: { | ||
@@ -38,2 +39,3 @@ path: "./", | ||
log, | ||
isInitialised = false, | ||
emit = function(obj){ | ||
@@ -47,2 +49,7 @@ obj = obj || {}; | ||
module.exports.init = function (args) { | ||
if(isInitialised) { | ||
console.warn("Cleartrace: was already initialised, aborting.") | ||
return false; | ||
} | ||
isInitialised = true; | ||
args = args || {}; | ||
@@ -57,3 +64,4 @@ options = util.def(options, args); | ||
var cpuProfiler = cpu.init(options.cpu), | ||
heapProfiler = heap.init(options.heap); | ||
heapProfiler = heap.init(options.heap), | ||
result = {}; | ||
@@ -77,2 +85,17 @@ // Setup bunyan with a rotating file stream | ||
// Grab any extra proxies | ||
for(var i = 0; i < options.proxies; i += 1) { | ||
var proxyPath = './lib/proxies/' + options.proxies[i] + '.proxy.js'; | ||
try { | ||
fs.accessSync(proxyPath); | ||
} catch (e) { | ||
// Must be external | ||
proxyPath = options.proxies[i]; | ||
} | ||
result[options.proxies[i]] = require(proxyPath); | ||
// Initialise our proxy | ||
result[options.proxies[i]].init(emit); | ||
} | ||
// Setup the require proxy last, so | ||
@@ -82,7 +105,7 @@ // nothing is logged incorrectly | ||
return { | ||
async: asyncproxy.init(emit), | ||
cpu: cpuProfiler, | ||
heap: heapProfiler | ||
}; | ||
result.require = requireproxy; | ||
result.cpu = cpuProfiler; | ||
result.heap = heapProfiler; | ||
return result; | ||
}; | ||
@@ -89,0 +112,0 @@ |
var fs = require('fs'), | ||
minimist = require('minimist'), | ||
util = require('./util.js'), | ||
argv, | ||
@@ -21,3 +22,4 @@ error = function(){ | ||
" -l Limit the results, default is 100, set to 0 for no limit\n" + | ||
" -d Data format, one of 'human' or 'json' default is 'human', use 'json' to get the full trace for each matched entry\n\n" + | ||
" -d Data format, one of 'human' or 'json' default is 'human', use 'json' to get the full trace for each matched entry\n" + | ||
" -r Run a bespoke report instead, eg: -r 'memoryleak', look in the lib/reports directory for available reports\n\n" + | ||
@@ -36,2 +38,4 @@ "NOTE: 'rss' is the 'Resident Set Size' and is used to show how much memory is allocated to a process in RAM. It includes all stack and heap memory; in cleartrace, we record the difference in RSS before and after a given function ran, so this is especially useful for finding memory leaks and memory hungry functions.\n\n" + | ||
}; | ||
// We just read the whole thing into memory | ||
// TODO: would be better if we could stream it, so we don't use so much memory | ||
getLog = function(path){ | ||
@@ -45,16 +49,2 @@ var obj = fs.readFileSync(path, {encoding: 'utf8'}); | ||
}, | ||
// Ref: http://stackoverflow.com/a/14919494/6637332 | ||
humanFormat = function(bytes) { | ||
var thresh = 1024; | ||
if(Math.abs(bytes) < thresh) { | ||
return bytes + ' B'; | ||
} | ||
var units = ['kB','MB','GB','TB','PB','EB','ZB','YB']; | ||
var u = -1; | ||
do { | ||
bytes /= thresh; | ||
++u; | ||
} while(Math.abs(bytes) >= thresh && u < units.length - 1); | ||
return bytes.toFixed(1)+' '+units[u]; | ||
}, | ||
dataFormat = "human", | ||
@@ -73,3 +63,3 @@ showLogLines = function(items){ | ||
if(dataFormat == "human") { | ||
console.log(items[i].appName + "." + items[i].funcName + "\t" + humanFormat(items[i].rssdiff) + "\t" + timeDiff + "ms"); | ||
console.log(items[i].appName + "." + items[i].funcName + "\t" + util.humanFormat(items[i].rssdiff) + "\t" + timeDiff + "ms"); | ||
} else { | ||
@@ -122,3 +112,4 @@ var comma = (i == items.length -1? "": ","); | ||
var fileName = argv._[0] || argv.i, | ||
limit = argv.l || 100; | ||
limit = argv.l || 100, | ||
report = argv.r; | ||
@@ -139,2 +130,8 @@ sort.options.order = argv.o || sort.options.order; | ||
if(report) { | ||
var reportInstance = require('./reports/'+report+'.report.js'); | ||
console.log(reportInstance(obj, dataFormat)); | ||
process.exit(0); | ||
} | ||
// filter | ||
@@ -141,0 +138,0 @@ var result = obj.filter(filter[filter.options.type]); |
@@ -77,2 +77,3 @@ // Proxy for tracking require modules | ||
} | ||
return attachRequireProxy; | ||
}; |
@@ -37,3 +37,17 @@ module.exports = { | ||
}, obj); | ||
} | ||
}, | ||
// Ref: http://stackoverflow.com/a/14919494/6637332 | ||
humanFormat: function(bytes) { | ||
var thresh = 1024; | ||
if(Math.abs(bytes) < thresh) { | ||
return bytes + ' B'; | ||
} | ||
var units = ['kB','MB','GB','TB','PB','EB','ZB','YB']; | ||
var u = -1; | ||
do { | ||
bytes /= thresh; | ||
++u; | ||
} while(Math.abs(bytes) >= thresh && u < units.length - 1); | ||
return bytes.toFixed(1)+' '+units[u]; | ||
} | ||
}; |
{ | ||
"name": "cleartrace", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Identify memory and processor usage of functions and modules in node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,4 +35,5 @@ # Cleartrace | ||
* **gzip** - Should we gzip rotated files, default is true | ||
* **proxy** - An object to configure how the proxy works | ||
* **proxy** - An object to configure how the 'require' proxy works | ||
* **autoStart** - Should we start capturing proxied info straight away, default is true | ||
* **proxies** - An array of other proxies to load, default is ['async'], you can add custom proxies here | ||
@@ -61,3 +62,3 @@ ## Usage | ||
**Note:** This MUST be called inline, ie: don't call it before the callback would normally be called, so stuff like this won't work: | ||
**NOTE:** This MUST be called inline, ie: don't call it before the callback would normally be called, so stuff like this won't work: | ||
@@ -76,20 +77,48 @@ ```javascript | ||
Find top 10 slowest functions | ||
Find top 3 slowest functions | ||
```bash | ||
cleartrace app.log.json -l 10 | ||
cleartrace myApp.log.json -l 3 | ||
``` | ||
Find top 10 memory use functions | ||
Output: | ||
``` | ||
myApp.myFunction 556.0 kB 209ms | ||
myApp.myFunction 528.0 kB 198ms | ||
myApp.myFunction 432.0 kB 128ms | ||
``` | ||
The columns shown are: method, rss, time | ||
Find top 3 memory use functions | ||
```bash | ||
cleartrace app.log.json -s rss -l 10 | ||
cleartrace myApp.log.json -s rss -l 3 | ||
``` | ||
Find top 50 rss memory usage for a particular function ('readFileSync'), show results in JSON format | ||
Output: | ||
``` | ||
myApp.myFunction 1256.0 kB 209ms | ||
myApp.myFunction 1238.2 kB 198ms | ||
myApp.myFunction 1211.4 kB 128ms | ||
``` | ||
Find top 3 rss memory usage for a particular function ('myFunction'), show results in JSON format | ||
```bash | ||
cleartrace app.log.json -s rss -f 'funcName' -n readFileSync -l 50 -d json | ||
cleartrace myApp.log.json -s rss -f 'funcName' -n 'myFunction' -l 3 -d json | ||
``` | ||
Run the tool on the commandline for more details on how to use it. | ||
```json | ||
[ | ||
{"name":"myApp","hostname":"localhost","pid":10815,"level":30,"origin":"object","filename":"/myApp/app.js","funcName":"myFunction","before":{"time":"2016-11-02T05:10:32.108Z","memory":{"rss":64540672,"heapTotal":54915424,"heapUsed":24459040}},"indent":1992,"after":{"time":"2016-11-02T05:10:32.109Z","memory":{"rss":64610304,"heapTotal":54915424,"heapUsed":24527704}},"rssdiff":69632,"appName":"myApp","msg":"","time":"2016-11-02T05:10:32.109Z","v":0}, | ||
{"name":"myApp","hostname":"localhost","pid":10815,"level":30,"origin":"object","filename":"/myApp/app.js","funcName":"myFunction","before":{"time":"2016-11-02T05:10:48.008Z","memory":{"rss":74342400,"heapTotal":54915424,"heapUsed":26868360}},"indent":2943,"after":{"time":"2016-11-02T05:10:48.009Z","memory":{"rss":74412032,"heapTotal":54915424,"heapUsed":26939008}},"rssdiff":69632,"appName":"myApp","msg":"","time":"2016-11-02T05:10:48.009Z","v":0}, | ||
{"name":"myApp","hostname":"localhost","pid":10815,"level":30,"origin":"object","filename":"/myApp/app.js","funcName":"myFunction","before":{"time":"2016-11-02T05:10:34.239Z","memory":{"rss":66646016,"heapTotal":54915424,"heapUsed":26611704}},"indent":2104,"after":{"time":"2016-11-02T05:10:34.239Z","memory":{"rss":66711552,"heapTotal":54915424,"heapUsed":26680256}},"rssdiff":65536,"appName":"myApp","msg":"","time":"2016-11-02T05:10:34.239Z","v":0} | ||
] | ||
``` | ||
As you can see, in JSON format, we get all the details - you can use this to create graphs or export the data for a dashboard, etc... | ||
**NOTE:** Run the tool on the commandline for more details on how to use it, theres also the option to execute bespoke reports, see /lib/reports/memoryleak.report.js for an example. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27307
17
697
121
8