node/benchmark/http_simple.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

2009-11-06 19:53:27 +08:00
path = require("path");
2010-03-10 04:00:06 +08:00
var puts = require("../lib/sys").puts;
http = require("../lib/http2");
2009-09-28 18:36:36 +08:00
fixed = ""
for (var i = 0; i < 20*1024; i++) {
fixed += "C";
}
2009-11-06 19:53:27 +08:00
stored = {};
2009-11-06 19:53:27 +08:00
2009-09-28 18:36:36 +08:00
http.createServer(function (req, res) {
var commands = req.url.split("/");
var command = commands[1];
var body = "";
var arg = commands[2];
var status = 200;
if (command == "bytes") {
var n = parseInt(arg, 10)
if (n <= 0)
throw "bytes called with n <= 0"
if (stored[n] === undefined) {
puts("create stored[n]");
stored[n] = "";
for (var i = 0; i < n; i++) {
stored[n] += "C"
}
}
body = stored[n];
} else if (command == "quit") {
res.connection.server.close();
body = "quitting";
} else if (command == "fixed") {
body = fixed;
} else {
status = 404;
body = "not found\n";
}
var content_length = body.length.toString();
2010-02-26 04:54:48 +08:00
res.writeHead( status
, { "Content-Type": "text/plain"
, "Content-Length": content_length
}
);
res.write(body);
2010-02-18 07:36:50 +08:00
res.close();
}).listen(8000);