Skip to content

Instantly share code, notes, and snippets.

@alanhoff
Created June 11, 2014 13:54
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 alanhoff/90f0f35b4688fcb7b9b9 to your computer and use it in GitHub Desktop.
Save alanhoff/90f0f35b4688fcb7b9b9 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var speed = require('./speedometer');
var write = fs.createWriteStream('./dump.bin');
var read = fs.createReadStream('/dev/urandom');
var measure = speed(write, 1000);
measure.on('speed', function(bytes){
console.log(bytes + ' bytes per seconds');
});
read.pipe(write);
var events = require('events');
module.exports = function(writable, interval){
var emitter = new events.EventEmitter();
var stop = false;
var lastBytes = 0;
var sendCalc = function(bytes){
emitter.emit('speed', bytes - lastBytes);
lastBytes = bytes;
};
(function loop(){
if(stop)
return;
var newBytes = writable.bytesWritten;
sendCalc(newBytes);
setTimeout(loop, interval);
})();
writable.on('finished', function(){
stop = true;
sendCalc(writable.bytesWritten);
});
return emitter;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment