diff options
author | isaacs <i@izs.me> | 2012-03-06 18:18:35 -0800 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-03-06 18:19:17 -0800 |
commit | d5fca08da45feb6e28c19a0ff2cfd819edf9238c (patch) | |
tree | ab76ee5900ebd3f2c666f35628540576ef912c7f /benchmark | |
parent | b72d43cbf9116fb5eb0dc92006dbc180d4db7d3e (diff) | |
download | nodejs-d5fca08da45feb6e28c19a0ff2cfd819edf9238c.tar.gz nodejs-d5fca08da45feb6e28c19a0ff2cfd819edf9238c.tar.bz2 nodejs-d5fca08da45feb6e28c19a0ff2cfd819edf9238c.zip |
A benchmark script for measuring client latency
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/client_latency.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/benchmark/client_latency.js b/benchmark/client_latency.js new file mode 100644 index 000000000..89de775e8 --- /dev/null +++ b/benchmark/client_latency.js @@ -0,0 +1,81 @@ +// first start node http_simple.js +var http = require('http'); + +var latency = []; + +var numRequests = parseInt(process.argv[2], 10) || 100; +var maxSockets = parseInt(process.argv[3], 10) || 100; +var runs = parseInt(process.argv[4], 10) || 100; +var prefix = process.argv[5] || ''; +if (prefix) prefix += '_'; +var request = require('request'); +var r = 0; + +var port = parseInt(process.env.PORT, 10) || 8000; +var host = process.env.HOST || '127.0.0.1'; + +http.globalAgent.maxSockets = maxSockets; + +run(); + +function run() { + if (r++ === runs) { + return finish(); + } + + // make numRequests in parallel + // retain the order in which they are *made*. This requires trapping + // each one in a closure, since otherwise, we'll of course end + // up mostly sorting them in ascending order, since the cb from a + // fast request will almost always be called before the cb from a + // slow one. + var c = numRequests; + var lat = []; + latency.push(lat); + for (var i = 0; i < numRequests; i++) (function (i) { + makeRequest(function(l) { + lat[i] = l; + c--; + if (c === 0) run(); + }); + })(i); +} + +function makeRequest(cb) { + var opts = { host: host, + port: port, + uri: 'http://'+host+':'+port+'/bytes/12', + forever: true, + path: '/bytes/12' }; + var pre = Date.now(); + var req = http.get(opts, function(res) { + return cb(Date.now() - pre); + }); +} + +function finish() { + var data = []; + latency.forEach(function(run, i) { + run.forEach(function(l, j) { + data[j] = data[j] || []; + data[j][i] = l; + }); + }); + + data = data.map(function (l, i) { + return l.join('\t') + }).join('\n') + '\n'; + + var fname = prefix + + 'client_latency_' + + numRequests + '_' + + maxSockets + '_' + + runs + '.tab'; + var path = require('path'); + fname = path.resolve(__dirname, '..', 'out', fname); + fname = path.relative(process.cwd(), fname); + require('fs').writeFile(fname, data, function(er) { + if (er) throw er; + console.log('written: %s', fname); + }); +} |