doc: document directories in test directory
[platform/upstream/nodejs.git] / benchmark / http / end-vs-write-end.js
1 // When calling .end(buffer) right away, this triggers a "hot path"
2 // optimization in http.js, to avoid an extra write call.
3 //
4 // However, the overhead of copying a large buffer is higher than
5 // the overhead of an extra write() call, so the hot path was not
6 // always as hot as it could be.
7 //
8 // Verify that our assumptions are valid.
9
10 var common = require('../common.js');
11 var PORT = common.PORT;
12
13 var bench = common.createBenchmark(main, {
14   type: ['asc', 'utf', 'buf'],
15   kb: [64, 128, 256, 1024],
16   c: [100],
17   method: ['write', 'end']
18 });
19
20 function main(conf) {
21   http = require('http');
22   var chunk;
23   var len = conf.kb * 1024;
24   switch (conf.type) {
25     case 'buf':
26       chunk = new Buffer(len);
27       chunk.fill('x');
28       break;
29     case 'utf':
30       encoding = 'utf8';
31       chunk = new Array(len / 2 + 1).join('ΓΌ');
32       break;
33     case 'asc':
34       chunk = new Array(len + 1).join('a');
35       break;
36   }
37
38   function write(res) {
39     res.write(chunk);
40     res.end();
41   }
42
43   function end(res) {
44     res.end(chunk);
45   }
46
47   var method = conf.method === 'write' ? write : end;
48   var args = ['-d', '10s', '-t', 8, '-c', conf.c];
49
50   var server = http.createServer(function(req, res) {
51     method(res);
52   });
53
54   server.listen(common.PORT, function() {
55     bench.http('/', args, function() {
56       server.close();
57     });
58   });
59 }