2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-12-30 18:38:06 -05:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2010-12-06 01:33:52 +03:00
|
|
|
// This test requires the program 'ab'
|
2016-12-30 18:38:06 -05:00
|
|
|
const http = require('http');
|
|
|
|
const exec = require('child_process').exec;
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const bodyLength = 12345;
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const body = 'c'.repeat(bodyLength);
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = http.createServer(function(req, res) {
|
2010-05-09 15:07:54 -07:00
|
|
|
res.writeHead(200, {
|
2010-12-06 01:33:52 +03:00
|
|
|
'Content-Length': bodyLength,
|
|
|
|
'Content-Type': 'text/plain'
|
2010-05-09 15:07:54 -07:00
|
|
|
});
|
|
|
|
res.end(body);
|
|
|
|
});
|
|
|
|
|
|
|
|
function runAb(opts, callback) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const command = `ab ${opts} http://127.0.0.1:${server.address().port}/`;
|
2010-12-06 01:33:52 +03:00
|
|
|
exec(command, function(err, stdout, stderr) {
|
2010-05-09 15:07:54 -07:00
|
|
|
if (err) {
|
2012-06-15 18:58:27 -07:00
|
|
|
if (/ab|apr/mi.test(stderr)) {
|
2016-05-11 15:34:52 -04:00
|
|
|
common.skip('problem spawning `ab`.\n' + stderr);
|
2010-08-13 11:54:40 -04:00
|
|
|
process.reallyExit(0);
|
|
|
|
}
|
2010-05-09 15:07:54 -07:00
|
|
|
process.exit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let m = /Document Length:\s*(\d+) bytes/mi.exec(stdout);
|
|
|
|
const documentLength = parseInt(m[1]);
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2016-01-30 20:43:11 -08:00
|
|
|
m = /Complete requests:\s*(\d+)/mi.exec(stdout);
|
2017-01-08 13:19:00 +00:00
|
|
|
const completeRequests = parseInt(m[1]);
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2016-01-30 20:43:11 -08:00
|
|
|
m = /HTML transferred:\s*(\d+) bytes/mi.exec(stdout);
|
2017-01-08 13:19:00 +00:00
|
|
|
const htmlTransfered = parseInt(m[1]);
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(bodyLength, documentLength);
|
|
|
|
assert.strictEqual(completeRequests * documentLength, htmlTransfered);
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2010-12-06 01:33:52 +03:00
|
|
|
if (callback) callback();
|
2010-05-09 15:07:54 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-15 15:43:24 -04:00
|
|
|
server.listen(0, common.mustCall(function() {
|
|
|
|
runAb('-c 1 -n 10', common.mustCall(function() {
|
2010-12-06 01:33:52 +03:00
|
|
|
console.log('-c 1 -n 10 okay');
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2016-07-15 15:43:24 -04:00
|
|
|
runAb('-c 1 -n 100', common.mustCall(function() {
|
2010-12-06 01:33:52 +03:00
|
|
|
console.log('-c 1 -n 100 okay');
|
2010-05-09 15:07:54 -07:00
|
|
|
|
2016-07-15 15:43:24 -04:00
|
|
|
runAb('-c 1 -n 1000', common.mustCall(function() {
|
2010-12-06 01:33:52 +03:00
|
|
|
console.log('-c 1 -n 1000 okay');
|
2010-05-09 15:07:54 -07:00
|
|
|
server.close();
|
2016-07-15 15:43:24 -04:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|