benchmark: Backport improvements made in master
[platform/upstream/nodejs.git] / benchmark / http_simple.js
1 var path = require('path'),
2     exec = require('child_process').exec,
3     http = require('http');
4
5 var port = parseInt(process.env.PORT || 8000);
6
7 console.log('pid ' + process.pid);
8
9 var fixed = makeString(20 * 1024, 'C'),
10     storedBytes = {},
11     storedBuffer = {},
12     storedUnicode = {};
13
14 var useDomains = process.env.NODE_USE_DOMAINS;
15
16 // set up one global domain.
17 if (useDomains) {
18   var domain = require('domain');
19   var gdom = domain.create();
20   gdom.on('error', function(er) {
21     console.log('Error on global domain', er);
22     throw er;
23   });
24   gdom.enter();
25 }
26
27 var server = http.createServer(function (req, res) {
28   if (useDomains) {
29     var dom = domain.create();
30     dom.add(req);
31     dom.add(res);
32   }
33
34   var commands = req.url.split('/');
35   var command = commands[1];
36   var body = '';
37   var arg = commands[2];
38   var n_chunks = parseInt(commands[3], 10);
39   var status = 200;
40
41   if (command == 'bytes') {
42     var n = ~~arg;
43     if (n <= 0)
44       throw new Error('bytes called with n <= 0')
45     if (storedBytes[n] === undefined) {
46       console.log('create storedBytes[n]');
47       storedBytes[n] = makeString(n, 'C');
48     }
49     body = storedBytes[n];
50
51   } else if (command == 'buffer') {
52     var n = ~~arg;
53     if (n <= 0)
54       throw new Error('buffer called with n <= 0');
55     if (storedBuffer[n] === undefined) {
56       console.log('create storedBuffer[n]');
57       storedBuffer[n] = new Buffer(n);
58       for (var i = 0; i < n; i++) {
59         storedBuffer[n][i] = 'C'.charCodeAt(0);
60       }
61     }
62     body = storedBuffer[n];
63
64   } else if (command == 'unicode') {
65     var n = ~~arg;
66     if (n <= 0)
67       throw new Error('unicode called with n <= 0');
68     if (storedUnicode[n] === undefined) {
69       console.log('create storedUnicode[n]');
70       storedUnicode[n] = makeString(n, '\u263A');
71     }
72     body = storedUnicode[n];
73
74   } else if (command == 'quit') {
75     res.connection.server.close();
76     body = 'quitting';
77
78   } else if (command == 'fixed') {
79     body = fixed;
80
81   } else if (command == 'echo') {
82     res.writeHead(200, { 'Content-Type': 'text/plain',
83                          'Transfer-Encoding': 'chunked' });
84     req.pipe(res);
85     return;
86
87   } else {
88     status = 404;
89     body = 'not found\n';
90   }
91
92   // example: http://localhost:port/bytes/512/4
93   // sends a 512 byte body in 4 chunks of 128 bytes
94   if (n_chunks > 0) {
95     res.writeHead(status, { 'Content-Type': 'text/plain',
96                             'Transfer-Encoding': 'chunked' });
97     // send body in chunks
98     var len = body.length;
99     var step = ~~(len / n_chunks) || len;
100
101     for (var i = 0; i < len; i += step) {
102       res.write(body.slice(i, i + step));
103     }
104
105     res.end();
106   } else {
107     var content_length = body.length.toString();
108
109     res.writeHead(status, { 'Content-Type': 'text/plain',
110                             'Content-Length': content_length });
111     res.end(body);
112   }
113 });
114
115 function makeString(size, c) {
116   var s = '';
117   while (s.length < size) {
118     s += c;
119   }
120   return s;
121 }
122
123 server.listen(port, function () {
124   console.log('Listening at http://127.0.0.1:'+port+'/');
125 });
126
127 process.on('exit', function() {
128   console.error('libuv counters', process.uvCounters());
129 });