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