fix whitespace errors
[platform/upstream/nodejs.git] / benchmark / http_simple.js
1 path = require("path");
2 Buffer = require("buffer").Buffer;
3
4 port = parseInt(process.env.PORT || 8000);
5
6 var old = (process.argv[2] == 'old');
7
8 console.log('pid ' + process.pid);
9
10 http = require(old ? "http_old" : 'http');
11 if (old) console.log('old version');
12
13 fixed = ""
14 for (var i = 0; i < 20*1024; i++) {
15   fixed += "C";
16 }
17
18 stored = {};
19 storedBuffer = {};
20
21 http.createServer(function (req, res) {
22   var commands = req.url.split("/");
23   var command = commands[1];
24   var body = "";
25   var arg = commands[2];
26   var status = 200;
27
28   if (command == "bytes") {
29     var n = parseInt(arg, 10)
30     if (n <= 0)
31       throw "bytes called with n <= 0"
32     if (stored[n] === undefined) {
33       console.log("create stored[n]");
34       stored[n] = "";
35       for (var i = 0; i < n; i++) {
36         stored[n] += "C"
37       }
38     }
39     body = stored[n];
40
41   } else if (command == "buffer") {
42     var n = parseInt(arg, 10)
43     if (n <= 0) throw new Error("bytes called with n <= 0");
44     if (storedBuffer[n] === undefined) {
45       console.log("create storedBuffer[n]");
46       storedBuffer[n] = new Buffer(n);
47       for (var i = 0; i < n; i++) {
48         storedBuffer[n][i] = "C".charCodeAt(0);
49       }
50     }
51     body = storedBuffer[n];
52
53   } else if (command == "quit") {
54     res.connection.server.close();
55     body = "quitting";
56
57   } else if (command == "fixed") {
58     body = fixed;
59
60   } else {
61     status = 404;
62     body = "not found\n";
63   }
64
65   var content_length = body.length.toString();
66
67   res.writeHead( status
68                 , { "Content-Type": "text/plain"
69                   , "Content-Length": content_length
70                   }
71                 );
72   if (old) {
73     res.write(body, 'ascii');
74     res.close();
75   } else {
76     res.end(body, 'ascii');
77   }
78 }).listen(port);
79
80 console.log('Listening at http://127.0.0.1:'+port+'/');