debugger: Disable/Enable raw mode for child
[platform/upstream/nodejs.git] / benchmark / http_simple.js
1 path = require("path");
2 exec = require("child_process").exec;
3 http = require("http");
4
5 port = parseInt(process.env.PORT || 8000);
6
7 console.log('pid ' + process.pid);
8
9 fixed = ""
10 for (var i = 0; i < 20*1024; i++) {
11   fixed += "C";
12 }
13
14 var uname, rev;
15
16 exec('git rev-list -1 HEAD', function (e, stdout) {
17   if (e) {
18     console.error("Problem executing: 'git rev-list -1 HEAD'");
19     throw new Error(e);
20   }
21   rev = stdout.replace(/\s/g, '');
22 });
23
24 exec('uname -a', function (e, stdout) {
25   if (e) {
26     console.error("Problem executing: 'uname -a'");
27     throw new Error(e);
28   }
29   uname = stdout.replace(/[\r\n]/g, '');
30 });
31
32
33
34 stored = {};
35 storedBuffer = {};
36
37 var server = http.createServer(function (req, res) {
38   var commands = req.url.split("/");
39   var command = commands[1];
40   var body = "";
41   var arg = commands[2];
42   var status = 200;
43
44   if (command == "bytes") {
45     var n = parseInt(arg, 10)
46     debugger;
47     if (n <= 0)
48       throw "bytes called with n <= 0"
49     if (stored[n] === undefined) {
50       console.log("create stored[n]");
51       stored[n] = "";
52       for (var i = 0; i < n; i++) {
53         stored[n] += "C"
54       }
55     }
56     body = stored[n];
57
58   } else if (command == "buffer") {
59     var n = parseInt(arg, 10)
60     if (n <= 0) throw new Error("bytes called with n <= 0");
61     if (storedBuffer[n] === undefined) {
62       console.log("create storedBuffer[n]");
63       storedBuffer[n] = new Buffer(n);
64       for (var i = 0; i < n; i++) {
65         storedBuffer[n][i] = "C".charCodeAt(0);
66       }
67     }
68     body = storedBuffer[n];
69
70   } else if (command == "quit") {
71     res.connection.server.close();
72     body = "quitting";
73
74   } else if (command == "fixed") {
75     body = fixed;
76
77   } else if (command == "info") {
78     body = 'rev=' + rev + '\nuname="' + uname + '"\n';
79
80   } else {
81     status = 404;
82     body = "not found\n";
83   }
84
85   var content_length = body.length.toString();
86
87   res.writeHead(status, { "Content-Type": "text/plain",
88                           "Content-Length": content_length });
89   res.end(body);
90
91 });
92
93 server.listen(port, function () {
94   console.log('Listening at http://127.0.0.1:'+port+'/');
95 });
96