Skip to content

Instantly share code, notes, and snippets.

@molnarg
Created September 22, 2012 14:10
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 molnarg/3766279 to your computer and use it in GitHub Desktop.
Save molnarg/3766279 to your computer and use it in GitHub Desktop.
var http = require('http'),
fs = require('fs')
var start = +new Date
var data = 0
var last_calculation = 0
var written = 0
var calculate = function() {
var elapsed = new Date - last_calculation;
console.log('speed', data/elapsed + ' kBps')
console.log('data', data/1000 + ' kB')
console.log('sum', written/1000 + ' kB')
data = 0
last_calculation = +new Date
}
http.createServer(function (req, res) {
var socket = req.connection
res.on('drain', function() {
console.log('drain', new Date - start);
})
// Proxying write
var write = res.write;
res.write = function(buffer) {
data += buffer.length;
written += buffer.length;
var flushed = write.apply(this, arguments)
console.log('writing', buffer.length, socket.bufferSize, flushed, new Date - start)
if (!flushed) calculate()
;
return flushed
}
// Proxying end
var end = res.end;
res.end = function() {
console.log('end')
return end.apply(this, arguments)
}
res.writeHead(200, {'Content-Type': 'text/plain'})
fs.createReadStream(process.argv[2]).pipe(res)
}).listen(1337, '127.0.0.1')
console.log('Server running at http://127.0.0.1:1337/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment