Using apachebench (ab) with Drupal 7 to load test site with authenticated users

apachebench is an excellent performance and load-testing tool for any website, and Drupal-based sites are no exception. A lot of Drupal sites, though, need to be measured not only under heavy anonymous traffic load (users who aren't logged in), but also under heavy authenticated-user load. has some good tips for ab testing, but the details for using ab's '-C' option (notice the capital C... C is for Cookie) are lacking. Basically, if you pass the -C option with a valid session ID/cookie, Drupal will send ab the page as if ab were authenticated.

Instead of constantly going into the database and looking up session IDs and such nonsense, I have a simple script, which is quite revised from the 2008-era script originally from 2bits that worked with Drupal 5, which will give you the proper ab commands for stress-testing your Drupal site under authenticated user load. Simply copy the attached script (source pasted below) to your site's docroot, and run the command from the command line as follows:

$ /path/to/drupal/root/ab-testing-cli.php 2 10

You'll get back the command to paste into the cli in order to test the URL you provided as an authenticated user. (Note: The sessions table needs to be populated for this to work, so someone (or a few someones) will need to have logged in during the past few hours/days for this to work correctly).

Here's the full code (file attached to bottom of post):

 * @file
 * Script to generate ab tests for logged in users using sessions from database.
 * This script is based on an older script by 2bits for load testing Drupal 5,
 * located at:
 * Place this script into the webroot of your Drupal site.
 * Usage (from command line):
 *   $ php /path/to/drupal/root/ab-testing-cli.php 2 200
 * After the script runs, it will output a list of commands for you to use to
 * test your website as a logged-in user.

// Set the variable below to your Drupal root (on the server).
$drupal_root = '/path/to/drupal/root/';

// If arguments not supplied properly, warn user.
if ($argc != 5) {
$prog = basename($argv[0]);
"Usage: $prog host url concurrency num_requests\n";

// Get the arguments for ab.
$url = $argv[2];
$number_concurrency = $argv[3];
$number_requests = $argv[4];

// Set this directory to your drupal root directory.

// Set up required variables to help Drupal bootstrap the correct site.
$_SERVER['HTTP_HOST'] = $argv[1];
$_SERVER['PHP_SELF'] = basename(__file__);
define('DRUPAL_ROOT', getcwd());

// Boostrap Drupal.

// Get as many sessions as the user calls for.
$results = db_query_range("SELECT sid FROM {sessions} WHERE uid > 1", 0, $number_concurrency)->fetchAll();

// Loop through the results and print the proper ab command for each session.
foreach ($results as $result) {
$cookie = session_name() . '=' . $result->sid;
"ab -c 1 -n $number_requests -C $cookie $url\n";


Very helpful, thanks a lot !
I think it definitely deservers its own drush command... Any volunteer ?

Perfect timing - this is just what I was looking for!

Check out drush - you'll find it much easier to create small utilities like this.