Skip to content

Instantly share code, notes, and snippets.

@zoka
Created March 19, 2010 18:45
Show Gist options
  • Save zoka/338020 to your computer and use it in GitHub Desktop.
Save zoka/338020 to your computer and use it in GitHub Desktop.
var sys = require("sys"),
fs = require("fs"),
http = require("http"),
url = require("url");
var base_chunk = '01234567890123456789';
// Produce a very large response.
var chargen = http.createServer(function (req, res) {
var len = req.headers['x-len'];
res.writeHead(200, {"transfer-encoding":"chunked"});
for (var i=0; i<len; i++) {
res.write(base_chunk+"-"+i);
}
sys.puts("chargen: sent "+len+" chunks back");
res.close();
});
chargen.listen(9000);
// Proxy to the chargen server.
var proxy = http.createServer(function (req, res) {
var proxy_req = http.createClient(9000, 'localhost')
.request(req.method, req.url, req.headers);
proxy_req.addListener('response', function(proxy_res) {
var counter = 0;
var got_mismatch = false;
res.writeHead(proxy_res.statusCode, proxy_res.headers);
proxy_res.addListener('data', function(chunk) {
var expected=base_chunk+"-"+counter;
if (!got_mismatch && chunk !== expected) {
got_mismatch = true;
sys.puts("proxy: expected:"+expected+" got:"+chunk);
}
res.write(chunk);
counter++;
});
proxy_res.addListener('end', function() {
res.close();
sys.puts("proxy: total chunks "+counter);
});
});
proxy_req.close();
});
proxy.listen(9001);
function call_chargen(list) {
if (list.length > 0) {
sys.debug("calling chargen for " + list[0] + " chunks.");
var req = http.createClient(9001, 'localhost').request('/', {'x-len': list[0]});
req.addListener('response', function(res) {
res.addListener('end', function() {
sys.debug("end for " + list[0] + " chunks.");
list.shift();
call_chargen(list);
});
});
req.close();
}
else {
sys.puts("End of list.");
proxy.close();
chargen.close();
}
}
call_chargen([ 100, 1000, 10000, 100000, 1000000 ]);
DEBUG: calling chargen for 100 chunks.
chargen: sent 100 chunks back
proxy: total chunks 100
DEBUG: end for 100 chunks.
DEBUG: calling chargen for 1000 chunks.
chargen: sent 1000 chunks back
proxy: expected:01234567890123456789-274 got:012345678901
proxy: total chunks 1003
DEBUG: end for 1000 chunks.
DEBUG: calling chargen for 10000 chunks.
chargen: sent 10000 chunks back
proxy: expected:01234567890123456789-274 got:012345678901
proxy: total chunks 10029
DEBUG: end for 10000 chunks.
DEBUG: calling chargen for 100000 chunks.
chargen: sent 100000 chunks back
proxy: expected:01234567890123456789-274 got:012345678901
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment