doc: improvements to console.markdown copy
[platform/upstream/nodejs.git] / benchmark / http_simple_auto.js
1 //
2 // Usage:
3 //   node benchmark/http_simple_auto.js <args> <target>
4 //
5 // Where:
6 //   <args>   Arguments to pass to `ab`.
7 //   <target> Target to benchmark, e.g. `bytes/1024` or `buffer/8192`.
8 //
9
10 var path = require("path");
11 var http = require("http");
12 var spawn = require("child_process").spawn;
13
14 var port = parseInt(process.env.PORT || 8000);
15
16 var fixed = ""
17 for (var i = 0; i < 20*1024; i++) {
18   fixed += "C";
19 }
20
21 var stored = {};
22 var storedBuffer = {};
23
24 var server = http.createServer(function (req, res) {
25   var commands = req.url.split("/");
26   var command = commands[1];
27   var body = "";
28   var arg = commands[2];
29   var n_chunks = parseInt(commands[3], 10);
30   var status = 200;
31
32   if (command == "bytes") {
33     var n = parseInt(arg, 10)
34     if (n <= 0)
35       throw "bytes called with n <= 0"
36     if (stored[n] === undefined) {
37       stored[n] = "";
38       for (var i = 0; i < n; i++) {
39         stored[n] += "C"
40       }
41     }
42     body = stored[n];
43
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);
51       }
52     }
53     body = storedBuffer[n];
54
55   } else if (command == "quit") {
56     res.connection.server.close();
57     body = "quitting";
58
59   } else if (command == "fixed") {
60     body = fixed;
61
62   } else if (command == "echo") {
63     res.writeHead(200, { "Content-Type": "text/plain",
64                          "Transfer-Encoding": "chunked" });
65     req.pipe(res);
66     return;
67
68   } else {
69     status = 404;
70     body = "not found\n";
71   }
72
73   // example: http://localhost:port/bytes/512/4
74   // sends a 512 byte body in 4 chunks of 128 bytes
75   if (n_chunks > 0) {
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;
81
82     for (var i = 0, n = (n_chunks - 1); i < n; ++i) {
83       res.write(body.slice(i * step, i * step + step));
84     }
85     res.end(body.slice((n_chunks - 1) * step));
86   } else {
87     var content_length = body.length.toString();
88
89     res.writeHead(status, { "Content-Type": "text/plain",
90                             "Content-Length": content_length });
91     res.end(body);
92   }
93
94 });
95
96 server.listen(port, function () {
97   var url = 'http://127.0.0.1:' + port + '/';
98
99   var n = process.argv.length - 1;
100   process.argv[n] = url + process.argv[n];
101
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() {
106     server.close();
107     process.nextTick(dump_mm_stats);
108   });
109 });
110
111 function dump_mm_stats() {
112   if (typeof gc != 'function') return;
113
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
118
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);
125     });
126   }
127 }