Merge branch 'master' into openssl
[platform/upstream/nodejs.git] / benchmark / http_simple.js
1 path = require("path");
2
3 var puts = require("sys").puts;
4
5 var old = (process.argv[2] == 'old');
6
7 puts('pid ' + process.pid);
8
9 http = require(old ? "http_old" : 'http');
10 if (old) puts('old version');
11
12 fixed = ""
13 for (var i = 0; i < 20*1024; i++) {
14   fixed += "C";
15 }
16
17 stored = {};
18
19 http.createServer(function (req, res) {
20   var commands = req.url.split("/");
21   var command = commands[1];
22   var body = "";
23   var arg = commands[2];
24   var status = 200;
25
26   if (command == "bytes") {
27     var n = parseInt(arg, 10)
28     if (n <= 0)
29       throw "bytes called with n <= 0" 
30     if (stored[n] === undefined) {
31       puts("create stored[n]");
32       stored[n] = "";
33       for (var i = 0; i < n; i++) {
34         stored[n] += "C"
35       }
36     }
37     body = stored[n];
38
39   } else if (command == "quit") {
40     res.connection.server.close();
41     body = "quitting";
42
43   } else if (command == "fixed") {
44     body = fixed;
45
46   } else {
47     status = 404;
48     body = "not found\n";
49   }
50
51   var content_length = body.length.toString();
52
53   res.writeHead( status 
54                 , { "Content-Type": "text/plain"
55                   , "Content-Length": content_length
56                   }
57                 );
58   if (old) {
59     res.write(body, 'ascii');
60     res.close();
61   } else {
62     res.end(body, 'ascii');
63   }
64 }).listen(8000);