Skip to content

Commit 030a923

Browse files
Rudi Cilibrasitrevnorris
Rudi Cilibrasi
authored andcommitted
benchmark: chunky http client benchmark variation
PR-URL: #228 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 1a3ca82 commit 030a923

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

benchmark/net/chunky_http_client.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
// test HTTP throughput in fragmented header case
4+
var common = require('../common.js');
5+
var net = require('net');
6+
var test = require('../../test/common.js');
7+
8+
var bench = common.createBenchmark(main, {
9+
len: [1, 4, 8, 16, 32, 64, 128],
10+
num: [5, 50, 500, 2000],
11+
type: ['send'],
12+
});
13+
14+
15+
function main(conf) {
16+
var len = +conf.len;
17+
var num = +conf.num;
18+
var type = conf.type;
19+
var todo = [];
20+
var headers = [];
21+
// Chose 7 because 9 showed "Connection error" / "Connection closed"
22+
// An odd number could result in a better length dispersion.
23+
for (var i = 7; i <= 7 * 7 * 7; i *= 7)
24+
headers.push(Array(i + 1).join('o'));
25+
26+
function WriteHTTPHeaders(channel, has_keep_alive, extra_header_count) {
27+
todo = []
28+
todo.push('GET / HTTP/1.1');
29+
todo.push('Host: localhost');
30+
todo.push('Connection: keep-alive');
31+
todo.push('Accept: text/html,application/xhtml+xml,' +
32+
'application/xml;q=0.9,image/webp,*/*;q=0.8');
33+
todo.push('User-Agent: Mozilla/5.0 (X11; Linux x86_64) ' +
34+
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
35+
'Chrome/39.0.2171.71 Safari/537.36');
36+
todo.push('Accept-Encoding: gzip, deflate, sdch');
37+
todo.push('Accept-Language: en-US,en;q=0.8');
38+
for (var i = 0; i < extra_header_count; i++) {
39+
// Utilize first three powers of a small integer for an odd cycle and
40+
// because the fourth power of some integers overloads the server.
41+
todo.push('X-Header-' + i + ': ' + headers[i % 3]);
42+
}
43+
todo.push('');
44+
todo.push('');
45+
todo = todo.join('\r\n');
46+
// Using odd numbers in many places may increase length coverage.
47+
var chunksize = 37;
48+
for (i = 0; i < todo.length; i += chunksize) {
49+
var cur = todo.slice(i, i + chunksize);
50+
channel.write(cur);
51+
}
52+
}
53+
54+
var success = 0;
55+
var failure = 0;
56+
var min = 10;
57+
var size = 0;
58+
var mod = 317;
59+
var mult = 17;
60+
var add = 11;
61+
var count = 0;
62+
var PIPE = test.PIPE;
63+
var socket = net.connect(PIPE, function() {
64+
bench.start();
65+
WriteHTTPHeaders(socket, 1, len);
66+
socket.setEncoding('utf8')
67+
socket.on('data', function(d) {
68+
var did = false;
69+
var pattern = 'HTTP/1.1 200 OK\r\n';
70+
if ((d.length === pattern.length && d === pattern) ||
71+
(d.length > pattern.length &&
72+
d.slice(0, pattern.length) === pattern)) {
73+
success += 1;
74+
did = true;
75+
} else {
76+
pattern = 'HTTP/1.1 '
77+
if ((d.length === pattern.length && d === pattern) ||
78+
(d.length > pattern.length &&
79+
d.slice(0, pattern.length) === pattern)) {
80+
failure += 1;
81+
did = true;
82+
}
83+
}
84+
size = (size * mult + add) % mod;
85+
if (did) {
86+
count += 1;
87+
if (count === num) {
88+
bench.end(count);
89+
} else {
90+
WriteHTTPHeaders(socket, 1, min + size);
91+
}
92+
}
93+
});
94+
socket.on('close', function() {
95+
console.log('Connection closed');
96+
});
97+
98+
socket.on('error', function() {
99+
throw new Error('Connection error');
100+
});
101+
});
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var http = require('http');
5+
var fs = require('fs');
6+
var spawn = require('child_process').spawn;
7+
var common = require('../common.js')
8+
var test = require('../../test/common.js')
9+
var pep = path.dirname(process.argv[1]) + '/chunky_http_client.js';
10+
var PIPE = test.PIPE;
11+
12+
var server;
13+
try {
14+
fs.unlinkSync(PIPE);
15+
} catch (e) { /* ignore */ }
16+
17+
server = http.createServer(function(req, res) {
18+
res.writeHead(200, { 'content-type': 'text/plain',
19+
'content-length': '2' });
20+
res.end('ok');
21+
});
22+
23+
server.on('error', function(err) {
24+
throw new Error('server error: ' + err);
25+
});
26+
27+
try {
28+
var child;
29+
30+
server.listen(PIPE);
31+
32+
child = spawn(process.execPath, [pep], { });
33+
34+
child.on('error', function(err) {
35+
throw new Error('spawn error: ' + err );
36+
});
37+
38+
child.stdout.pipe(process.stdout);
39+
40+
child.on('exit', function (exitCode) {
41+
console.error('Child exited with code: ' + exitCode);
42+
});
43+
44+
} catch(e) {
45+
throw new Error('error: ' + e );
46+
}
47+

0 commit comments

Comments
 (0)