Created
September 22, 2012 14:10
-
-
Save molnarg/3766279 to your computer and use it in GitHub Desktop.
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
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