| 1 |
<?php
|
| 2 |
// $Id: ab.module,v 1.2 2008/09/12 18:23:38 tjholowaychuk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* ApacheBench support and report generator.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/* -----------------------------------------------------------------
|
| 10 |
|
| 11 |
Hook Implementations
|
| 12 |
|
| 13 |
------------------------------------------------------------------ */
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_perm().
|
| 17 |
*/
|
| 18 |
function ab_perm() {
|
| 19 |
return array('administer ab', 'view ab reports');
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_menu().
|
| 24 |
*/
|
| 25 |
function ab_menu() {
|
| 26 |
$items = array();
|
| 27 |
|
| 28 |
$items['admin/reports/ab'] = array(
|
| 29 |
'title' => 'ApacheBench Reports',
|
| 30 |
'page callback' => 'ab_reports',
|
| 31 |
'access arguments' => array('view ab reports'),
|
| 32 |
'file' => 'ab.admin.inc',
|
| 33 |
);
|
| 34 |
$items['admin/settings/ab'] = array(
|
| 35 |
'title' => 'ApacheBench',
|
| 36 |
'page callback' => 'drupal_get_form',
|
| 37 |
'page arguments' => array('ab_settings'),
|
| 38 |
'access arguments' => array('administer ab'),
|
| 39 |
'file' => 'ab.admin.inc',
|
| 40 |
);
|
| 41 |
|
| 42 |
return $items;
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Implementation of hook_cron().
|
| 47 |
*/
|
| 48 |
function ab_cron() {
|
| 49 |
if ($last_run = variable_get('cron_last', FALSE)){
|
| 50 |
$frequency = variable_get('ab_frequency', 0);
|
| 51 |
$last_run_hours = floor((time() - $last_run) / (60 * 60));
|
| 52 |
|
| 53 |
// Ensure we can invoke ab
|
| 54 |
if ($frequency <= 0 || $last_run_hours <= $frequency){
|
| 55 |
return;
|
| 56 |
}
|
| 57 |
|
| 58 |
// Perform tests
|
| 59 |
$uris = explode("\n", variable_get('ab_uris', ''));
|
| 60 |
// @todo allow $uri(s) for ab().
|
| 61 |
foreach((array) $uris AS $uri){
|
| 62 |
ab(trim($uri), variable_get('ab_requests', 100), variable_get('ab_concurrency', 25), TRUE);
|
| 63 |
}
|
| 64 |
|
| 65 |
// Log success
|
| 66 |
watchdog('ab', 'ApacheBench invoked on %count uris.', array('%count' => count($uris)), WATCHDOG_INFO);
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
| 70 |
/* -----------------------------------------------------------------
|
| 71 |
|
| 72 |
Public API
|
| 73 |
|
| 74 |
------------------------------------------------------------------ */
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Invoke ApacheBench
|
| 78 |
*
|
| 79 |
* @param string $uri
|
| 80 |
* Valid absolute URI and path such as 'http://vision-media.ca/',
|
| 81 |
* note the trailing slash (/).
|
| 82 |
*
|
| 83 |
* @param int $requests
|
| 84 |
* (optional) Total number of requests.
|
| 85 |
*
|
| 86 |
* @param int $concurrency
|
| 87 |
* (optional) Concurrency of requests.
|
| 88 |
*
|
| 89 |
* @param bool $store
|
| 90 |
* (optional) Bool indicating database storage for reporting.
|
| 91 |
*
|
| 92 |
* @return mixed
|
| 93 |
* - success: Array of results
|
| 94 |
* - failure: int
|
| 95 |
*/
|
| 96 |
function ab($uri, $requests = 100, $concurrency = 25, $store = FALSE) {
|
| 97 |
$results = array();
|
| 98 |
|
| 99 |
// Ensure concurrency is larger than requests
|
| 100 |
if ($concurrency > $requests){
|
| 101 |
return -1;
|
| 102 |
}
|
| 103 |
|
| 104 |
// Preven malicious args
|
| 105 |
if (!is_numeric($concurrency) || !is_numeric($requests)){
|
| 106 |
return -3;
|
| 107 |
}
|
| 108 |
|
| 109 |
// Ensure a valid uri
|
| 110 |
if (!valid_url($uri, TRUE)){
|
| 111 |
return -2;
|
| 112 |
}
|
| 113 |
|
| 114 |
$results = ab_parse_output(shell_exec("ab -n {$requests} -c {$concurrency} {$uri}"));
|
| 115 |
|
| 116 |
// Store data
|
| 117 |
if ($store === TRUE){
|
| 118 |
$args = array();
|
| 119 |
$args[] = $results['complete_requests'];
|
| 120 |
$args[] = $results['document_length'];
|
| 121 |
$args[] = $results['failed_requests'];
|
| 122 |
$args[] = $results['total_transferred'];
|
| 123 |
$args[] = $results['html_transferred'];
|
| 124 |
$args[] = $results['requests_per_second'];
|
| 125 |
$args[] = $results['time_taken_for_tests'];
|
| 126 |
$args[] = $uri;
|
| 127 |
$args[] = $concurrency;
|
| 128 |
$args[] = $requests;
|
| 129 |
$args[] = time();
|
| 130 |
db_query("
|
| 131 |
INSERT INTO {ab_results}
|
| 132 |
SET
|
| 133 |
rid = '',
|
| 134 |
complete_requests = %d,
|
| 135 |
document_length = %d,
|
| 136 |
failed_requests = %d,
|
| 137 |
total_transferred = %d,
|
| 138 |
html_transferred = %d,
|
| 139 |
requests_per_second = %d,
|
| 140 |
time_taken_for_tests = %f,
|
| 141 |
uri = '%s',
|
| 142 |
concurrency = %d,
|
| 143 |
requests = %d,
|
| 144 |
created = %d
|
| 145 |
", $args);
|
| 146 |
}
|
| 147 |
|
| 148 |
return $results;
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Clear all ab results.
|
| 153 |
*/
|
| 154 |
function ab_clear_all() {
|
| 155 |
db_query("DELETE FROM {ab_results}");
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Parse ab invocation output.
|
| 160 |
*
|
| 161 |
* @param string $output
|
| 162 |
*
|
| 163 |
* @return array
|
| 164 |
*/
|
| 165 |
function ab_parse_output($output) {
|
| 166 |
$results = array();
|
| 167 |
|
| 168 |
$cols = array();
|
| 169 |
$cols[] = 'complete requests';
|
| 170 |
$cols[] = 'document length';
|
| 171 |
$cols[] = 'failed requests';
|
| 172 |
$cols[] = 'total transferred';
|
| 173 |
$cols[] = 'html transferred';
|
| 174 |
$cols[] = 'requests per second';
|
| 175 |
$cols[] = 'time taken for tests';
|
| 176 |
|
| 177 |
preg_match_all('!('. implode('|', $cols) . '):\s+([0-9\.]+)!i', $output, $matches, PREG_SET_ORDER);
|
| 178 |
foreach((array) $matches AS $match){
|
| 179 |
$results[drupal_strtolower(strtr($match[1], ' ', '_'))] = $match[2];
|
| 180 |
}
|
| 181 |
|
| 182 |
return $results;
|
| 183 |
}
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Require ABChart class.
|
| 187 |
*/
|
| 188 |
function ab_require_chart() {
|
| 189 |
if (module_exists('open_flash_chart_api')){
|
| 190 |
require_once(dirname(__FILE__) . '/ab.chart.inc');
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|
| 194 |
|