Created
April 24, 2015 15:20
-
-
Save umamialex/01d17e3280841c01d75e to your computer and use it in GitHub Desktop.
Memory Logger
This file contains hidden or 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
(function() { | |
'use strict'; | |
var _ = require('lodash'); | |
var moment = require('moment'); | |
var accounting = require('accounting'); | |
var path = require('path'); | |
var mkdirp = require('mkdirp'); | |
var heapdump = require('heapdump'); | |
require('colors'); | |
var bytes = 1048756; | |
var period = 100; | |
var frequency = period / 1000; | |
var duration = 5; | |
var startTime = moment(); | |
var startRss = process.memoryUsage().rss / bytes; | |
var rssHistory = [ 0 ]; | |
var snapshotPath = ''; | |
var memoryThreshold = 0; | |
var logMemory = _.throttle(function(rss, dRss, rssVAvg, rssAAvg, dump) { | |
var now = moment(); | |
var dT = moment.duration(now.diff(startTime)); | |
var dTSeconds = dT.seconds(); | |
var dTMinutes = dT.minutes(); | |
var spacer = ' '; | |
var humanizedDT = dTMinutes + ':' + (dTSeconds < 10 ? '0' + dTSeconds : dTSeconds); | |
var formattedDT = spacer + 'dt: ' + humanizedDT; | |
var formattedRss = spacer + 'R: ' + accounting.toFixed(rss, 3) + ' MB'; | |
var formattedDRss = spacer + 'dR: ' + accounting.toFixed(dRss, 3) + ' MB'; | |
var formattedRssVAvg = spacer + 'Vavg: ' + (rssVAvg <= 0 ? '-' : '+') + accounting.toFixed(Math.abs(rssVAvg), 3) + ' MB/s'; | |
var formattedRssAAvg = spacer + 'Aavg: ' + (rssAAvg <= 0 ? '-' : '+') + accounting.toFixed(Math.abs(rssAAvg), 3) + ' MB/s^2'; | |
var logList = [ now.valueOf().toString().grey.italic ]; | |
logList.push(formattedDT.grey); | |
logList.push(formattedRss); | |
logList.push(formattedDRss.yellow); | |
logList.push(rssVAvg < 0 ? formattedRssVAvg.green : (rssVAvg > 0 ? formattedRssVAvg.red : formattedRssVAvg.grey)); | |
logList.push(rssAAvg < 0 ? formattedRssAAvg.green : (rssAAvg > 0 ? formattedRssAAvg.red : formattedRssAAvg.grey)); | |
console.log.apply(console, logList); | |
if (!dump || dRss < memoryThreshold) { | |
return; | |
} | |
console.log('Snapshot'); | |
heapdump.writeSnapshot(path.join(snapshotPath, humanizedDT + ' - ' + memoryThreshold + 'MB.heapsnapshot')); | |
memoryThreshold += 10; | |
}, 1000); | |
function getMemoryStats(dump) { | |
var memory = process.memoryUsage(); | |
var rss = memory.rss / bytes; | |
var dRss = rss - startRss; | |
rssHistory.push(rss); | |
rssHistory = _.takeRight(rssHistory, Math.round(duration * 1000 / period)); | |
var rssVAvg = (rssHistory[rssHistory.length - 1] - rssHistory[0]) / (rssHistory.length * frequency); | |
var midpoint = Math.round(rssHistory.length / 2); | |
var v1 = (rssHistory[midpoint] - rssHistory[0]) / (midpoint * frequency); | |
var v2 = (rssHistory[rssHistory.length - 1] - rssHistory[midpoint]) / (midpoint * frequency); | |
var rssAAvg = (v2 - v1) / (midpoint * frequency); | |
logMemory(rss, dRss, rssVAvg, rssAAvg, dump); | |
} | |
function start(dump, dirname) { | |
if (dump) { | |
snapshotPath = path.join(dirname, 'heapdump', startTime.format('YYYY-MM-DD HH:mm:ss')); | |
mkdirp.sync(snapshotPath); | |
} | |
getMemoryStats(dump); | |
return setInterval(_.partial(getMemoryStats, dump), period); | |
} | |
module.exports = { | |
startTime: startTime, | |
startRss: startRss, | |
rssHistory: rssHistory, | |
logMemory: logMemory, | |
getMemoryStats: getMemoryStats, | |
start: start | |
}; | |
})(); |
This file contains hidden or 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
{ | |
"name": "memory-logger", | |
"version": "0.1.0", | |
"description": "Logs out RSS memory usage, velocity, and acceleration.", | |
"main": "memory-logger.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Alexander Martin", | |
"license": "MIT", | |
"dependencies": { | |
"accounting": "^0.4.1", | |
"colors": "^1.0.3", | |
"heapdump": "^0.3.5", | |
"lodash": "^3.7.0", | |
"mkdirp": "^0.5.0", | |
"moment": "^2.10.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment