Skip to content

Instantly share code, notes, and snippets.

@justmoon
Created June 30, 2011 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justmoon/1055587 to your computer and use it in GitHub Desktop.
Save justmoon/1055587 to your computer and use it in GitHub Desktop.
node-binary/node-chainsaw memory leak demo
var profiler = require('v8-profiler');
var Binary = require('binary');
var EventEmitter = require('events').EventEmitter;
var em = new EventEmitter;
var CHAIN_LENGTH = 5000;
var benchmarkTimer;
Binary.stream(em)
.loop(function (end) {
var vars_ = this.vars;
this
.scan('filler', 'BEGINMSG')
.buffer('cmd', 3)
.word8('num')
.tap(function (vars) {
if (vars.cmd.toString('ascii') == 'end') end();
})
;
})
.tap(function (vars) {
var duration = (new Date).getTime() - benchmarkTimer;
var memory = process.memoryUsage();
//profiler.stopProfiling('genChain');
profiler.takeSnapshot('finalHeap');
console.log("Downloaded "+CHAIN_LENGTH+" blocks in " + duration + "ms");
console.log("Memory usage: "+memory.vsize);
debugger;
})
;
process.nextTick(function () {
benchmarkTimer = (new Date).getTime();
for (var i = 0; i < CHAIN_LENGTH; i++) {
em.emit('data', new Buffer("BEGINMSGcmd\x01" +
"GARBAGEDATAXXXX" +
"BEGINMSGcmd\x02" +
"BEGINMSGcmd\x03", 'ascii'));
}
em.emit('data', new Buffer("BEGINMSGend\x03", 'ascii'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment