src: move debug agent from deps/ to src/
[platform/upstream/nodejs.git] / benchmark / http_bench.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 var spawn = require('child_process').spawn;
23 var cluster = require('cluster');
24 var http = require('http');
25
26 var options = {
27   mode: 'master',
28   host: '127.0.0.1',
29   port: 22344,
30   path: '/',
31   servers: 1,
32   clients: 1
33 };
34
35 for (var i = 2; i < process.argv.length; ++i) {
36   var args = process.argv[i].split('=', 2);
37   var key = args[0];
38   var val = args[1];
39   options[key] = val;
40 }
41
42 switch (options.mode) {
43 case 'master': startMaster(); break;
44 case 'server': startServer(); break;
45 case 'client': startClient(); break;
46 default: throw new Error('Bad mode: ' + options.mode);
47 }
48
49 process.title = 'http_bench[' + options.mode + ']';
50
51 // monkey-patch the log functions so they include name + pid
52 console.log = patch(console.log);
53 console.trace = patch(console.trace);
54 console.error = patch(console.error);
55
56 function patch(fun) {
57   var prefix = process.title + '[' + process.pid + '] ';
58   return function() {
59     var args = Array.prototype.slice.call(arguments);
60     args[0] = prefix + args[0];
61     return fun.apply(console, args);
62   };
63 }
64
65 function startMaster() {
66   if (!cluster.isMaster) return startServer();
67
68   for (var i = ~~options.servers; i > 0; --i) cluster.fork();
69
70   for (var i = ~~options.clients; i > 0; --i) {
71     var cp = spawn(process.execPath, [__filename, 'mode=client']);
72     cp.stdout.pipe(process.stdout);
73     cp.stderr.pipe(process.stderr);
74   }
75 }
76
77 function startServer() {
78   http.createServer(onRequest).listen(options.port, options.host);
79
80   var body = Array(1024).join('x');
81   var headers = {'Content-Length': '' + body.length};
82
83   function onRequest(req, res) {
84     req.on('error', onError);
85     res.on('error', onError);
86     res.writeHead(200, headers);
87     res.end(body);
88   }
89
90   function onError(err) {
91     console.error(err.stack);
92   }
93 }
94
95 function startClient() {
96   // send off a bunch of concurrent requests
97   // TODO make configurable
98   sendRequest();
99   sendRequest();
100
101   function sendRequest() {
102     var req = http.request(options, onConnection);
103     req.on('error', onError);
104     req.end();
105   }
106
107   // add a little back-off to prevent EADDRNOTAVAIL errors, it's pretty easy
108   // to exhaust the available port range
109   function relaxedSendRequest() {
110     setTimeout(sendRequest, 1);
111   }
112
113   function onConnection(res) {
114     res.on('error', onError);
115     res.on('data', onData);
116     res.on('end', relaxedSendRequest);
117   }
118
119   function onError(err) {
120     console.error(err.stack);
121     relaxedSendRequest();
122   }
123
124   function onData(data) {
125     // this space intentionally left blank
126   }
127 }