1 path = require("path");
2 exec = require("child_process").exec;
3 http = require("http");
5 port = parseInt(process.env.PORT || 8000);
7 console.log('pid ' + process.pid);
10 for (var i = 0; i < 20*1024; i++) {
17 var useDomains = process.env.NODE_USE_DOMAINS;
19 // set up one global domain.
21 var domain = require('domain');
22 var gdom = domain.create();
23 gdom.on('error', function(er) {
24 console.log('Error on global domain', er);
30 var server = http.createServer(function (req, res) {
33 var dom = domain.create();
38 var commands = req.url.split("/");
39 var command = commands[1];
41 var arg = commands[2];
42 var n_chunks = parseInt(commands[3], 10);
45 if (command == "bytes") {
46 var n = parseInt(arg, 10)
48 throw "bytes called with n <= 0"
49 if (stored[n] === undefined) {
50 console.log("create stored[n]");
52 for (var i = 0; i < n; i++) {
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);
68 body = storedBuffer[n];
70 } else if (command == "quit") {
71 res.connection.server.close();
74 } else if (command == "fixed") {
77 } else if (command == "echo") {
78 res.writeHead(200, { "Content-Type": "text/plain",
79 "Transfer-Encoding": "chunked" });
88 // example: http://localhost:port/bytes/512/4
89 // sends a 512 byte body in 4 chunks of 128 bytes
91 res.writeHead(status, { "Content-Type": "text/plain",
92 "Transfer-Encoding": "chunked" });
93 // send body in chunks
94 var len = body.length;
95 var step = ~~(len / n_chunks) || len;
97 for (var i = 0; i < len; i += step) {
98 res.write(body.slice(i, i + step));
103 var content_length = body.length.toString();
105 res.writeHead(status, { "Content-Type": "text/plain",
106 "Content-Length": content_length });
112 server.listen(port, function () {
113 console.log('Listening at http://127.0.0.1:'+port+'/');
116 process.on('exit', function() {
117 console.error('libuv counters', process.uvCounters());