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