src: move debug agent from deps/ to src/
[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 var fixed = makeString(20 * 1024, 'C'),
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   if (command == 'bytes') {
40     var n = ~~arg;
41     if (n <= 0)
42       throw new Error('bytes called with n <= 0')
43     if (storedBytes[n] === undefined) {
44       storedBytes[n] = makeString(n, 'C');
45     }
46     body = storedBytes[n];
47
48   } else if (command == 'buffer') {
49     var n = ~~arg;
50     if (n <= 0)
51       throw new Error('buffer called with n <= 0');
52     if (storedBuffer[n] === undefined) {
53       storedBuffer[n] = new Buffer(n);
54       for (var i = 0; i < n; i++) {
55         storedBuffer[n][i] = 'C'.charCodeAt(0);
56       }
57     }
58     body = storedBuffer[n];
59
60   } else if (command == 'unicode') {
61     var n = ~~arg;
62     if (n <= 0)
63       throw new Error('unicode called with n <= 0');
64     if (storedUnicode[n] === undefined) {
65       storedUnicode[n] = makeString(n, '\u263A');
66     }
67     body = storedUnicode[n];
68
69   } else if (command == 'quit') {
70     res.connection.server.close();
71     body = 'quitting';
72
73   } else if (command == 'fixed') {
74     body = fixed;
75
76   } else if (command == 'echo') {
77     res.writeHead(200, { 'Content-Type': 'text/plain',
78                          'Transfer-Encoding': 'chunked' });
79     req.pipe(res);
80     return;
81
82   } else {
83     status = 404;
84     body = 'not found\n';
85   }
86
87   // example: http://localhost:port/bytes/512/4
88   // sends a 512 byte body in 4 chunks of 128 bytes
89   if (n_chunks > 0) {
90     res.writeHead(status, { 'Content-Type': 'text/plain',
91                             'Transfer-Encoding': 'chunked' });
92     // send body in chunks
93     var len = body.length;
94     var step = Math.floor(len / n_chunks) || 1;
95
96     for (var i = 0, n = (n_chunks - 1); i < n; ++i) {
97       res.write(body.slice(i * step, i * step + step));
98     }
99     res.end(body.slice((n_chunks - 1) * step));
100   } else {
101     var content_length = body.length.toString();
102
103     res.writeHead(status, { 'Content-Type': 'text/plain',
104                             'Content-Length': content_length });
105     res.end(body);
106   }
107 });
108
109 function makeString(size, c) {
110   var s = '';
111   while (s.length < size) {
112     s += c;
113   }
114   return s;
115 }
116
117 server.listen(port, function () {
118   if (module === require.main)
119     console.error('Listening at http://127.0.0.1:'+port+'/');
120 });