Created
June 11, 2014 13:54
-
-
Save alanhoff/90f0f35b4688fcb7b9b9 to your computer and use it in GitHub Desktop.
This file contains 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
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); |
This file contains 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
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