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'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25// This test requires the program 'ab' 26const http = require('http'); 27const exec = require('child_process').exec; 28 29const bodyLength = 12345; 30 31const body = 'c'.repeat(bodyLength); 32 33const server = http.createServer(function(req, res) { 34 res.writeHead(200, { 35 'Content-Length': bodyLength, 36 'Content-Type': 'text/plain' 37 }); 38 res.end(body); 39}); 40 41function runAb(opts, callback) { 42 const command = `ab ${opts} http://127.0.0.1:${server.address().port}/`; 43 exec(command, function(err, stdout, stderr) { 44 if (err) { 45 if (/ab|apr/i.test(stderr)) { 46 common.printSkipMessage(`problem spawning \`ab\`.\n${stderr}`); 47 process.reallyExit(0); 48 } 49 throw err; 50 } 51 52 let m = /Document Length:\s*(\d+) bytes/i.exec(stdout); 53 const documentLength = parseInt(m[1]); 54 55 m = /Complete requests:\s*(\d+)/i.exec(stdout); 56 const completeRequests = parseInt(m[1]); 57 58 m = /HTML transferred:\s*(\d+) bytes/i.exec(stdout); 59 const htmlTransferred = parseInt(m[1]); 60 61 assert.strictEqual(bodyLength, documentLength); 62 assert.strictEqual(completeRequests * documentLength, htmlTransferred); 63 64 if (callback) callback(); 65 }); 66} 67 68server.listen(0, common.mustCall(function() { 69 runAb('-c 1 -n 10', common.mustCall(function() { 70 console.log('-c 1 -n 10 okay'); 71 72 runAb('-c 1 -n 100', common.mustCall(function() { 73 console.log('-c 1 -n 100 okay'); 74 75 runAb('-c 1 -n 1000', common.mustCall(function() { 76 console.log('-c 1 -n 1000 okay'); 77 server.close(); 78 })); 79 })); 80 })); 81})); 82