Skip to content

Instantly share code, notes, and snippets.

@jmgunn87
Created May 22, 2012 12:28
Show Gist options
  • Save jmgunn87/2768750 to your computer and use it in GitHub Desktop.
Save jmgunn87/2768750 to your computer and use it in GitHub Desktop.
ab tester
<?php
/**
* simple ab tester
* @author James Marshall-Gunn <[email protected]>
*/
/**
*
* Parse the basic colon delimited stuff here.
* All results in milliseconds.
*
* @param string $resultsString
* @param array $resultsArray
*/
function parseBasic ($resultsString, &$resultsArray) {
/** extract the range of results we want and parse them up */
$subStart = strpos ($resultsString, "Concurrency Level:");
$subEnd = strpos ($resultsString, "Transfer rate:");
$resultsParts = explode ("\n", substr ($resultsString, $subStart, $subEnd - $subStart));
/** parse here */
foreach ($resultsParts as $part) {
list ($key, $value) = explode(':', $part);
$resultsArray [ltrim (rtrim ($key))] = (int)$value;
}
}
/**
*
* Parse the basic connection times results
*
* for each key added to the results array there will
* be another array of ordered numeric values that
* represent the min, mean[+/-sd], median and max results
* in milliseconds respectively.
*
* @param string $resultsString
* @param array $resultsArray
*/
function parseConnectionTimes ($resultsString, &$resultsArray) {
/** extract the range of results we want and parse them up */
$subStart = strpos ($resultsString, "Connect:");
$subEnd = strpos ($resultsString, "Percentage of the requests served within a certain time (ms)");
$resultsParts = explode ("\n", substr ($resultsString, $subStart, $subEnd - $subStart));
foreach ($resultsParts as $part) {
if ($part != '') {
list ($key, $value) = explode (':', $part);
$resultsArray [ltrim (rtrim ($key))] =
preg_split ("/\s+/", $value, -1, PREG_SPLIT_NO_EMPTY);
}
}
}
/**
*
* Parse result percentages.
*
* @param string $resultsString
* @param array $resultsArray
*/
function parsePercentages ($resultsString, &$resultsArray) {
/** extract the range of results we want and parse them up */
$subStart = strpos ($resultsString, "Percentage of the requests served within a certain time (ms)") +
strlen ("Percentage of the requests served within a certain time (ms)");
$subEnd = strlen ($resultsString);
$results = substr ($resultsString, $subStart, $subEnd - $subStart);
$resultsArray['Percentages'] = preg_split ("/\s+[a-zA-Z()]*/", $results, -1, PREG_SPLIT_NO_EMPTY);
}
/**
*
* Run a single AB test
*
* @param string $host
* @param int $numberOfRequests
* @param int $concurrentConnections
*/
function runTest ($host, $numberOfRequests, $concurrentConnections) {
$command = "ab -n $numberOfRequests -c $concurrentConnections $host ";
$results = `$command`;
$resultsArray = array ();
parseBasic ($results, $resultsArray);
parseConnectionTimes ($results, $resultsArray);
parsePercentages ($results, $resultsArray);
$resultsArray ['pass'] = true;
return $resultsArray;
}
/**
*
* general script configurations
* followed by the application driver
*
* */
$hosts = array
(
//'http://localhost.com/' => array(),
);
$numberOfRequests = 500;
$timeout = 120;
$concurrentConnections = 10;
$requestsStep = 0;
$concurrencyStep = 10;
$keepTesting = false;
echo "\n_-_ starting tests _-_\n\n";
do{
echo "**** entering pass ****\n\n";
foreach ($hosts as $host => &$resultsHistory) {
echo "testing host $host\n";
$results = runTest ($host, $numberOfRequests, $concurrentConnections);
$keepTesting = isset ($results) && $results['pass'];
$resultsHistory []= $results;
echo "\n";
}
echo "**** exiting pass ****\n\n";
$numberOfRequests += $requestsStep;
$concurrentConnections += $concurrencyStep;
} while ($keepTesting && $concurrentConnections < 50);
echo "\n-_- test complete -_-\n\n";
var_dump($hosts);exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment