forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-request-body.js
69 lines (61 loc) · 1.48 KB
/
client-request-body.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Measure the time it takes for the HTTP client to send a request body.
var common = require('../common.js');
var http = require('http');
var bench = common.createBenchmark(main, {
dur: [5],
type: ['asc', 'utf', 'buf'],
bytes: [32, 256, 1024],
method: ['write', 'end '] // two spaces added to line up each row
});
function main(conf) {
var dur = +conf.dur;
var len = +conf.bytes;
var encoding;
var chunk;
switch (conf.type) {
case 'buf':
chunk = new Buffer(len);
chunk.fill('x');
break;
case 'utf':
encoding = 'utf8';
chunk = new Array(len / 2 + 1).join('ü');
break;
case 'asc':
chunk = new Array(len + 1).join('a');
break;
}
var nreqs = 0;
var options = {
headers: { 'Connection': 'keep-alive', 'Transfer-Encoding': 'chunked' },
agent: new http.Agent({ maxSockets: 1 }),
host: '127.0.0.1',
port: common.PORT,
path: '/',
method: 'POST'
};
var server = http.createServer(function(req, res) {
res.end();
});
server.listen(options.port, options.host, function() {
setTimeout(done, dur * 1000);
bench.start();
pummel();
});
function pummel() {
var req = http.request(options, function(res) {
nreqs++;
pummel(); // Line up next request.
res.resume();
});
if (conf.method === 'write') {
req.write(chunk, encoding);
req.end();
} else {
req.end(chunk, encoding);
}
}
function done() {
bench.end(nreqs);
}
}