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