Merge remote-tracking branch 'ry/v0.6' into master
[platform/upstream/nodejs.git] / benchmark / http_simple.js
1 path = require("path");
2 exec = require("child_process").exec;
3 http = require("http");
4
5 port = parseInt(process.env.PORT || 8000);
6
7 console.log('pid ' + process.pid);
8
9 fixed = ""
10 for (var i = 0; i < 20*1024; i++) {
11   fixed += "C";
12 }
13
14 stored = {};
15 storedBuffer = {};
16
17 var useDomains = process.env.NODE_USE_DOMAINS;
18
19 // set up one global domain.
20 if (useDomains) {
21   var domain = require('domain');
22   var gdom = domain.create();
23   gdom.on('error', function(er) {
24     console.log('Error on global domain', er);
25     throw er;
26   });
27   gdom.enter();
28 }
29
30 var server = http.createServer(function (req, res) {
31
32   if (useDomains) {
33     var dom = domain.create();
34     dom.add(req);
35     dom.add(res);
36   }
37
38   var commands = req.url.split("/");
39   var command = commands[1];
40   var body = "";
41   var arg = commands[2];
42   var n_chunks = parseInt(commands[3], 10);
43   var status = 200;
44
45   if (command == "bytes") {
46     var n = parseInt(arg, 10)
47     if (n <= 0)
48       throw "bytes called with n <= 0"
49     if (stored[n] === undefined) {
50       console.log("create stored[n]");
51       stored[n] = "";
52       for (var i = 0; i < n; i++) {
53         stored[n] += "C"
54       }
55     }
56     body = stored[n];
57
58   } else if (command == "buffer") {
59     var n = parseInt(arg, 10)
60     if (n <= 0) throw new Error("bytes called with n <= 0");
61     if (storedBuffer[n] === undefined) {
62       console.log("create storedBuffer[n]");
63       storedBuffer[n] = new Buffer(n);
64       for (var i = 0; i < n; i++) {
65         storedBuffer[n][i] = "C".charCodeAt(0);
66       }
67     }
68     body = storedBuffer[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 = ~~(len / n_chunks) || len;
96
97     for (var i = 0; i < len; i += step) {
98       res.write(body.slice(i, i + step));
99     }
100
101     res.end();
102   } else {
103     var content_length = body.length.toString();
104
105     res.writeHead(status, { "Content-Type": "text/plain",
106                             "Content-Length": content_length });
107     res.end(body);
108   }
109
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 });