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