express-watcher
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -12,7 +12,7 @@ var express = require('express') | ||
// example with callback function | ||
// app.use(function(req, res, next){ | ||
// resourceMonitorMiddlewareCB(req, res, next, function(diffJson){ | ||
// console.log(' diffJson : ', diffJson) | ||
// }) | ||
// }) | ||
app.use(function(req, res, next){ | ||
resourceMonitorMiddlewareCB(req, res, next, function(diffJson){ | ||
console.log(' diffJson : ', diffJson) | ||
}) | ||
}) | ||
@@ -19,0 +19,0 @@ |
147
index.js
var time0 = (new Date()).getTime() | ||
var m0 = process.memoryUsage() | ||
var c0 = process.cpuUsage() | ||
var mMAX = process.memoryUsage() | ||
var cMAX = process.cpuUsage() | ||
const jm = require('js-meter') | ||
var diffHandle = function(m0, m1, diffCPU, diffTime){ | ||
const isPrint = true | ||
const isMs = true // or Second | ||
const isKb = true // or Mb | ||
const MAX = {} | ||
var diffJson = {} | ||
if(diffTime) diffJson.diffTime = diffTime /1000 | ||
diffJson.diffRss = (m1['rss'] - m0['rss']) / 1048576 | ||
diffJson.diffHeapTotal = (m1['heapTotal'] - m0['heapTotal']) / 1048576 | ||
diffJson.diffHeapUsed = (m1['heapUsed'] - m0['heapUsed']) / 1048576 | ||
diffJson.diffExternal = (m1['external'] - m0['external']) / 1048576 | ||
diffJson.diffCpu = (diffCPU.user + diffCPU.system) /1000000 | ||
return diffJson | ||
const printAllTimeHigh = (MAX) => { | ||
var mUnit = (isKb) ? 'kb' : 'mb' | ||
var tUnit = (isMs) ? 'ms' : 's' | ||
console.log('\n *_*_*_* All Time High *_*_*_*') | ||
console.log('RAM : ', MAX.diffRAM , mUnit) | ||
console.log('HeapTotal : ', MAX.diffHeapTotal , mUnit) | ||
console.log('HeapUsed : ', MAX.diffHeapUsed , mUnit) | ||
console.log('External : ', MAX.diffExternal , mUnit) | ||
console.log('CPU : ', MAX.diffCPU, tUnit) | ||
console.log('Spend time : ', MAX.diffTime, tUnit) | ||
} | ||
var printDiffJson = function(diffJson){ | ||
if(diffJson.diffTime) | ||
console.log('Time : ', diffJson.diffTime, 's') | ||
console.log('RAM : ', diffJson.diffRss, 'mb') | ||
console.log('HeapTotal : ', diffJson.diffHeapTotal, 'mb') | ||
console.log('HeapUsed : ', diffJson.diffHeapUsed, 'mb') | ||
console.log('External : ', diffJson.diffExternal, 'mb') | ||
console.log('CPU : ', diffJson.diffCpu, 's') | ||
console.log('\n') | ||
} | ||
var updateMAX = function(){ | ||
// var rss0 = m0.rss; | ||
var m1 = process.memoryUsage() | ||
var c1 = process.cpuUsage() | ||
for (var key in mMAX) { | ||
var t0 = mMAX[key] | ||
var t1 = m1[key] | ||
mMAX[key] = (t1>t0) ? t1 : t0 | ||
} | ||
for (var key in cMAX) { | ||
var t0 = cMAX[key] | ||
var t1 = c1[key] | ||
cMAX[key] = (t1>t0) ? t1 : t0 | ||
} | ||
} | ||
// process.stdin.resume();//so the program will not close instantly | ||
function exitHandler(options, err) { | ||
var printAllTimeHight = function(){ | ||
var diffCPU = process.cpuUsage(c0) | ||
var diffJson = diffHandle(m0, mMAX, diffCPU) | ||
console.log('\n *_*_*_* All Time High *_*_*_*') | ||
printDiffJson(diffJson) | ||
} | ||
// if (options.cleanup){ | ||
@@ -69,7 +28,7 @@ // printMem(mMAX, true) | ||
if (err){ | ||
printAllTimeHigh(MAX) | ||
console.log(err.stack); | ||
printAllTimeHight() | ||
} | ||
if (options.exit){ | ||
printAllTimeHight() | ||
printAllTimeHigh(MAX) | ||
process.exit(); | ||
@@ -89,31 +48,22 @@ } | ||
var checkMax = (meter) => { | ||
for (var key in meter) { | ||
if(!MAX[key]) | ||
MAX[key] = meter[key] | ||
else if(meter[key] > MAX[key]) | ||
MAX[key] = meter[key] | ||
} | ||
} | ||
var resourceMonitorMiddleware = function(req, res, next){ | ||
res.locals['time'] = (new Date()).getTime() | ||
res.locals['mem'] = process.memoryUsage() | ||
res.locals['cpu'] = process.cpuUsage() | ||
var _startTime = res.locals['time'] | ||
var _m0 = res.locals['mem'] | ||
var _c0 = res.locals['cpu'] | ||
var temp = res.send | ||
const m = new jm({isPrint, isMs, isKb}) | ||
const temp = res.send | ||
res.send = function() { | ||
console.log(`*_*_*_*_*_* ${req.url}`) | ||
const meter = m.stop() | ||
var _m1 = process.memoryUsage() | ||
var _endTime = (new Date()).getTime() | ||
var diffCPU = process.cpuUsage(_c0) | ||
var diffTime = _endTime - _startTime | ||
if(_m1 && _m0) { | ||
var diffJson = diffHandle(_m0, _m1, diffCPU, diffTime) | ||
printDiffJson(diffJson) | ||
// if(cb) cb(diffJson) | ||
} | ||
updateMAX() | ||
checkMax(meter) | ||
temp.apply(this,arguments); | ||
} | ||
updateMAX() | ||
next() | ||
@@ -124,32 +74,13 @@ } | ||
res.locals['time'] = (new Date()).getTime() | ||
res.locals['mem'] = process.memoryUsage() | ||
res.locals['cpu'] = process.cpuUsage() | ||
var _startTime = res.locals['time'] | ||
var _m0 = res.locals['mem'] | ||
var _c0 = res.locals['cpu'] | ||
var temp = res.send | ||
const m = new jm({isPrint, isMs, isKb}) | ||
const temp = res.send | ||
res.send = function() { | ||
console.log(`*_*_*_*_*_* ${req.url}`) | ||
const meter = m.stop() | ||
var _m1 = process.memoryUsage() | ||
var _endTime = (new Date()).getTime() | ||
var diffCPU = process.cpuUsage(_c0) | ||
var diffTime = _endTime - _startTime | ||
if(_m1 && _m0) { | ||
var diffJson = diffHandle(_m0, _m1, diffCPU, diffTime) | ||
console.log('*_*_*_*_*_*_*_*_*_*_*_*\n', req.originalUrl) | ||
printDiffJson(diffJson) | ||
if(cb) cb(diffJson) | ||
} | ||
updateMAX() | ||
if(cb) cb(meter) | ||
checkMax(meter) | ||
temp.apply(this,arguments); | ||
} | ||
updateMAX() | ||
next() | ||
@@ -156,0 +87,0 @@ } |
{ | ||
"name": "express-watcher", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "It is a middleware of monitoring Express router, this middleware can be used either globally or on a per-route basis and sends information of response time, used memory and CPU.", | ||
@@ -38,4 +38,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"express": "^4.15.3" | ||
"express": "^4.15.3", | ||
"js-meter": "^1.0.0" | ||
} | ||
} |
6652
2
92
+ Addedjs-meter@^1.0.0
+ Addedjs-meter@1.1.4(transitive)