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