Allow different ports for http_simple.js
[platform/upstream/nodejs.git] / benchmark / http_simple.js
1 path = require("path");
2
3 port = parseInt(process.env.PORT || 8000);
4
5 var puts = require("sys").puts;
6
7 var old = (process.argv[2] == 'old');
8
9 puts('pid ' + process.pid);
10
11 http = require(old ? "http_old" : 'http');
12 if (old) puts('old version');
13
14 fixed = ""
15 for (var i = 0; i < 20*1024; i++) {
16   fixed += "C";
17 }
18
19 stored = {};
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       puts("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 == "quit") {
42     res.connection.server.close();
43     body = "quitting";
44
45   } else if (command == "fixed") {
46     body = fixed;
47
48   } else {
49     status = 404;
50     body = "not found\n";
51   }
52
53   var content_length = body.length.toString();
54
55   res.writeHead( status 
56                 , { "Content-Type": "text/plain"
57                   , "Content-Length": content_length
58                   }
59                 );
60   if (old) {
61     res.write(body, 'ascii');
62     res.close();
63   } else {
64     res.end(body, 'ascii');
65   }
66 }).listen(port);
67
68 puts('Listening at http://127.0.0.1:'+port+'/');