Skip to content

Commit f7f5fa7

Browse files
committed
[dist doc] Added documentation for consistent benchmarking of node-http-proxy
1 parent 455f97e commit f7f5fa7

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

benchmark/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Benchmarking `node-http-proxy`
2+
3+
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).
4+
5+
## Pre-requisites
6+
7+
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**.
8+
9+
```
10+
$ wrk
11+
Usage: wrk <options> <url>
12+
Options:
13+
-c, --connections <n> Connections to keep open
14+
-r, --requests <n> Total requests to make
15+
-t, --threads <n> Number of threads to use
16+
17+
-H, --header <h> Add header to request
18+
-v, --version Print version details
19+
20+
Numeric arguments may include a SI unit (2k, 2M, 2G)
21+
```
22+
23+
## Benchmarks
24+
25+
1. [Simple HTTP benchmark](#simple-http)
26+
27+
### Simple HTTP
28+
29+
_This benchmark requires three terminals running:_
30+
31+
1. **A proxy server:** `node benchmark/scripts/proxy.js`
32+
2. **A target server:** `node benchmark/scripts/hello.js`
33+
3. **A wrk process:** `wrk -c 20 -r 10000 -t 2 http://127.0.0.1:8000`

benchmark/scripts/hello.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('http').createServer(function(req, res) {
2+
res.end('Hello world!');
3+
}).listen(9000);

benchmark/scripts/proxy.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var http = require('http'),
2+
httpProxy = require('../../');
3+
//
4+
// Create your proxy server
5+
//
6+
httpProxy.createProxyServer({ target: 'http://localhost:9000' }).listen(8000);
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var crypto = require('crypto'),
2+
WebSocket = require('ws'),
3+
async = require('async'),
4+
httpProxy = require('../../');
5+
6+
var SERVER_PORT = 8415,
7+
PROXY_PORT = 8514;
8+
9+
var testSets = [
10+
{
11+
size: 1024 * 1024, // 1 MB
12+
count: 128 // 128 MB
13+
},
14+
{
15+
size: 1024, // 1 KB,
16+
count: 1024 // 1 MB
17+
},
18+
{
19+
size: 128, // 128 B
20+
count: 1024 * 8 // 1 MB
21+
}
22+
];
23+
24+
testSets.forEach(function (set) {
25+
set.buffer = new Buffer(crypto.randomBytes(set.size));
26+
27+
set.buffers = [];
28+
for (var i = 0; i < set.count; i++) {
29+
set.buffers.push(set.buffer);
30+
}
31+
});
32+
33+
function runSet(set, callback) {
34+
function runAgainst(port, callback) {
35+
function send(sock) {
36+
sock.send(set.buffers[got++]);
37+
if (got === set.count) {
38+
t = new Date() - t;
39+
40+
server.close();
41+
proxy.close();
42+
43+
callback(null, t);
44+
}
45+
}
46+
47+
var server = new WebSocket.Server({ port: SERVER_PORT }),
48+
proxy = httpProxy.createServer(SERVER_PORT, 'localhost').listen(PROXY_PORT),
49+
client = new WebSocket('ws://localhost:' + port),
50+
got = 0,
51+
t = new Date();
52+
53+
server.on('connection', function (ws) {
54+
send(ws);
55+
56+
ws.on('message', function (msg) {
57+
send(ws);
58+
});
59+
});
60+
61+
client.on('message', function () {
62+
send(client);
63+
});
64+
}
65+
66+
async.series({
67+
server: async.apply(runAgainst, SERVER_PORT),
68+
proxy: async.apply(runAgainst, PROXY_PORT)
69+
}, function (err, results) {
70+
if (err) {
71+
throw err;
72+
}
73+
74+
var mb = (set.size * set.count) / (1024 * 1024);
75+
console.log(set.size / (1024) + ' KB * ' + set.count + ' (' + mb + ' MB)');
76+
77+
Object.keys(results).forEach(function (key) {
78+
var t = results[key],
79+
throughput = mb / (t / 1000);
80+
81+
console.log(' ' + key + ' took ' + t + ' ms (' + throughput + ' MB/s)');
82+
});
83+
84+
callback();
85+
});
86+
}
87+
88+
async.forEachLimit(testSets, 1, runSet);

0 commit comments

Comments
 (0)