1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var spawn = require('child_process').spawn;
23 var cluster = require('cluster');
24 var http = require('http');
35 for (var i = 2; i < process.argv.length; ++i) {
36 var args = process.argv[i].split('=', 2);
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);
49 process.title = 'http_bench[' + options.mode + ']';
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);
57 var prefix = process.title + '[' + process.pid + '] ';
59 var args = Array.prototype.slice.call(arguments);
60 args[0] = prefix + args[0];
61 return fun.apply(console, args);
65 function startMaster() {
66 if (!cluster.isMaster) return startServer();
68 for (var i = ~~options.servers; i > 0; --i) cluster.fork();
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);
77 function startServer() {
78 http.createServer(onRequest).listen(options.port, options.host);
80 var body = Array(1024).join('x');
81 var headers = {'Content-Length': '' + body.length};
83 function onRequest(req, res) {
84 req.on('error', onError);
85 res.on('error', onError);
86 res.writeHead(200, headers);
90 function onError(err) {
91 console.error(err.stack);
95 function startClient() {
96 // send off a bunch of concurrent requests
97 // TODO make configurable
101 function sendRequest() {
102 var req = http.request(options, onConnection);
103 req.on('error', onError);
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);
113 function onConnection(res) {
114 res.on('error', onError);
115 res.on('data', onData);
116 res.on('end', relaxedSendRequest);
119 function onError(err) {
120 console.error(err.stack);
121 relaxedSendRequest();
124 function onData(data) {
125 // this space intentionally left blank