From f7f5fa727e8f1d3f4946e61ad03830dab1da01a5 Mon Sep 17 00:00:00 2001 From: indexzero Date: Thu, 26 Sep 2013 03:27:55 -0400 Subject: [PATCH] [dist doc] Added documentation for consistent benchmarking of node-http-proxy --- benchmark/README.md | 33 ++++++++ benchmark/scripts/hello.js | 3 + benchmark/scripts/proxy.js | 6 ++ benchmark/scripts/websockets-throughput.js | 88 ++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 benchmark/README.md create mode 100644 benchmark/scripts/hello.js create mode 100644 benchmark/scripts/proxy.js create mode 100644 benchmark/scripts/websockets-throughput.js diff --git a/benchmark/README.md b/benchmark/README.md new file mode 100644 index 0000000..2a852d1 --- /dev/null +++ b/benchmark/README.md @@ -0,0 +1,33 @@ +# Benchmarking `node-http-proxy` + +The long-term goal of these scripts and documentation is to provide a consistent and well understood benchmarking process for `node-http-proxy` so that performance does not degrade over time. They were initially created to compare the performance of `v0.10.3` and `v1.0.0` (which was a significant rewrite). + +## Pre-requisites + +All benchmarking shall be done with [wrk](https://github.com/wg/wrk) which _is the same tool used for performance testing by the node.js core team._ **Make sure you have `wrk` installed before continuing**. + +``` +$ wrk +Usage: wrk + Options: + -c, --connections Connections to keep open + -r, --requests Total requests to make + -t, --threads Number of threads to use + + -H, --header Add header to request + -v, --version Print version details + + Numeric arguments may include a SI unit (2k, 2M, 2G) +``` + +## Benchmarks + +1. [Simple HTTP benchmark](#simple-http) + +### Simple HTTP + +_This benchmark requires three terminals running:_ + +1. **A proxy server:** `node benchmark/scripts/proxy.js` +2. **A target server:** `node benchmark/scripts/hello.js` +3. **A wrk process:** `wrk -c 20 -r 10000 -t 2 http://127.0.0.1:8000` \ No newline at end of file diff --git a/benchmark/scripts/hello.js b/benchmark/scripts/hello.js new file mode 100644 index 0000000..be09b63 --- /dev/null +++ b/benchmark/scripts/hello.js @@ -0,0 +1,3 @@ +require('http').createServer(function(req, res) { + res.end('Hello world!'); +}).listen(9000); \ No newline at end of file diff --git a/benchmark/scripts/proxy.js b/benchmark/scripts/proxy.js new file mode 100644 index 0000000..a70c558 --- /dev/null +++ b/benchmark/scripts/proxy.js @@ -0,0 +1,6 @@ +var http = require('http'), + httpProxy = require('../../'); +// +// Create your proxy server +// +httpProxy.createProxyServer({ target: 'http://localhost:9000' }).listen(8000); \ No newline at end of file diff --git a/benchmark/scripts/websockets-throughput.js b/benchmark/scripts/websockets-throughput.js new file mode 100644 index 0000000..866e424 --- /dev/null +++ b/benchmark/scripts/websockets-throughput.js @@ -0,0 +1,88 @@ +var crypto = require('crypto'), + WebSocket = require('ws'), + async = require('async'), + httpProxy = require('../../'); + +var SERVER_PORT = 8415, + PROXY_PORT = 8514; + +var testSets = [ + { + size: 1024 * 1024, // 1 MB + count: 128 // 128 MB + }, + { + size: 1024, // 1 KB, + count: 1024 // 1 MB + }, + { + size: 128, // 128 B + count: 1024 * 8 // 1 MB + } +]; + +testSets.forEach(function (set) { + set.buffer = new Buffer(crypto.randomBytes(set.size)); + + set.buffers = []; + for (var i = 0; i < set.count; i++) { + set.buffers.push(set.buffer); + } +}); + +function runSet(set, callback) { + function runAgainst(port, callback) { + function send(sock) { + sock.send(set.buffers[got++]); + if (got === set.count) { + t = new Date() - t; + + server.close(); + proxy.close(); + + callback(null, t); + } + } + + var server = new WebSocket.Server({ port: SERVER_PORT }), + proxy = httpProxy.createServer(SERVER_PORT, 'localhost').listen(PROXY_PORT), + client = new WebSocket('ws://localhost:' + port), + got = 0, + t = new Date(); + + server.on('connection', function (ws) { + send(ws); + + ws.on('message', function (msg) { + send(ws); + }); + }); + + client.on('message', function () { + send(client); + }); + } + + async.series({ + server: async.apply(runAgainst, SERVER_PORT), + proxy: async.apply(runAgainst, PROXY_PORT) + }, function (err, results) { + if (err) { + throw err; + } + + var mb = (set.size * set.count) / (1024 * 1024); + console.log(set.size / (1024) + ' KB * ' + set.count + ' (' + mb + ' MB)'); + + Object.keys(results).forEach(function (key) { + var t = results[key], + throughput = mb / (t / 1000); + + console.log(' ' + key + ' took ' + t + ' ms (' + throughput + ' MB/s)'); + }); + + callback(); + }); +} + +async.forEachLimit(testSets, 1, runSet);