2010-12-03 09:03:18 +08:00
|
|
|
// This test requires the program 'ab'
|
2010-12-05 07:20:34 +08:00
|
|
|
var common = require('../common');
|
2010-12-06 06:33:52 +08:00
|
|
|
var assert = require('assert');
|
2010-12-05 08:11:57 +08:00
|
|
|
var http = require('http');
|
|
|
|
var exec = require('child_process').exec;
|
2009-11-06 20:42:56 +08:00
|
|
|
|
2010-12-05 08:11:57 +08:00
|
|
|
var body = 'hello world\n';
|
|
|
|
var server = http.createServer(function(req, res) {
|
2010-04-23 08:22:03 +08:00
|
|
|
res.writeHead(200, {
|
2010-12-03 09:03:18 +08:00
|
|
|
'Content-Length': body.length,
|
|
|
|
'Content-Type': 'text/plain'
|
2009-11-06 20:42:56 +08:00
|
|
|
});
|
2010-02-17 14:16:29 +08:00
|
|
|
res.write(body);
|
2010-04-09 01:44:22 +08:00
|
|
|
res.end();
|
2009-11-06 20:42:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var keepAliveReqSec = 0;
|
|
|
|
var normalReqSec = 0;
|
|
|
|
|
|
|
|
|
|
|
|
function runAb(opts, callback) {
|
2010-12-03 09:03:18 +08:00
|
|
|
var command = 'ab ' + opts + ' http://127.0.0.1:' + common.PORT + '/';
|
|
|
|
exec(command, function(err, stdout, stderr) {
|
2010-02-20 10:54:25 +08:00
|
|
|
if (err) {
|
2010-12-03 09:03:18 +08:00
|
|
|
if (stderr.indexOf('ab') >= 0) {
|
|
|
|
console.log('ab not installed? skipping test.\n' + stderr);
|
2010-08-13 23:54:40 +08:00
|
|
|
process.reallyExit(0);
|
|
|
|
}
|
2010-02-20 10:54:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-02-20 08:55:46 +08:00
|
|
|
if (err) throw err;
|
|
|
|
var matches = /Requests per second:\s*(\d+)\./mi.exec(stdout);
|
|
|
|
var reqSec = parseInt(matches[1]);
|
|
|
|
|
|
|
|
matches = /Keep-Alive requests:\s*(\d+)/mi.exec(stdout);
|
|
|
|
var keepAliveRequests;
|
|
|
|
if (matches) {
|
|
|
|
keepAliveRequests = parseInt(matches[1]);
|
|
|
|
} else {
|
|
|
|
keepAliveRequests = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(reqSec, keepAliveRequests);
|
|
|
|
});
|
2009-11-06 20:42:56 +08:00
|
|
|
}
|
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
server.listen(common.PORT, function() {
|
|
|
|
runAb('-k -c 100 -t 2', function(reqSec, keepAliveRequests) {
|
2010-04-06 02:32:18 +08:00
|
|
|
keepAliveReqSec = reqSec;
|
|
|
|
assert.equal(true, keepAliveRequests > 0);
|
2010-12-03 09:03:18 +08:00
|
|
|
console.log('keep-alive: ' + keepAliveReqSec + ' req/sec');
|
2010-04-06 02:32:18 +08:00
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
runAb('-c 100 -t 2', function(reqSec, keepAliveRequests) {
|
2010-04-06 02:32:18 +08:00
|
|
|
normalReqSec = reqSec;
|
|
|
|
assert.equal(0, keepAliveRequests);
|
2010-12-03 09:03:18 +08:00
|
|
|
console.log('normal: ' + normalReqSec + ' req/sec');
|
2010-04-06 02:32:18 +08:00
|
|
|
server.close();
|
|
|
|
});
|
2009-11-06 20:42:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
process.addListener('exit', function() {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, normalReqSec > 50);
|
|
|
|
assert.equal(true, keepAliveReqSec > 50);
|
|
|
|
assert.equal(true, normalReqSec < keepAliveReqSec);
|
2009-11-06 20:42:56 +08:00
|
|
|
});
|