Friday, January 10, 2014

Node's http-proxy to simulate high latency

For testing a web app under "worse" connectivity conditions than I normally have on my dev machine, I created a proxy to increase latency. With Node's http-proxy this was surprisingly simple:

In a new directory, create app.js like this:
var http = require('http'),    httpProxy = require('http-proxy');
// initialize parametersvar listenPort = 5001,  destinationPort = 5000,  delay = 1000;
// router optionsvar options = {  router: {    '.*': 'localhost:' + destinationPort  }};
// create server and listenvar server = httpProxy.createServer(function(req, res, next) {  var delayedResponse = function() {    next();  };  setTimeout(delayedResponse, delay);}, options);server.listen(listenPort);
Install http-proxy (assuming you are using npm):
npm install http-proxy
And kick it:
node app.js
Making hard coded parameters configurable is left as an exercise to the reader. Hint: process.argv gives you access to command line arguments in Node.

Note that this is *not* simulating low bandwidth, just a sleepy server with high latency; once the delay is over, the proxy will respond with all bandwidth available. Thus, this approach isn't generally suitable to simulate a slow network connection.

No comments:

Post a Comment