3 // node benchmark/http_simple_auto.js <args> <target>
6 // <args> Arguments to pass to `ab`.
7 // <target> Target to benchmark, e.g. `bytes/1024` or `buffer/8192`.
10 var path = require("path");
11 var http = require("http");
12 var spawn = require("child_process").spawn;
14 var port = parseInt(process.env.PORT || 8000);
17 for (var i = 0; i < 20*1024; i++) {
22 var storedBuffer = {};
24 var server = http.createServer(function (req, res) {
25 var commands = req.url.split("/");
26 var command = commands[1];
28 var arg = commands[2];
29 var n_chunks = parseInt(commands[3], 10);
32 if (command == "bytes") {
33 var n = parseInt(arg, 10)
35 throw "bytes called with n <= 0"
36 if (stored[n] === undefined) {
38 for (var i = 0; i < n; i++) {
44 } else if (command == "buffer") {
45 var n = parseInt(arg, 10)
46 if (n <= 0) throw new Error("bytes called with n <= 0");
47 if (storedBuffer[n] === undefined) {
48 storedBuffer[n] = new Buffer(n);
49 for (var i = 0; i < n; i++) {
50 storedBuffer[n][i] = "C".charCodeAt(0);
53 body = storedBuffer[n];
55 } else if (command == "quit") {
56 res.connection.server.close();
59 } else if (command == "fixed") {
62 } else if (command == "echo") {
63 res.writeHead(200, { "Content-Type": "text/plain",
64 "Transfer-Encoding": "chunked" });
73 // example: http://localhost:port/bytes/512/4
74 // sends a 512 byte body in 4 chunks of 128 bytes
76 res.writeHead(status, { "Content-Type": "text/plain",
77 "Transfer-Encoding": "chunked" });
78 // send body in chunks
79 var len = body.length;
80 var step = Math.floor(len / n_chunks) || 1;
82 for (var i = 0, n = (n_chunks - 1); i < n; ++i) {
83 res.write(body.slice(i * step, i * step + step));
85 res.end(body.slice((n_chunks - 1) * step));
87 var content_length = body.length.toString();
89 res.writeHead(status, { "Content-Type": "text/plain",
90 "Content-Length": content_length });
96 server.listen(port, function () {
97 var url = 'http://127.0.0.1:' + port + '/';
99 var n = process.argv.length - 1;
100 process.argv[n] = url + process.argv[n];
102 var cp = spawn('ab', process.argv.slice(2));
103 cp.stdout.pipe(process.stdout);
104 cp.stderr.pipe(process.stderr);
105 cp.on('exit', function() {
107 process.nextTick(dump_mm_stats);
111 function dump_mm_stats() {
112 if (typeof gc != 'function') return;
114 var before = process.memoryUsage();
115 for (var i = 0; i < 10; ++i) gc();
116 var after = process.memoryUsage();
117 setTimeout(print_stats, 250); // give GC time to settle
119 function print_stats() {
120 console.log('\nBEFORE / AFTER GC');
121 ['rss', 'heapTotal', 'heapUsed'].forEach(function(key) {
122 var a = before[key] / (1024 * 1024);
123 var b = after[key] / (1024 * 1024);
124 console.log('%sM / %sM %s', a.toFixed(2), b.toFixed(2), key);