Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Created July 6, 2015 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cmawhorter/2fb412a799f474b4e721 to your computer and use it in GitHub Desktop.
Save cmawhorter/2fb412a799f474b4e721 to your computer and use it in GitHub Desktop.
node http-proxy pr perf test
process.title = 'node http-proxy';
var CONCURRENCY = 250;
var assert = require('assert')
, crypto = require('crypto')
, http = require('http');
http.globalAgent.maxSockets = Infinity;
var httpProxy = require('./index.js');
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8082'
});
proxy.on('proxyRes', function (proxyRes, req, res) {
proxyRes.abort();
res.end(req.url.substr(1));
});
var clientServer = http.createServer(function requestHandler(req, res) {
proxy.web(req, res);
});
var originServer = http.createServer(function(req, res) {
res.end('Response');
});
clientServer.listen('8087');
originServer.listen('8082');
var completedRequests = 0;
var activeRequests = 0;
var totalTime = 0;
function request() {
var start = new Date().getTime();
activeRequests++;
var uid = crypto.randomBytes(16).toString('hex');
var requestUrl = 'http://127.0.0.1:8087/' + uid;
http.request(requestUrl, function(res) {
var chunks = [];
res.on('data', Array.prototype.push.bind(chunks));
res.on('end', function() {
// console.log('%s -> %s = %s', requestUrl, uid, chunks.join(''));
assert.strictEqual(uid, chunks.join(''));
activeRequests--;
completedRequests++;
totalTime += new Date().getTime() - start;
process.nextTick(request);
});
}).end();
}
for (var i=0; i < CONCURRENCY; i++) {
request();
}
setInterval(function() {
console.log('Completed: %s, Active: %s/%s, Avg. Response: %ss', completedRequests, activeRequests, CONCURRENCY, ((totalTime / completedRequests) / 1000).toFixed(2));
}, 1500);
@cmawhorter
Copy link
Author

I should note this is relevant only to an (unmerged) PR I submitted to the project.

It will not work with npm releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment